Symfony2 Service Container: Inject me, my friend

12,233 views

Published on

0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
12,233
On SlideShare
0
From Embeds
0
Number of Embeds
66
Actions
Shares
0
Downloads
71
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Symfony2 Service Container: Inject me, my friend

  1. 1. Kirill chEbba Chebunin [email_address] Symfony Service Container. Inject me, my friend.
  2. 2. Theory Service Container. Theory Kirill chEbba Chebunin <ul><li>Inversion of Control </li></ul><ul><li>Dependency Injection </li></ul><ul><li>IoC(DI) Container </li></ul>
  3. 3. Don't call us, we'll call you Service Container. Theory Kirill chEbba Chebunin
  4. 4. IoC Implementation Service Container. Theory Kirill chEbba Chebunin <ul><li>Interfaces </li></ul><ul><li>Abstract Factory </li></ul><ul><li>Service Locator </li></ul>
  5. 5. Dependency Injection Service Container. Theory Kirill chEbba Chebunin <ul><li>Constructor Injection </li></ul><ul><li>Setter Injection </li></ul><ul><li>Interface Injection </li></ul>
  6. 6. IoC Container Service Container. Theory Kirill chEbba Chebunin <ul><li>Service Definitions </li></ul><ul><li>Factory </li></ul><ul><li>Service Locator </li></ul>
  7. 7. IoC Container Service Container. Theory Kirill chEbba Chebunin
  8. 8. Symfony2 Service Container = IoC Container Service Container. Implementation Kirill chEbba Chebunin
  9. 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. 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;SymfonyComponentTranslationIdentityTranslator&quot; > <argument type= &quot;service&quot; id= &quot;translator.selector&quot; /> </service>
  11. 11. Setter Injection Service Container. Implementation Kirill chEbba Chebunin class Configuration { public function setSQLLogger ( SQLLogger $logger = null ) { /**/ } } <service class= &quot;DoctrineDBALConfiguration&quot; > <call method= &quot;setSQLLogger&quot; > <argument type= &quot;service&quot; id= &quot;doctrine.dbal.logger&quot; /> </call> </service>
  12. 12. Dynamic Service Locator Service Container. Implementation Kirill chEbba Chebunin public function some () { $this -> container -> get ( 'logger' ); }
  13. 13. Additional capabilities Service Container. Implementation Kirill chEbba Chebunin <ul><li>Parameters </li></ul><ul><li>Imports </li></ul><ul><li>Private & Abstract Services </li></ul><ul><li>Aliases </li></ul><ul><li>Tags </li></ul><ul><li>… </li></ul>
  14. 14. Scopes Service Container. Implementation Kirill chEbba Chebunin <ul><li>Container </li></ul><ul><li>Prototype </li></ul><ul><li>Request </li></ul><ul><li>Custom </li></ul>
  15. 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 ( Exception $e ) { $this -> container -> leaveScope ( 'request' ); throw $e ; } $this -> container -> leaveScope ( 'request' );
  16. 16. Advanced Features Service Container. Advanced Kirill chEbba Chebunin
  17. 17. Extensions Service Container. Advanced Kirill chEbba Chebunin
  18. 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. 19. Extensions Service Container. Advanced Kirill chEbba Chebunin interface ExtensionInterface { /* … */ function load ( array $config , ContainerBuilder $container ); /* … */ }
  20. 20. Container Compiler Service Container. Advanced Kirill chEbba Chebunin <ul><li>Compiler </li></ul><ul><li>PassConfig </li></ul><ul><li>Freeze </li></ul>
  21. 21. Compiler Passes Service Container. Advanced Kirill chEbba Chebunin
  22. 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. 23. Compiler Passes Service Container. Advanced Kirill chEbba Chebunin <ul><li>Merge </li></ul><ul><li>BeforeOptimization </li></ul><ul><li>Optimization </li></ul><ul><li>BeforeRemoving </li></ul><ul><li>Removing </li></ul><ul><li>AfterRemoving </li></ul>
  24. 24. Optimization Passes Service Container. Advanced Kirill chEbba Chebunin <ul><li>ResolveParameterPlaceHolders </li></ul><ul><li>ResolveReferencesToAliases </li></ul><ul><li>CheckCircularReferences </li></ul><ul><li>... </li></ul>
  25. 25. Removing Passes Service Container. Advanced Kirill chEbba Chebunin <ul><li>RemovePrivateAliases </li></ul><ul><li>RemoveAbstractDefinitions </li></ul><ul><li>ReplaceAliasByActualDefinition </li></ul><ul><li>... </li></ul>
  26. 26. Compiler Pass Hooks Service Container. Advanced Kirill chEbba Chebunin <ul><li>BeforeOptimization </li></ul><ul><li>BeforeRemoving </li></ul><ul><li>AfterRemoving </li></ul>
  27. 27. MergePass Add Extension Services Service Container. Advanced Kirill chEbba Chebunin
  28. 28. Frozen Container Service Container. Advanced Kirill chEbba Chebunin
  29. 29. Container Dumper Service Container. Advanced Kirill chEbba Chebunin
  30. 30. Container Dumper Service Container. Advanced Kirill chEbba Chebunin <ul><li>XmlDumper </li></ul><ul><li>YamlDumper </li></ul><ul><li>PhpDumper </li></ul>
  31. 31. appDevDebugProjectContainer Service Container. Advanced Kirill chEbba Chebunin protected function getLoggerService () { $this -> services [ 'logger' ] = $instance = new SymfonyBridgeMonologLogger ( '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. 32. Container & Bundles Service Container. Bundles Kirill chEbba Chebunin
  33. 33. Container & Bundles Service Container. Bundles Kirill chEbba Chebunin <ul><li>Extension </li></ul><ul><li>Compiler Passes </li></ul><ul><li>Bundle Boot </li></ul>
  34. 34. Bundle Extension Service Container. Bundles Kirill chEbba Chebunin
  35. 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. 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. 37. Bundle Compiler Passes Service Container. Bundles Kirill chEbba Chebunin
  38. 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. 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. 40. Bundle Boot Service Container. Bundles Kirill chEbba Chebunin
  41. 41. It is Your Choice Service Container. Afterword Kirill chEbba Chebunin <ul><li>Constructor VS Setter injection </li></ul><ul><li>DI VS Service Locator </li></ul><ul><li>Extension VS Compiler Pass </li></ul>
  42. 42. The End Service Container. Afterword Kirill chEbba Chebunin
  43. 43. Time for Questions Service Container. Afterword Kirill chEbba Chebunin <ul><li>[email_address] </li></ul><ul><li>http://github.com/chEbba </li></ul><ul><li>@iamchEbba </li></ul>

×