I love myhttp://www.flickr.com/photos/slushpup/3055613967/
Dennis Benkert Software Developer     Coding     Consulting     Coaching Symfony User Groups Symfony Day Cologne
What mom never told you about    Bundle configurations
parameters:    global.page_size:        10    gloabl.page_num:         5    global.tracking.key:     Correct type?        ...
my_project_tracking:    user:                  rebum    api_version:           1_0    use_ssl:               true    # Do ...
Bad configuration   Worse than bad code
Bad validation    Even worse
Config Component    http://www.flickr.com/photos/capcase/2735500813/
Cache               Locate           ConfigValidate            Load
Cache               Locate           ConfigValidate            Load
Locate & Load      http://www.flickr.com/photos/onthespiral/3406281939/
$locator = new FileLocator(    __DIR__./../Resources/config);        Path(s) of resources
Available in path(s)?$locator->locate(config.xml);
// src/YourBundle/DependencyInjection/// YourBundleExtension.php        Special Loader for DIC$loader = new LoaderXmlFileL...
Load configuration in DIC$loader->load(services.xml);
$loader->load(services.xml);$loader->load(other_services.xml);
Cache               Locate           ConfigValidate            Load
Validatehttp://www.flickr.com/photos/jeremybrooks/3214838875/
your_bundle:    enabled: true
// src/YourBundle/DependencyInjection/// YourBundleExtension.phppublic function load($configs, /*...*/){     // processing...
your_bundle:    enabled: true               Validate               Convertarray(enabled => true);
Build a Config Tree          http://www.flickr.com/photos/jlscha/6226656013/
// src/YourBundle/DependencyInjection/// Configuration.php                    The config treepublic function getConfigTree...
your_bundle:    enabled: true
$rootNode Node type           Node name     ->children()          ->booleanNode(enabled)                ->defaultFalse()  ...
ScalarBoolean            Array          Node
ScalarBoolean            Array          Node
->booleanNode(enabled)->end()
your_bundle:    enabled: troo
Type validation
ScalarBoolean            Array          Node
your_bundle:    timeout: 60
All scalar types->scalarNode(timeout)     ->defaultValue(3600)     ->isRequired()     ->cannotBeEmpty()->end()            ...
your_bundle:    timeout: sixty seconds
->scalarNode(timeout)        Custom validation     // ...                      Validation logic     ->validate()          ...
Custom error
ScalarBoolean            Array          Node
connection:    url: http://example.com    user: api    key: $ome35ecre7Ke$
Group of nodes->arrayNode(connection)    ->children() Specific validation        ->scalarNode(url)            ->isRequired...
array(connection =>    array(        url => http://example.com,        user => api,        key => $ome35ecre7Ke$    ));
connections:     default:          url:    http://example.com          user:   api          key:    $ome35ecre7Ke$     fal...
->arrayNode(connections)Prototype variations    ->useAttributeAsKey(name)    ->prototype(array)        ->children()       ...
array (    connections => array (        default => array (            url => ...,            user => api,            key ...
Build upon structure      http://www.flickr.com/photos/victornuno/222145881/
// src/YourBundle/DependencyInjection/// YourBundleExtension.phppublic function load(/*...*/)                 Load service...
public function load(/*...*/){    if (true === $config[enabled]) {        $loader->load(services.xml);    }            Spl...
Manipulate DIC$container->setParameter(    your_bundle.timeout,    $config[timeout]);
Testing Configuration        http://www.flickr.com/photos/sidelong/246816211/
Get DIC$container =Extension  Register your $this->createContainer();$container->registerExtension( Extension             ...
$this->assertTrue(    $container->getParameter(enabled));
/**             Custom error * @expectedException Exception * @expectedMessage No integer */public function testConfigurat...
Let‘s recaphttp://www.flickr.com/photos/tine72/5464869042/
Configurationneeds structure
Configurationneeds validation
Configurationneeds conversion
Config Component  is awesome
What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012
Upcoming SlideShare
Loading in...5
×

What mom never told you about bundle configurations - Symfony Live Paris 2012

17,444

Published on

Published in: Technology, Business
3 Comments
13 Likes
Statistics
Notes
No Downloads
Views
Total Views
17,444
On Slideshare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
0
Comments
3
Likes
13
Embeds 0
No embeds

No notes for slide

What mom never told you about bundle configurations - Symfony Live Paris 2012

  1. 1. I love myhttp://www.flickr.com/photos/slushpup/3055613967/
  2. 2. Dennis Benkert Software Developer Coding Consulting Coaching Symfony User Groups Symfony Day Cologne
  3. 3. What mom never told you about Bundle configurations
  4. 4. parameters: global.page_size: 10 gloabl.page_num: 5 global.tracking.key: Correct type? ASFDIPSADPFIHwer234123QSD # Various configuration webservices.timeout: 100 content_check.enabled: true birthdates.start: 1950 # TODO: Delete after release logging.registrations: true # Tracking Bundle Configuration trackingbundle.user: rebum Only this bundle?! trackingbundle.api.version: 1_0 trackingbundle.use_ssl: true # Do not touch this!!! trackingbundle.track_super_users: false infobundle.god_mode: true # Ticket 123456234 infobundle.level: 42 # Connection data for some service some_webservice.url: http://example.com some_webservice.user: api Multiple connections?! some_webservice.key: Sdfihwef $5sdf” SAFAWEF some_webservice.ssl: true
  5. 5. my_project_tracking: user: rebum api_version: 1_0 use_ssl: true # Do not touch this!!! track_super_users: falsemy_project_info: god_mode: true # Ticket 123456234 level: 42my_project_ webservice: connection: url: http://example.com user: api key: Sdfihwef $5sdf” SAFAWEF ssl: trueparameters: global.page_size: 10 gloabl.page_num: 5 global.tracking.key: ASFDIPSADPFIHwer234123QSD # ...
  6. 6. Bad configuration Worse than bad code
  7. 7. Bad validation Even worse
  8. 8. Config Component http://www.flickr.com/photos/capcase/2735500813/
  9. 9. Cache Locate ConfigValidate Load
  10. 10. Cache Locate ConfigValidate Load
  11. 11. Locate & Load http://www.flickr.com/photos/onthespiral/3406281939/
  12. 12. $locator = new FileLocator( __DIR__./../Resources/config); Path(s) of resources
  13. 13. Available in path(s)?$locator->locate(config.xml);
  14. 14. // src/YourBundle/DependencyInjection/// YourBundleExtension.php Special Loader for DIC$loader = new LoaderXmlFileLoader( $container, new FileLocator(...)); Path(s) of resources
  15. 15. Load configuration in DIC$loader->load(services.xml);
  16. 16. $loader->load(services.xml);$loader->load(other_services.xml);
  17. 17. Cache Locate ConfigValidate Load
  18. 18. Validatehttp://www.flickr.com/photos/jeremybrooks/3214838875/
  19. 19. your_bundle: enabled: true
  20. 20. // src/YourBundle/DependencyInjection/// YourBundleExtension.phppublic function load($configs, /*...*/){ // processing and unprocessed Matched ... if (true === $config[enabled]) { $loader->load(services.xml); } Processed and validated}
  21. 21. your_bundle: enabled: true Validate Convertarray(enabled => true);
  22. 22. Build a Config Tree http://www.flickr.com/photos/jlscha/6226656013/
  23. 23. // src/YourBundle/DependencyInjection/// Configuration.php The config treepublic function getConfigTreeBuilder(){ $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder ->root(your_bundle); // tree definition ... Your root node return $treeBuilder;}
  24. 24. your_bundle: enabled: true
  25. 25. $rootNode Node type Node name ->children() ->booleanNode(enabled) ->defaultFalse() ->end() ->end() More options;
  26. 26. ScalarBoolean Array Node
  27. 27. ScalarBoolean Array Node
  28. 28. ->booleanNode(enabled)->end()
  29. 29. your_bundle: enabled: troo
  30. 30. Type validation
  31. 31. ScalarBoolean Array Node
  32. 32. your_bundle: timeout: 60
  33. 33. All scalar types->scalarNode(timeout) ->defaultValue(3600) ->isRequired() ->cannotBeEmpty()->end() Validation options
  34. 34. your_bundle: timeout: sixty seconds
  35. 35. ->scalarNode(timeout) Custom validation // ... Validation logic ->validate() ->ifTrue(function ($v) { return !is_int($v); }) ->thenInvalid(No integer) ->end()->end() Custom error
  36. 36. Custom error
  37. 37. ScalarBoolean Array Node
  38. 38. connection: url: http://example.com user: api key: $ome35ecre7Ke$
  39. 39. Group of nodes->arrayNode(connection) ->children() Specific validation ->scalarNode(url) ->isRequired() ->end() ->scalarNode(user)->end() ->scalarNode(key)->end() ->end()->end()
  40. 40. array(connection => array( url => http://example.com, user => api, key => $ome35ecre7Ke$ ));
  41. 41. connections: default: url: http://example.com user: api key: $ome35ecre7Ke$ fallback: url: http://back.example.com user: fallback_api key: $ome35ecre7Ke$
  42. 42. ->arrayNode(connections)Prototype variations ->useAttributeAsKey(name) ->prototype(array) ->children() Key generation ->scalarNode(url) ->isRequired() ->end() ->scalarNode(user)->end() ->scalarNode(key)->end() ->end() validation Prototype ->end() ->requiresAtLeastOneElement()->end()
  43. 43. array ( connections => array ( default => array ( url => ..., user => api, key => $ome35ecre7Ke$ ), fallback => array ( url => ..., user => fallback_api, key => $ome35ecre7Ke$ )));
  44. 44. Build upon structure http://www.flickr.com/photos/victornuno/222145881/
  45. 45. // src/YourBundle/DependencyInjection/// YourBundleExtension.phppublic function load(/*...*/) Load services{ if (true === $config[enabled]) { $loader->load(services.xml); }}
  46. 46. public function load(/*...*/){ if (true === $config[enabled]) { $loader->load(services.xml); } Split services if (true === $config[add_extras]) { $loader->load(extras.xml); }}
  47. 47. Manipulate DIC$container->setParameter( your_bundle.timeout, $config[timeout]);
  48. 48. Testing Configuration http://www.flickr.com/photos/sidelong/246816211/
  49. 49. Get DIC$container =Extension Register your $this->createContainer();$container->registerExtension( Extension Load your new YourBundleExtension());$container->loadFromExtension( Process configuration array()); your_bundle,$this->compileContainer($container);
  50. 50. $this->assertTrue( $container->getParameter(enabled));
  51. 51. /** Custom error * @expectedException Exception * @expectedMessage No integer */public function testConfiguration(){}
  52. 52. Let‘s recaphttp://www.flickr.com/photos/tine72/5464869042/
  53. 53. Configurationneeds structure
  54. 54. Configurationneeds validation
  55. 55. Configurationneeds conversion
  56. 56. Config Component is awesome

×