Upload
srijan-technologies
View
55
Download
0
Embed Size (px)
Citation preview
Demystifying Dependency Injection And Services In Drupal 8
Jyoti Singh | Developer| Srijan Technologies
#SrijanWW | @srijan
#SrijanWW | @srijan
● Problems in Drupal 7● Dependency Injection● Services and Services Container● Drupal 8 core services and how to use them.● Using Services in contributed module.
Objective Of The Session
#SrijanWW | @srijan
● No way to Reuse the code (not OVERRIDE)
● - for eg : Copy the callback entirely and replace it with your code.
#SrijanWW | @srijan
How can we test something?
- Create mock objects? NO. - Want a simple test class? Bootstrap Drupal first.
#SrijanWW | @srijan
Lot of Cluttering Happens
- Excessive use of alter hooks - preprocess for calculations- PHP in template files
#SrijanWW | @srijan
Some procedural code
*foo_bar knows about the user object.*node_make_me_look_cool needs a function from another module.*You need bootstrapped Drupal to use module_load_include, or at least include the file that
declares it.*foo_bar assumes some default database connection.
#SrijanWW | @srijan
Dependency injection is an advanced software design pattern. It implements “inversion of control”, which means that reusable code calls task specific code.
#SrijanWW | @srijan
What does this mean for us Drupal developers?
It means we can write more efficient code by reducing the load. We only load what we need.
Type Of Injections
Type Of Injections
Constructor injection
Setter injection
property injection
Setter injectionSetter injection
class User{ function setSessionStorage($storage){ $this->storage = $storage;}}
class User{ public $sessionStorage;}
$user->sessionStorage = $storage;
class User{ function construct($storage) {
$this->storage = $storage;}}
#SrijanWW | @srijan
Defining the Services :
services:
example.access_checker:
class:
Drupal\example\Access\CustomAccessCheck
arguments: ['@current_user']
tags:
- { name: access_check, applies_to:
_example_access_check }
#SrijanWW | @srijan
namespace Drupal\example\Access;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
class CustomAccessCheck implements AccessInterface {
/**
* A custom access check.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Run access checks for this account.
*/
public function access(AccountInterface $account) {
return $account->hasPermission('custom_permission’);
}
}
Service is simply an object, usually with one instance of each service's class for each service on a site. For example, Drupal 8 sites have a service for sending email, for logging errors, for making HTTP requests, and dozens of other common tasks.
#SrijanWW | @srijan
Some of the commonly used services in Drupal 8 core are :
● entity.query and entity.manager – Services use to access the informationa about Drupal entities.
● config.fatory – Loads the Config information out of your Drupal site
● config.storage - Service that knows where to store variables on your site
● config.typed - Helps to store different data types in config object.
● event_dispatcher - helps to "lazy load" classes so loading code and instantiating objects only happens when the objects are actually needed.
● current_user – The service that provides data for the drupal user.
#SrijanWW | @srijan
● Create a src Folder inside the custom module.● Create a EvenSubscriber Class inside the src
Folder● Implement the EventSubscriberInterface .● Implement some of the basic functions of
EventSubscriberInterface like onRespond() and getSubscribedEvents()
● Create a services.yml file inside the root folder of the custom module.
#SrijanWW | @srijan
This is how the EvenSubscriber Class looks like :
class ZipangSubscriber implements EventSubscriberInterface {
public function onRespond(FilterResponseEvent $event) { $response = $event->getResponse(); $response->headers->set('X-Custom-Header', '******'); }
public static function getSubscribedEvents() { $events[KernelEvents::RESPONSE][] = array('onRespond'); return $events; }}
#SrijanWW | @srijan
Service Tags
Tags are used to define a group of related services, or to specify some aspect of how the service behaves. Some of the tags used are :● event_subscriber: Indicates an event subscriber
service. ● access_check: Indicates a route access checking
service;● cache.bin: Indicates a cache bin service,etc
#SrijanWW | @srijan