More Related Content Similar to Symfony2 Service Container: Inject me, my friend Similar to Symfony2 Service Container: Inject me, my friend (20) Symfony2 Service Container: Inject me, my friend3. Don't call us, we'll call you Service Container. Theory Kirill chEbba Chebunin 9. Service Definition Service Container. Implementation Kirill chEbba Chebunin <service id= "monolog.logger.event" class= "%logger.class%" > <argument>event</argument> <call method= "pushHandler" > <argument type= "service" id= "monolog.handler.main" /> </call> <call method= "pushHandler" > <argument type= "service" id= "monolog.handler.firephp" /> </call> <call method= "pushHandler" > <argument type= "service" id= "monolog.handler.debug" /> </call> </service> 10. Constructor Injection Service Container. Implementation Kirill chEbba Chebunin class IdentityTranslator implements TranslatorInterface { public function __construct ( MessageSelector $selector ) { /**/ } } <service id= "translator" class= "SymfonyomponentranslationdentityTranslator" > <argument type= "service" id= "translator.selector" /> </service> 11. Setter Injection Service Container. Implementation Kirill chEbba Chebunin class Configuration { public function setSQLLogger ( SQLLogger $logger = null ) { /**/ } } <service class= "DoctrineBALonfiguration" > <call method= "setSQLLogger" > <argument type= "service" id= "doctrine.dbal.logger" /> </call> </service> 12. Dynamic Service Locator Service Container. Implementation Kirill chEbba Chebunin public function some () { $this -> container -> get ( 'logger' ); } 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' ); 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 ); /* … */ } 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 ); } 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 ; } 35. Bundle Extension Service Container. Bundles Kirill chEbba Chebunin SecurityBundle/Resources/config/templating_php.xml <services> <service id= "templating.helper.security" class= "%templating.helper.security.class%" > <tag name= "templating.helper" alias= "security" /> <argument type= "service" id= "security.context" on-invalid= "ignore" /> </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' ); 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 ))); }