43
Kirill chEbba Chebunin [email protected] Symfony Service Container. Inject me, my friend.

Symfony2 Service Container: Inject me, my friend

Embed Size (px)

Citation preview

Page 1: Symfony2 Service Container: Inject me, my friend

Kirill chEbba [email protected]

Symfony Service Container.Inject me, my friend.

Page 2: Symfony2 Service Container: Inject me, my friend

Theory

Service Container. Theory

Kirill chEbba Chebunin

•Inversion of Control•Dependency Injection•IoC(DI) Container

Page 3: Symfony2 Service Container: Inject me, my friend

Don't call us, we'll call you

Service Container. Theory

Kirill chEbba Chebunin

Page 4: Symfony2 Service Container: Inject me, my friend

IoC Implementation

Service Container. Theory

Kirill chEbba Chebunin

•Interfaces•Abstract Factory•Service Locator

Page 5: Symfony2 Service Container: Inject me, my friend

Dependency Injection

Service Container. Theory

Kirill chEbba Chebunin

•Constructor Injection•Setter Injection•Interface Injection

Page 6: Symfony2 Service Container: Inject me, my friend

IoC Container

Service Container. Theory

Kirill chEbba Chebunin

•Service Definitions•Factory•Service Locator

Page 7: Symfony2 Service Container: Inject me, my friend

IoC Container

Service Container. Theory

Kirill chEbba Chebunin

Page 8: Symfony2 Service Container: Inject me, my friend

Symfony2 Service Container=

IoC Container

Service Container. Implementation

Kirill chEbba Chebunin

Page 9: Symfony2 Service Container: Inject me, my friend

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>

Page 10: Symfony2 Service Container: Inject me, my friend

Constructor Injection

Service Container. Implementation

Kirill chEbba Chebunin

class IdentityTranslator implements TranslatorInterface{ public function __construct(MessageSelector $selector) {/**/}}

<service id="translator" class="Symfony\Component\Translation\IdentityTranslator"> <argument type="service" id="translator.selector"/></service>

Page 11: Symfony2 Service Container: Inject me, my friend

Setter Injection

Service Container. Implementation

Kirill chEbba Chebunin

class Configuration{ public function setSQLLogger(SQLLogger $logger = null) {/**/}}

<service class="Doctrine\DBAL\Configuration"> <call method="setSQLLogger"> <argument type="service" id="doctrine.dbal.logger"/> </call></service>

Page 12: Symfony2 Service Container: Inject me, my friend

Dynamic Service Locator

Service Container. Implementation

Kirill chEbba Chebunin

public function some(){ $this->container->get('logger');}

Page 13: Symfony2 Service Container: Inject me, my friend

Additional capabilities

Service Container. Implementation

Kirill chEbba Chebunin

•Parameters•Imports•Private & Abstract Services•Aliases•Tags•…

Page 14: Symfony2 Service Container: Inject me, my friend

Scopes

Service Container. Implementation

Kirill chEbba Chebunin

•Container•Prototype•Request•Custom

Page 15: Symfony2 Service Container: Inject me, my friend

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');

Page 16: Symfony2 Service Container: Inject me, my friend

Advanced Features

Service Container. Advanced

Kirill chEbba Chebunin

Page 17: Symfony2 Service Container: Inject me, my friend

Extensions

Service Container. Advanced

Kirill chEbba Chebunin

Page 18: Symfony2 Service Container: Inject me, my friend

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

Page 19: Symfony2 Service Container: Inject me, my friend

Extensions

Service Container. Advanced

Kirill chEbba Chebunin

interface ExtensionInterface{ /* … */ function load(array $config, ContainerBuilder $container); /* … */}

Page 20: Symfony2 Service Container: Inject me, my friend

Container Compiler

Service Container. Advanced

Kirill chEbba Chebunin

•Compiler•PassConfig•Freeze

Page 21: Symfony2 Service Container: Inject me, my friend

Compiler Passes

Service Container. Advanced

Kirill chEbba Chebunin

Page 22: Symfony2 Service Container: Inject me, my friend

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);}

Page 23: Symfony2 Service Container: Inject me, my friend

Compiler Passes

Service Container. Advanced

Kirill chEbba Chebunin

•Merge•BeforeOptimization•Optimization•BeforeRemoving•Removing•AfterRemoving

Page 24: Symfony2 Service Container: Inject me, my friend

Optimization Passes

Service Container. Advanced

Kirill chEbba Chebunin

•ResolveParameterPlaceHolders•ResolveReferencesToAliases•CheckCircularReferences•...

Page 25: Symfony2 Service Container: Inject me, my friend

Removing Passes

Service Container. Advanced

Kirill chEbba Chebunin

•RemovePrivateAliases•RemoveAbstractDefinitions•ReplaceAliasByActualDefinition•...

Page 26: Symfony2 Service Container: Inject me, my friend

Compiler Pass Hooks

Service Container. Advanced

Kirill chEbba Chebunin

•BeforeOptimization•BeforeRemoving•AfterRemoving

Page 27: Symfony2 Service Container: Inject me, my friend

MergePassAdd

Extension Services

Service Container. Advanced

Kirill chEbba Chebunin

Page 28: Symfony2 Service Container: Inject me, my friend

Frozen Container

Service Container. Advanced

Kirill chEbba Chebunin

Page 29: Symfony2 Service Container: Inject me, my friend

Container Dumper

Service Container. Advanced

Kirill chEbba Chebunin

Page 30: Symfony2 Service Container: Inject me, my friend

Container Dumper

Service Container. Advanced

Kirill chEbba Chebunin

•XmlDumper•YamlDumper•PhpDumper

Page 31: Symfony2 Service Container: Inject me, my friend

appDevDebugProjectContainer

Service Container. Advanced

Kirill chEbba Chebunin

protected function getLoggerService(){ $this->services['logger'] = $instance = new \Symfony\Bridge\Monolog\Logger('app');

$instance->pushHandler($this->get('monolog.handler.main')); $instance->pushHandler($this->get('monolog.handler.firephp')); $instance->pushHandler($this->get('monolog.handler.debug'));

return $instance;}

Page 32: Symfony2 Service Container: Inject me, my friend

Container & Bundles

Service Container. Bundles

Kirill chEbba Chebunin

Page 33: Symfony2 Service Container: Inject me, my friend

Container & Bundles

Service Container. Bundles

Kirill chEbba Chebunin

•Extension•Compiler Passes•Bundle Boot

Page 34: Symfony2 Service Container: Inject me, my friend

Bundle Extension

Service Container. Bundles

Kirill chEbba Chebunin

Page 35: Symfony2 Service Container: Inject me, my friend

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>

Page 36: Symfony2 Service Container: Inject me, my friend

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');

Page 37: Symfony2 Service Container: Inject me, my friend

Bundle Compiler Passes

Service Container. Bundles

Kirill chEbba Chebunin

Page 38: Symfony2 Service Container: Inject me, my friend

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());}

Page 39: Symfony2 Service Container: Inject me, my friend

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)));}

Page 40: Symfony2 Service Container: Inject me, my friend

Bundle Boot

Service Container. Bundles

Kirill chEbba Chebunin

Page 41: Symfony2 Service Container: Inject me, my friend

It is Your Choice

Service Container. Afterword

Kirill chEbba Chebunin

•Constructor VS Setter injection•DI VS Service Locator•Extension VS Compiler Pass

Page 42: Symfony2 Service Container: Inject me, my friend

The End

Service Container. Afterword

Kirill chEbba Chebunin

Page 43: Symfony2 Service Container: Inject me, my friend

Time for Questions

Service Container. Afterword

Kirill chEbba Chebunin

[email protected]•http://github.com/chEbba•@iamchEbba