SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Kirill chEbba Chebunin [email_address] Symfony Service Container. Inject me, my friend.
Theory Service Container. Theory Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
Don't call us, we'll call you Service Container. Theory Kirill chEbba Chebunin
IoC Implementation Service Container. Theory Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
Dependency Injection Service Container. Theory Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
IoC Container Service Container. Theory Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
IoC Container Service Container. Theory Kirill chEbba Chebunin
Symfony2 Service Container = IoC Container Service Container. Implementation Kirill chEbba Chebunin
Service Definition Service Container. Implementation Kirill chEbba Chebunin <service  id= &quot;monolog.logger.event&quot;   class= &quot;%logger.class%&quot; > <argument>event</argument> <call  method= &quot;pushHandler&quot; > <argument  type= &quot;service&quot;   id= &quot;monolog.handler.main&quot; /> </call> <call  method= &quot;pushHandler&quot; > <argument  type= &quot;service&quot;   id= &quot;monolog.handler.firephp&quot; /> </call> <call  method= &quot;pushHandler&quot; > <argument  type= &quot;service&quot;   id= &quot;monolog.handler.debug&quot; /> </call> </service>
Constructor Injection Service Container. Implementation Kirill chEbba Chebunin class IdentityTranslator implements TranslatorInterface { public function __construct ( MessageSelector  $selector )   { /**/ } } <service  id= &quot;translator&quot;   class= &quot;SymfonyomponentranslationdentityTranslator&quot; > <argument  type= &quot;service&quot;   id= &quot;translator.selector&quot; /> </service>
Setter Injection Service Container. Implementation Kirill chEbba Chebunin class Configuration { public function setSQLLogger ( SQLLogger  $logger   =  null ) { /**/ } } <service  class= &quot;DoctrineBALonfiguration&quot; > <call  method= &quot;setSQLLogger&quot; > <argument  type= &quot;service&quot;   id= &quot;doctrine.dbal.logger&quot; /> </call> </service>
Dynamic Service Locator Service Container. Implementation Kirill chEbba Chebunin public function some () { $this -> container -> get ( 'logger' ); }
Additional capabilities Service Container. Implementation Kirill chEbba Chebunin ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Scopes Service Container. Implementation Kirill chEbba Chebunin ,[object Object],[object Object],[object Object],[object Object]
Custom scope Service Container. Implementation Kirill chEbba Chebunin $this -> container -> enterScope ( 'request' ); $this -> container -> set ( 'request' ,   $request ,  'request' ); try  { $response   =  parent :: handle ( $request ,   $type ,   $catch ); }  catch  ( xception  $e )   { $this -> container -> leaveScope ( 'request' ); throw  $e ; } $this -> container -> leaveScope ( 'request' );
Advanced Features Service Container. Advanced Kirill chEbba Chebunin
Extensions Service Container. Advanced Kirill chEbba Chebunin
Extensions Service Container. Advanced Kirill chEbba Chebunin doctrine: dbal:      driver:   % database_driver %      host:   % database_host %      port:   % database_port %      dbname:   % database_name %      user:   % database_user %      password:   % database_password %      charset:   UTF8
Extensions Service Container. Advanced Kirill chEbba Chebunin interface ExtensionInterface { /* … */ function load ( array   $config ,  ContainerBuilder  $container ); /* … */ }
Container Compiler Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
Compiler Passes Service Container. Advanced Kirill chEbba Chebunin
Compiler Pass Service Container. Advanced Kirill chEbba Chebunin interface CompilerPassInterface { /** * You can modify the container here before  * it is dumped to PHP code. * * @param ContainerBuilder $container */ function process ( ContainerBuilder  $container ); }
Compiler Passes Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Optimization Passes Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object],[object Object]
Removing Passes Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object],[object Object]
Compiler Pass Hooks Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
MergePass Add Extension Services Service Container. Advanced Kirill chEbba Chebunin
Frozen Container Service Container. Advanced Kirill chEbba Chebunin
Container Dumper Service Container. Advanced Kirill chEbba Chebunin
Container Dumper Service Container. Advanced Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
appDevDebugProjectContainer Service Container. Advanced Kirill chEbba Chebunin protected function getLoggerService () { $this -> services [ 'logger' ]   =   $instance   =   new  ymfonyridgeonologogger ( 'app' ); $instance -> pushHandler ( $this -> get ( 'monolog.handler.main' )); $instance -> pushHandler ( $this -> get ( 'monolog.handler.firephp' )); $instance -> pushHandler ( $this -> get ( 'monolog.handler.debug' )); return   $instance ; }
Container & Bundles Service Container. Bundles Kirill chEbba Chebunin
Container & Bundles Service Container. Bundles Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
Bundle Extension Service Container. Bundles Kirill chEbba Chebunin
Bundle Extension Service Container. Bundles Kirill chEbba Chebunin SecurityBundle/Resources/config/templating_php.xml <services> <service  id= &quot;templating.helper.security&quot; class= &quot;%templating.helper.security.class%&quot; > <tag  name= &quot;templating.helper&quot;   alias= &quot;security&quot;  /> <argument  type= &quot;service&quot;   id= &quot;security.context&quot; on-invalid= &quot;ignore&quot;  /> </service> </services>
Bundle Extension Service Container. Bundles Kirill chEbba Chebunin // load services $loader   =  new XmlFileLoader ( $container ,   new FileLocator ( __DIR__. '/../Resources/config' ) ); $loader -> load ( 'security.xml' ); $loader -> load ( 'security_listeners.xml' ); $loader -> load ( 'security_rememberme.xml' ); $loader -> load ( 'templating_php.xml' ); $loader -> load ( 'templating_twig.xml' ); $loader -> load ( 'collectors.xml' );
Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin
Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin TwigBundle public function build ( ContainerBuilder  $container ) { parent :: build ( $container ); $container -> addCompilerPass ( new TwigEnvironmentPass ()); $container -> addCompilerPass ( new ExceptionListenerPass ()); }
Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin TwigEnvironmentPass $definition   =   $container -> getDefinition ( 'twig' ); foreach   ( $container -> findTaggedServiceIds ( 'twig.extension' )   as   $id   =>   $attributes )   { $definition -> addMethodCall ( 'addExtension' ,   array ( new Reference ( $id ))); }
Bundle Boot Service Container. Bundles Kirill chEbba Chebunin
It is Your Choice Service Container. Afterword Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]
The End Service Container. Afterword Kirill chEbba Chebunin
Time for Questions Service Container. Afterword Kirill chEbba Chebunin ,[object Object],[object Object],[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)James Titcumb
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015Fernando Hamasaki de Amorim
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016Kacper Gunia
 
Symfony without the framework
Symfony without the frameworkSymfony without the framework
Symfony without the frameworkGOG.com dev team
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2Kacper Gunia
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersKacper Gunia
 
Information security programming in ruby
Information security programming in rubyInformation security programming in ruby
Information security programming in rubyHiroshi Nakamura
 
Apigility reloaded
Apigility reloadedApigility reloaded
Apigility reloadedRalf Eggert
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...King Foo
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalKent Ohashi
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Shinya Ohyanagi
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojobpmedley
 
Let's play a game with blackfire player
Let's play a game with blackfire playerLet's play a game with blackfire player
Let's play a game with blackfire playerMarcin Czarnecki
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony TechniquesKris Wallsmith
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Kacper Gunia
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needKacper Gunia
 

Was ist angesagt? (20)

Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
Symfony without the framework
Symfony without the frameworkSymfony without the framework
Symfony without the framework
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
 
Information security programming in ruby
Information security programming in rubyInformation security programming in ruby
Information security programming in ruby
 
Beyond Phoenix
Beyond PhoenixBeyond Phoenix
Beyond Phoenix
 
Apigility reloaded
Apigility reloadedApigility reloaded
Apigility reloaded
 
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
 
Interceptors: Into the Core of Pedestal
Interceptors: Into the Core of PedestalInterceptors: Into the Core of Pedestal
Interceptors: Into the Core of Pedestal
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Let's play a game with blackfire player
Let's play a game with blackfire playerLet's play a game with blackfire player
Let's play a game with blackfire player
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
 

Andere mochten auch

Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Lets play with Symfony2
Lets play with Symfony2Lets play with Symfony2
Lets play with Symfony2Noel GUILBERT
 
Dependency Injection Smells
Dependency Injection SmellsDependency Injection Smells
Dependency Injection SmellsMatthias Noback
 
Dependency injection with Symfony 2
Dependency injection with Symfony 2Dependency injection with Symfony 2
Dependency injection with Symfony 2cammanderson
 
In The Future We All Use Symfony2
In The Future We All Use Symfony2In The Future We All Use Symfony2
In The Future We All Use Symfony2Brent Shaffer
 
The use of Symfony2 @ Overblog
The use of Symfony2 @ OverblogThe use of Symfony2 @ Overblog
The use of Symfony2 @ OverblogXavier Hausherr
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction PresentationNerd Tzanetopoulos
 
Presentation du framework symfony
Presentation du framework symfonyPresentation du framework symfony
Presentation du framework symfonyJeremy Gachet
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Fabien Potencier
 
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2Abdelkader Rhouati
 
Symfony 2 : chapitre 4 - Les services et les formulaires
Symfony 2 : chapitre 4 - Les services et les formulairesSymfony 2 : chapitre 4 - Les services et les formulaires
Symfony 2 : chapitre 4 - Les services et les formulairesAbdelkader Rhouati
 
Symfony 2 : chapitre 1 - Présentation Générale
Symfony 2 : chapitre 1 - Présentation GénéraleSymfony 2 : chapitre 1 - Présentation Générale
Symfony 2 : chapitre 1 - Présentation GénéraleAbdelkader Rhouati
 

Andere mochten auch (12)

Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Lets play with Symfony2
Lets play with Symfony2Lets play with Symfony2
Lets play with Symfony2
 
Dependency Injection Smells
Dependency Injection SmellsDependency Injection Smells
Dependency Injection Smells
 
Dependency injection with Symfony 2
Dependency injection with Symfony 2Dependency injection with Symfony 2
Dependency injection with Symfony 2
 
In The Future We All Use Symfony2
In The Future We All Use Symfony2In The Future We All Use Symfony2
In The Future We All Use Symfony2
 
The use of Symfony2 @ Overblog
The use of Symfony2 @ OverblogThe use of Symfony2 @ Overblog
The use of Symfony2 @ Overblog
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction Presentation
 
Presentation du framework symfony
Presentation du framework symfonyPresentation du framework symfony
Presentation du framework symfony
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
Symfony 2 : chapitre 3 - Les modèles en Doctrine 2
 
Symfony 2 : chapitre 4 - Les services et les formulaires
Symfony 2 : chapitre 4 - Les services et les formulairesSymfony 2 : chapitre 4 - Les services et les formulaires
Symfony 2 : chapitre 4 - Les services et les formulaires
 
Symfony 2 : chapitre 1 - Présentation Générale
Symfony 2 : chapitre 1 - Présentation GénéraleSymfony 2 : chapitre 1 - Présentation Générale
Symfony 2 : chapitre 1 - Présentation Générale
 

Ähnlich wie Symfony2 Service Container: Inject me, my friend

Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsMarian Marinov
 
JUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleJUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleGeoffrey De Smet
 
Lightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClientLightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClientAdam Wiggins
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부Hyun-Mook Choi
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Build Lightweight Web Module
Build Lightweight Web ModuleBuild Lightweight Web Module
Build Lightweight Web ModuleMorgan Cheng
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascriptmpnkhan
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
JSF Component Behaviors
JSF Component BehaviorsJSF Component Behaviors
JSF Component BehaviorsAndy Schwartz
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Phpfunkatron
 
Scale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App FabricScale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App FabricChris Dufour
 
Structure your Play application with the cake pattern (and test it)
Structure your Play application with the cake pattern (and test it)Structure your Play application with the cake pattern (and test it)
Structure your Play application with the cake pattern (and test it)yann_s
 
Performance measurement and tuning
Performance measurement and tuningPerformance measurement and tuning
Performance measurement and tuningAOE
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDiego Lewin
 

Ähnlich wie Symfony2 Service Container: Inject me, my friend (20)

Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your plugins
 
JUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by exampleJUDCon London 2011 - Bin packing with drools planner by example
JUDCon London 2011 - Bin packing with drools planner by example
 
Lightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClientLightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClient
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부Fargate 를 이용한 ECS with VPC 1부
Fargate 를 이용한 ECS with VPC 1부
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Build Lightweight Web Module
Build Lightweight Web ModuleBuild Lightweight Web Module
Build Lightweight Web Module
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
JSF Component Behaviors
JSF Component BehaviorsJSF Component Behaviors
JSF Component Behaviors
 
Intro To Mvc Development In Php
Intro To Mvc Development In PhpIntro To Mvc Development In Php
Intro To Mvc Development In Php
 
Scale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App FabricScale Your Data Tier With Windows Server App Fabric
Scale Your Data Tier With Windows Server App Fabric
 
Structure your Play application with the cake pattern (and test it)
Structure your Play application with the cake pattern (and test it)Structure your Play application with the cake pattern (and test it)
Structure your Play application with the cake pattern (and test it)
 
WCF - In a Week
WCF - In a WeekWCF - In a Week
WCF - In a Week
 
Performance measurement and tuning
Performance measurement and tuningPerformance measurement and tuning
Performance measurement and tuning
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony Container
 

Symfony2 Service Container: Inject me, my friend

  • 1. Kirill chEbba Chebunin [email_address] Symfony Service Container. Inject me, my friend.
  • 2.
  • 3. Don't call us, we'll call you Service Container. Theory Kirill chEbba Chebunin
  • 4.
  • 5.
  • 6.
  • 7. IoC Container Service Container. Theory Kirill chEbba Chebunin
  • 8. Symfony2 Service Container = IoC Container Service Container. Implementation Kirill chEbba Chebunin
  • 9. Service Definition Service Container. Implementation Kirill chEbba Chebunin <service id= &quot;monolog.logger.event&quot; class= &quot;%logger.class%&quot; > <argument>event</argument> <call method= &quot;pushHandler&quot; > <argument type= &quot;service&quot; id= &quot;monolog.handler.main&quot; /> </call> <call method= &quot;pushHandler&quot; > <argument type= &quot;service&quot; id= &quot;monolog.handler.firephp&quot; /> </call> <call method= &quot;pushHandler&quot; > <argument type= &quot;service&quot; id= &quot;monolog.handler.debug&quot; /> </call> </service>
  • 10. Constructor Injection Service Container. Implementation Kirill chEbba Chebunin class IdentityTranslator implements TranslatorInterface { public function __construct ( MessageSelector $selector ) { /**/ } } <service id= &quot;translator&quot; class= &quot;SymfonyomponentranslationdentityTranslator&quot; > <argument type= &quot;service&quot; id= &quot;translator.selector&quot; /> </service>
  • 11. Setter Injection Service Container. Implementation Kirill chEbba Chebunin class Configuration { public function setSQLLogger ( SQLLogger $logger = null ) { /**/ } } <service class= &quot;DoctrineBALonfiguration&quot; > <call method= &quot;setSQLLogger&quot; > <argument type= &quot;service&quot; id= &quot;doctrine.dbal.logger&quot; /> </call> </service>
  • 12. Dynamic Service Locator Service Container. Implementation Kirill chEbba Chebunin public function some () { $this -> container -> get ( 'logger' ); }
  • 13.
  • 14.
  • 15. Custom scope Service Container. Implementation Kirill chEbba Chebunin $this -> container -> enterScope ( 'request' ); $this -> container -> set ( 'request' , $request , 'request' ); try { $response = parent :: handle ( $request , $type , $catch ); } catch ( xception $e ) { $this -> container -> leaveScope ( 'request' ); throw $e ; } $this -> container -> leaveScope ( 'request' );
  • 16. Advanced Features Service Container. Advanced Kirill chEbba Chebunin
  • 17. Extensions Service Container. Advanced Kirill chEbba Chebunin
  • 18. Extensions Service Container. Advanced Kirill chEbba Chebunin doctrine: dbal:     driver: % database_driver %     host: % database_host %     port: % database_port %     dbname: % database_name %     user: % database_user %     password: % database_password %     charset: UTF8
  • 19. Extensions Service Container. Advanced Kirill chEbba Chebunin interface ExtensionInterface { /* … */ function load ( array $config , ContainerBuilder $container ); /* … */ }
  • 20.
  • 21. Compiler Passes Service Container. Advanced Kirill chEbba Chebunin
  • 22. Compiler Pass Service Container. Advanced Kirill chEbba Chebunin interface CompilerPassInterface { /** * You can modify the container here before * it is dumped to PHP code. * * @param ContainerBuilder $container */ function process ( ContainerBuilder $container ); }
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. MergePass Add Extension Services Service Container. Advanced Kirill chEbba Chebunin
  • 28. Frozen Container Service Container. Advanced Kirill chEbba Chebunin
  • 29. Container Dumper Service Container. Advanced Kirill chEbba Chebunin
  • 30.
  • 31. appDevDebugProjectContainer Service Container. Advanced Kirill chEbba Chebunin protected function getLoggerService () { $this -> services [ 'logger' ] = $instance = new ymfonyridgeonologogger ( 'app' ); $instance -> pushHandler ( $this -> get ( 'monolog.handler.main' )); $instance -> pushHandler ( $this -> get ( 'monolog.handler.firephp' )); $instance -> pushHandler ( $this -> get ( 'monolog.handler.debug' )); return $instance ; }
  • 32. Container & Bundles Service Container. Bundles Kirill chEbba Chebunin
  • 33.
  • 34. Bundle Extension Service Container. Bundles Kirill chEbba Chebunin
  • 35. Bundle Extension Service Container. Bundles Kirill chEbba Chebunin SecurityBundle/Resources/config/templating_php.xml <services> <service id= &quot;templating.helper.security&quot; class= &quot;%templating.helper.security.class%&quot; > <tag name= &quot;templating.helper&quot; alias= &quot;security&quot; /> <argument type= &quot;service&quot; id= &quot;security.context&quot; on-invalid= &quot;ignore&quot; /> </service> </services>
  • 36. Bundle Extension Service Container. Bundles Kirill chEbba Chebunin // load services $loader = new XmlFileLoader ( $container , new FileLocator ( __DIR__. '/../Resources/config' ) ); $loader -> load ( 'security.xml' ); $loader -> load ( 'security_listeners.xml' ); $loader -> load ( 'security_rememberme.xml' ); $loader -> load ( 'templating_php.xml' ); $loader -> load ( 'templating_twig.xml' ); $loader -> load ( 'collectors.xml' );
  • 37. Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin
  • 38. Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin TwigBundle public function build ( ContainerBuilder $container ) { parent :: build ( $container ); $container -> addCompilerPass ( new TwigEnvironmentPass ()); $container -> addCompilerPass ( new ExceptionListenerPass ()); }
  • 39. Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin TwigEnvironmentPass $definition = $container -> getDefinition ( 'twig' ); foreach ( $container -> findTaggedServiceIds ( 'twig.extension' ) as $id => $attributes ) { $definition -> addMethodCall ( 'addExtension' , array ( new Reference ( $id ))); }
  • 40. Bundle Boot Service Container. Bundles Kirill chEbba Chebunin
  • 41.
  • 42. The End Service Container. Afterword Kirill chEbba Chebunin
  • 43.