ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Services, Dependency
Injection and Containers
In Drupal 8
About Me
Sushil Hanwate
Twitter: @thesushyl
Drupal Developer @
What is a Service?
A Service is any PHP object that performs some sort of "global" task.
It's a purposefully-generic name used in computer science to describe an
object that's created for a specific purpose (e.g. delivering emails).
As a rule, a PHP object is a service if it is used globally in your application.
Defining Services
// user.services.yml
services:
access_check.permission:
class: DrupaluserAccessPermissionAccessCheck // ¡®modules/user/src/access/PermissionAccessCheck.php¡¯
tags:
- { name: access_check, applies_to: _permission }
// core.services.yml
services:
path.alias_manager:
class: DrupalCorePathAliasManager // ¡®/core/lib/drupal/core/path/AliasManager.php¡¯
arguments: ['@path.alias_storage', '@path.alias_whitelist', '@language_manager', '@cache.data']
Obtaining Services
Service Location (Procedural)
Dependency Injection (Used in classes)
Obtaining Services: Service Location
// Get service from outside of a class.
$service = Drupal::service('service_name');
// If you have $container variable
$service = $container->get('service_name');
// Example services
$cache = Drupal::cache();
$lock = Drupal::lock()->acquire($lock_name)
Obtaining Services: Service Location
// Source
$cache = Drupal::cache();
Dependency Injection - Definition
Dependency injection is a software design pattern in which
one or more dependencies/services are injected, or passed by
reference , into a dependent object/client and are made part
of the client's state.
The pattern separates the creation of a client's dependencies
from its own behavior, which allows program designs to be
loosely coupled and to follow the dependency inversion and
single responsibility principles.
Obtaining Services: Dependency Injection
1. DI is used in classes.
2. If your class is a service, inject dependencies via arguments in *.services.yml
services:
path.alias_manager:
class: DrupalCorePathAliasManager // ¡®/core/lib/drupal/core/path/AliasManager.php¡¯
arguments: ['@path.alias_storage', '@path.alias_whitelist', '@language_manager', '@cache.data']
// code
public function __construct (AliasStorageInterface $storage,
AliasWhitelistInterface $whitelist,
LanguageManagerInterface $language_manager,
CacheBackendInterface $cache) {
Overriding Services
1. Locate default class and interface
2. Define a class that provides same services with different way implementing interface
and/or overriding the class, say
Drupalfoo_barMyNewServiceClass
1. Define a class that implements DrupalCoreDependencyInjectionServiceModifierInterface called
Drupalfoo_barFooBarServiceProvider
2. Put this in alter() method
public function alter(ContainerBuilder $container) {
$definition = $container->getDefinition('service.name');
$definition->setClass(Drupalfoo_barMyNewServiceClass);
}
Why Dependency Injection?
Clutter-free
Re-usable
Testable
References
1.http://www.slideshare.net/katbailey/di-drupal8
2.https://www.youtube.com/watch?v=TAXgizY-p4k
3.https://docs.acquia.com/articles/drupal-8-dependency-
injection
Questions?

More Related Content

Services, dependency injection and containers

  • 1. Services, Dependency Injection and Containers In Drupal 8
  • 2. About Me Sushil Hanwate Twitter: @thesushyl Drupal Developer @
  • 3. What is a Service? A Service is any PHP object that performs some sort of "global" task. It's a purposefully-generic name used in computer science to describe an object that's created for a specific purpose (e.g. delivering emails). As a rule, a PHP object is a service if it is used globally in your application.
  • 4. Defining Services // user.services.yml services: access_check.permission: class: DrupaluserAccessPermissionAccessCheck // ¡®modules/user/src/access/PermissionAccessCheck.php¡¯ tags: - { name: access_check, applies_to: _permission } // core.services.yml services: path.alias_manager: class: DrupalCorePathAliasManager // ¡®/core/lib/drupal/core/path/AliasManager.php¡¯ arguments: ['@path.alias_storage', '@path.alias_whitelist', '@language_manager', '@cache.data']
  • 5. Obtaining Services Service Location (Procedural) Dependency Injection (Used in classes)
  • 6. Obtaining Services: Service Location // Get service from outside of a class. $service = Drupal::service('service_name'); // If you have $container variable $service = $container->get('service_name'); // Example services $cache = Drupal::cache(); $lock = Drupal::lock()->acquire($lock_name)
  • 7. Obtaining Services: Service Location // Source $cache = Drupal::cache();
  • 8. Dependency Injection - Definition Dependency injection is a software design pattern in which one or more dependencies/services are injected, or passed by reference , into a dependent object/client and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.
  • 9. Obtaining Services: Dependency Injection 1. DI is used in classes. 2. If your class is a service, inject dependencies via arguments in *.services.yml services: path.alias_manager: class: DrupalCorePathAliasManager // ¡®/core/lib/drupal/core/path/AliasManager.php¡¯ arguments: ['@path.alias_storage', '@path.alias_whitelist', '@language_manager', '@cache.data'] // code public function __construct (AliasStorageInterface $storage, AliasWhitelistInterface $whitelist, LanguageManagerInterface $language_manager, CacheBackendInterface $cache) {
  • 10. Overriding Services 1. Locate default class and interface 2. Define a class that provides same services with different way implementing interface and/or overriding the class, say Drupalfoo_barMyNewServiceClass 1. Define a class that implements DrupalCoreDependencyInjectionServiceModifierInterface called Drupalfoo_barFooBarServiceProvider 2. Put this in alter() method public function alter(ContainerBuilder $container) { $definition = $container->getDefinition('service.name'); $definition->setClass(Drupalfoo_barMyNewServiceClass); }