Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inside the Symfony DIC and Config

Inside the Symfony DIC and Config

Slides for my Symfony workshop at PHPBenelux 2013

Andreas Hucks

January 25, 2013

Transcript

53 slides
  1. €

  2. class  Logger   {    public  function  log($message,  $level  =

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

         $this-­‐>formatter  =  $formatter;    }      [...]   }  
  4. class  Logger   {    [...]      public  function

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

     $formatter)    {      $this-­‐>formatter  =  $formatter;    }      [...]   }  
  6. “Dependency Injection is where components are given their dependencies through

    their constructors, methods, or directly into fields.”
  7. interface  ExchangeRatesInterface   {          /**  

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

               *  @return  string            */          public  function  getRawData();   }  
  9.   interface  ParserInterface   {          /**

               *  @param  string  $rawData            *  @return  array            */          public  function  parse($rawData);   }  
  10. argument A parameter given by the DIC to a service,

    via the constructor or a mutator method (setter).
  11. <service  id="sensio.logger"  class="Sensio\Logger"  >            <!-­‐-­‐

     Constructor  arguments  -­‐-­‐>          <argument>/path/to/app/logs/app.log</argument>          <argument  type="service"                                               id="sensio.logger.xml_formatter"  />              <!-­‐-­‐  Methods  to  call  -­‐-­‐>          <call  method="setDefaultSeverity">                  <argument>DEBUG</argument>          </call>      <!-­‐-­‐  Tags  -­‐-­‐>            <tag  name="tools.logger"  />         </service>  
  12. <service  id="sensio.logger"  class="Sensio\Logger"  >            <!-­‐-­‐

     Constructor  arguments  -­‐-­‐>          <argument>/path/to/app/logs/app.log</argument>          <argument  type="service"                                               id="sensio.logger.xml_formatter"  />              <!-­‐-­‐  Methods  to  call  -­‐-­‐>          <call  method="setDefaultSeverity">                  <argument>DEBUG</argument>          </call>      <!-­‐-­‐  Tags  -­‐-­‐>            <tag  name="tools.logger"  />         </service>  
  13. <service  id="sensio.logger"  class="Sensio\Logger"  >            <!-­‐-­‐

     Constructor  arguments  -­‐-­‐>          <argument>/path/to/app/logs/app.log</argument>          <argument  type="service"                                               id="sensio.logger.xml_formatter"  />              <!-­‐-­‐  Methods  to  call  -­‐-­‐>          <call  method="setDefaultSeverity">                  <argument>DEBUG</argument>          </call>      <!-­‐-­‐  Tags  -­‐-­‐>            <tag  name="tools.logger"  />         </service>  
  14. <service  id="sensio.logger"  class="Sensio\Logger"  >            <!-­‐-­‐

     Constructor  arguments  -­‐-­‐>          <argument>/path/to/app/logs/app.log</argument>          <argument  type="service"                                               id="sensio.logger.xml_formatter"  />              <!-­‐-­‐  Methods  to  call  -­‐-­‐>          <call  method="setDefaultSeverity">                  <argument>DEBUG</argument>          </call>      <!-­‐-­‐  Tags  -­‐-­‐>            <tag  name="tools.logger"  />         </service>  
  15. 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');   }  
  16. 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                          )                  )          )   )  
  17. Array  (          [exchange_rates]  =>  Array  (

                     [curl]  =>  Array  (                          [CURLOPT_TIMEOUT]  =>  10                          [CURLOPT_PROXY]  =>  http://localhost:8888                  )                  [endpoint]  =>  http://www.ecb.europa.eu/stats...          )   )  
  18. 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;    }   }  
  19. -­‐>scalarNode(‘some_url')    -­‐>isRequired()    -­‐>cannotBeEmpty()    -­‐>validate()      -­‐>ifTrue(function($value)

     {        return  !filter_var($value,  \FILTER_VALIDATE_URL);    })      -­‐>thenInvalid('Not  a  valid  url.')    -­‐>end()   -­‐>end()  

More Decks by Andreas Hucks

Other Decks in Programming