Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Symfony DIC and Configuration

Slide 3

Slide 3 text

Andreas Hucks @meandmymonkey Senior Developer, Trainer, Consultant SensioLabs Deutschland

Slide 4

Slide 4 text

The Project:

Slide 5

Slide 5 text

Slide 6

Slide 6 text

http://goo.gl/rS06X

Slide 7

Slide 7 text

Life without Dependency Injection

Slide 8

Slide 8 text

class  Logger   {    public  function  log($message,  $level  =  'INFO')    {      $formatter  =  new  XmlFormatter();        $log  =  $formatter-­‐>format(        $message,        $level      );        $this-­‐>writeLog($log);    }   }  

Slide 9

Slide 9 text

Problems?

Slide 10

Slide 10 text

class  Logger   {    function  __construct(FormatterInterface  $formatter)    {      $this-­‐>formatter  =  $formatter;    }      [...]   }  

Slide 11

Slide 11 text

class  Logger   {    [...]      public  function  log($message,  $level  =  'INFO')    {      $log  =  $this-­‐>formatter-­‐>format(        $message,        $level      );        $this-­‐>writeLog($log);    }   }  

Slide 12

Slide 12 text

Alternative: Setter Injection

Slide 13

Slide 13 text

class  Logger   {    [...]      function  setFormatter(FormatterInterface  $formatter)    {      $this-­‐>formatter  =  $formatter;    }      [...]   }  

Slide 14

Slide 14 text

“Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.”

Slide 15

Slide 15 text

Interfacing with the Webservice

Slide 16

Slide 16 text

interface  ExchangeRatesInterface   {          /**            *  @return  array            */          public  function  getRates();            /**            *  @param  string  $currency            *  @return  float            */          public  function  getRate($currency);   }  

Slide 17

Slide 17 text

  interface  AdapterInterface   {          /**            *  @return  string            */          public  function  getRawData();   }  

Slide 18

Slide 18 text

  interface  ParserInterface   {          /**            *  @param  string  $rawData            *  @return  array            */          public  function  parse($rawData);   }  

Slide 19

Slide 19 text

Task Implement class ExchangeRates using MockAdapter and XmlParser. Generate output using ExchangeRatesCommand .

Slide 20

Slide 20 text

The Service Container

Slide 21

Slide 21 text

The Service Container is simply a PHP object that manages the instantiation of services.

Slide 22

Slide 22 text

$container-­‐>get('logger');       $container-­‐>getParameter('logger.class');    

Slide 23

Slide 23 text

Vocabulary Elements

Slide 24

Slide 24 text

service An object managed by the Dependency Injection Container.

Slide 25

Slide 25 text

argument A parameter given by the DIC to a service, via the constructor or a mutator method (setter).

Slide 26

Slide 26 text

parameter A configuration value.

Slide 27

Slide 27 text

                     /path/to/app/logs/app.log                                                    DEBUG                                      

Slide 28

Slide 28 text

Vocabulary Attributes

Slide 29

Slide 29 text

id The service identifier or name

Slide 30

Slide 30 text

class The service class to instantiate

Slide 31

Slide 31 text

alias An alias name for this service

Slide 32

Slide 32 text

public Set to false to make the service private and used internally

Slide 33

Slide 33 text

                     /path/to/app/logs/app.log                                                    DEBUG                                      

Slide 34

Slide 34 text

Vocabulary Service Elements

Slide 35

Slide 35 text

argument An argument to pass to a method (constructor or setter)

Slide 36

Slide 36 text

call A method to call on the newly created object

Slide 37

Slide 37 text

tag A tag to attach to the service definition

Slide 38

Slide 38 text

                     /path/to/app/logs/app.log                                                    DEBUG                                      

Slide 39

Slide 39 text

Vocabulary Argument Attributes

Slide 40

Slide 40 text

type Define the nature of the argument. Allowed values: string, constant, collection or service

Slide 41

Slide 41 text

id The service name, if the argument is a service

Slide 42

Slide 42 text

key The argument key when dealing with the collection type

Slide 43

Slide 43 text

                     /path/to/app/logs/app.log                                                    DEBUG                                      

Slide 44

Slide 44 text

http://goo.gl/FNJ9H

Slide 45

Slide 45 text

Task Write a container configuration!

Slide 46

Slide 46 text

Configuration

Slide 47

Slide 47 text

public  function  load(      array  $configs,      ContainerBuilder  $container   )   {    $configuration  =  new  Configuration();    $config  =  $this      -­‐>processConfiguration($configuration,  $configs);      $loader  =  new  Loader\XmlFileLoader(      $container,      new  FileLocator(__DIR__.'/../Resources/config')    );      $loader-­‐>load('exchangerates.xml');   }  

Slide 48

Slide 48 text

Array  (          [0]  =>  Array  (                  [exchange_rates]  =>  Array  (                          [curl]  =>  Array  (                                  [CURLOPT_TIMEOUT]  =>  5                          )                  )          )          [1]  =>  Array  (                  [exchange_rates]  =>  Array  (                          [curl]  =>  Array  (                                  [CURLOPT_TIMEOUT]  =>  10,                                  [CURLOPT_PROXY]  =>  http:// localhost:8888                          )                  )          )   )  

Slide 49

Slide 49 text

Array  (          [exchange_rates]  =>  Array  (                  [curl]  =>  Array  (                          [CURLOPT_TIMEOUT]  =>  10                          [CURLOPT_PROXY]  =>  http://localhost:8888                  )                  [endpoint]  =>  http://www.ecb.europa.eu/stats...          )   )  

Slide 50

Slide 50 text

class  Configuration  implements  ConfigurationInterface   {    public  function  getConfigTreeBuilder()    {      $treeBuilder  =  new  TreeBuilder();      $rootNode  =  $treeBuilder>root('acme_dic_workshop');        $rootNode        -­‐>children()          -­‐>scalarNode('some_config_var')            -­‐>isRequired()            -­‐>defaultValue('foo')            -­‐>end()          -­‐>end();        return  $treeBuilder;    }   }  

Slide 51

Slide 51 text

-­‐>scalarNode(‘some_url')    -­‐>isRequired()    -­‐>cannotBeEmpty()    -­‐>validate()      -­‐>ifTrue(function($value)  {        return  !filter_var($value,  \FILTER_VALIDATE_URL);    })      -­‐>thenInvalid('Not  a  valid  url.')    -­‐>end()   -­‐>end()  

Slide 52

Slide 52 text

http://goo.gl/8sa6Q

Slide 53

Slide 53 text

Thanks! https://joind.in/7778