SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Downloaden Sie, um offline zu lesen
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
Why Dependency Injection
How we want our code to look
like?
#SrijanWW | @srijan
How we want our code to look
like?
#SrijanWW | @srijan
How It Actually Looks
#SrijanWW | @srijan
●
#SrijanWW | @srijan
#SrijanWW | @srijan
Problems In Drupal 7!
#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
Let’s take a wrong approach
#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
User class uses sessions to store
language
#SrijanWW | @srijan
What Is Dependency Injection?
#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
Example !
#SrijanWW | @srijan
Defining the Services :
services:
example.access_checker:
class:
DrupalexampleAccessCustomAccessCheck
arguments: ['@current_user']
tags:
- { name: access_check, applies_to:
_example_access_check }
#SrijanWW | @srijan
namespace DrupalexampleAccess;
use DrupalCoreRoutingAccessAccessInterface;
use DrupalCoreSessionAccountInterface;
class CustomAccessCheck implements AccessInterface {
/**
* A custom access check.
*
* @param DrupalCoreSessionAccountInterface $account
* Run access checks for this account.
*/
public function access(AccountInterface $account) {
return $account->hasPermission('custom_permission’);
}
}
What are services and services
container?
#SrijanWW | @srijan
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
Goal : We want to write a code that is :
● Reusable
● Clutter Free
● Testeable
#SrijanWW | @srijan
The less your code knows, the
more reusable it is!
#SrijanWW | @srijan
Core Services in Drupal 8
#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
Creating Services in
Custom Module
#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
Questions
#SrijanWW | @srijan

Weitere ähnliche Inhalte

Was ist angesagt?

React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorialMohammed Fazuluddin
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
Introduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalsIntroduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalswebsyndicate
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to reactkiranabburi
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.jsDoug Neiner
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
Couchbase - Introduction
Couchbase - IntroductionCouchbase - Introduction
Couchbase - IntroductionKnoldus Inc.
 
Understanding Facebook's React.js
Understanding Facebook's React.jsUnderstanding Facebook's React.js
Understanding Facebook's React.jsFederico Torre
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online TrainingLearntek1
 

Was ist angesagt? (20)

React js
React jsReact js
React js
 
Reactjs
Reactjs Reactjs
Reactjs
 
React.js+Redux Workshops
React.js+Redux WorkshopsReact.js+Redux Workshops
React.js+Redux Workshops
 
ReactJs
ReactJsReactJs
ReactJs
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Introduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentalsIntroduction to ReactJs & fundamentals
Introduction to ReactJs & fundamentals
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
ReactJS for Beginners
ReactJS for BeginnersReactJS for Beginners
ReactJS for Beginners
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
 
React and redux
React and reduxReact and redux
React and redux
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Couchbase - Introduction
Couchbase - IntroductionCouchbase - Introduction
Couchbase - Introduction
 
Understanding Facebook's React.js
Understanding Facebook's React.jsUnderstanding Facebook's React.js
Understanding Facebook's React.js
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React js Online Training
React js Online TrainingReact js Online Training
React js Online Training
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 

Ähnlich wie Final dependency presentation.odp

[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...Srijan Technologies
 
Angular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияAngular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияOlga Lavrentieva
 
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Community
 
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr... [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...Srijan Technologies
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015Pushkar Chivate
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Greg Szczotka
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applicationsDivyanshGupta922023
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development GuideNitin Giri
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJSInfragistics
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDatabricks
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratiehcderaad
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoRubén Mondéjar Andreu
 
7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script TaskPramod Singla
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 
BDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesBDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesSauce Labs
 

Ähnlich wie Final dependency presentation.odp (20)

[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
[Srijan Wednesday Webinars] New Recipe of Decoupling: Drupal 8, Symfony and S...
 
Angular js
Angular jsAngular js
Angular js
 
Angular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решенияAngular.js опыт использования, проблемы и решения
Angular.js опыт использования, проблемы и решения
 
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
 
Spring boot
Spring bootSpring boot
Spring boot
 
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr... [Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
[Srijan Wednesday Webinar] Decoupled Demystified: The Present & Future of Dr...
 
Introduction to AngularJs
Introduction to AngularJsIntroduction to AngularJs
Introduction to AngularJs
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014
 
jquery summit presentation for large scale javascript applications
jquery summit  presentation for large scale javascript applicationsjquery summit  presentation for large scale javascript applications
jquery summit presentation for large scale javascript applications
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
Dependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark ApplicationsDependency Injection in Apache Spark Applications
Dependency Injection in Apache Spark Applications
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie
 
Grails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duoGrails & Angular: unleashing the dynamic duo
Grails & Angular: unleashing the dynamic duo
 
7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task7\9 SSIS 2008R2_Training - Script Task
7\9 SSIS 2008R2_Training - Script Task
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
Angular Seminar-js
Angular Seminar-jsAngular Seminar-js
Angular Seminar-js
 
BDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User StoriesBDD Selenium for Agile Teams - User Stories
BDD Selenium for Agile Teams - User Stories
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 

Mehr von Srijan Technologies

[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...Srijan Technologies
 
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...Srijan Technologies
 
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...Srijan Technologies
 
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital SignageSrijan Technologies
 
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...Srijan Technologies
 
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPRSrijan Technologies
 
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of BusinessSrijan Technologies
 
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that WorksSrijan Technologies
 
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization EngineSrijan Technologies
 
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing [Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing Srijan Technologies
 
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation SystemSrijan Technologies
 
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and DrupalSrijan Technologies
 
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’Srijan Technologies
 
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...Srijan Technologies
 
[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA TeamSrijan Technologies
 
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with AppiumSrijan Technologies
 
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile CultureSrijan Technologies
 
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX DesignSrijan Technologies
 
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...Srijan Technologies
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails AppSrijan Technologies
 

Mehr von Srijan Technologies (20)

[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
[Srijan Wednesday Webinar] How to Run Stateless and Stateful Services on K8S ...
 
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
[Srijan Wednesday Webinars] How to Set Up a Node.js Microservices Architectur...
 
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
[Srijan Wednesday Webinars] How to Build a Cloud Native Platform for Enterpri...
 
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
[Srijan Wednesday Webinars] Using Drupal as Data Pipeline for Digital Signage
 
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
[Srijan Wednesday Webinars] Let’s Take the Best Route - Exploring Drupal 8 Ro...
 
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
[Srijan Wednesday Webinars] Is Your Business Ready for GDPR
 
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
[Srijan Wednesday Webinars] Artificial Intelligence & the Future of Business
 
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works[Srijan Wednesday Webinars] How to Design a Chatbot that Works
[Srijan Wednesday Webinars] How to Design a Chatbot that Works
 
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
[Srijan Wednesday Webinar] Leveraging the OGD Platform and Visualization Engine
 
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing [Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
[Srijan Wednesday Webinars] Why Adopt Analytics Driven Testing
 
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
[Srijan Wednesday Webinar] Key ingredients of a Powerful Test Automation System
 
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
[Srijan Wednesday Webinar] Building BPMN Web Portals with Camunda and Drupal
 
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
[Srijan Wednesday Webinars] Automating Visual Regression using ‘Galen’
 
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
[Srijan Wednesday Webinars] NASA, Netflix, Tinder: Digital Transformation and...
 
[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team[Srijan Wednesday Webinars] Building a High Performance QA Team
[Srijan Wednesday Webinars] Building a High Performance QA Team
 
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
 
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
[Srijan Wednesday Webinars] Transitioning to an Organization-wide Agile Culture
 
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
[Srijan Wednesday Webinars] Opportunities and Challenges in Enterprise UX Design
 
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
[Srijan Wednesday Webinars] Why Enterprises Should Embrace Distributed Agile ...
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
 

Kürzlich hochgeladen

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Kürzlich hochgeladen (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

Final dependency presentation.odp

  • 1. Demystifying Dependency Injection And Services In Drupal 8 Jyoti Singh | Developer| Srijan Technologies #SrijanWW | @srijan
  • 2. #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
  • 3. #SrijanWW | @srijan Why Dependency Injection
  • 4. How we want our code to look like? #SrijanWW | @srijan
  • 5. How we want our code to look like? #SrijanWW | @srijan
  • 6. How It Actually Looks #SrijanWW | @srijan
  • 9. #SrijanWW | @srijan ● No way to Reuse the code (not OVERRIDE) ● - for eg : Copy the callback entirely and replace it with your code.
  • 10. #SrijanWW | @srijan How can we test something? - Create mock objects? NO. - Want a simple test class? Bootstrap Drupal first.
  • 11. #SrijanWW | @srijan Lot of Cluttering Happens - Excessive use of alter hooks - preprocess for calculations - PHP in template files
  • 12. #SrijanWW | @srijan Let’s take a wrong approach
  • 13. #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.
  • 14. #SrijanWW | @srijan User class uses sessions to store language
  • 15. #SrijanWW | @srijan What Is Dependency Injection?
  • 16. #SrijanWW | @srijan Dependency injection is an advanced software design pattern. It implements “inversion of control”, which means that reusable code calls task specific code.
  • 17. #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
  • 18. 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
  • 20. Defining the Services : services: example.access_checker: class: DrupalexampleAccessCustomAccessCheck arguments: ['@current_user'] tags: - { name: access_check, applies_to: _example_access_check } #SrijanWW | @srijan namespace DrupalexampleAccess; use DrupalCoreRoutingAccessAccessInterface; use DrupalCoreSessionAccountInterface; class CustomAccessCheck implements AccessInterface { /** * A custom access check. * * @param DrupalCoreSessionAccountInterface $account * Run access checks for this account. */ public function access(AccountInterface $account) { return $account->hasPermission('custom_permission’); } }
  • 21. What are services and services container? #SrijanWW | @srijan
  • 22. 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
  • 23. Goal : We want to write a code that is : ● Reusable ● Clutter Free ● Testeable #SrijanWW | @srijan
  • 24. The less your code knows, the more reusable it is! #SrijanWW | @srijan
  • 25. Core Services in Drupal 8 #SrijanWW | @srijan
  • 26. 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
  • 27. Creating Services in Custom Module #SrijanWW | @srijan
  • 28. ● 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
  • 29. 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
  • 30. 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