Your SlideShare is downloading. ×
  • Like
  • Save
Things Your Mother Didnt Tell You About Bundle Configurations - Symfony Live…
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Things Your Mother Didnt Tell You About Bundle Configurations - Symfony Live…

  • 1,213 views
Published

 

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,213
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. I love myhttp://www.flickr.com/photos/slushpup/3055613967/
  • 2. Dennis Benkert Software Developer Coaching Coding Consulting
  • 3. Things Your Mother Didn’t TellYou About Bundle Configurations
  • 4. app/config.ymlparameters: global.page_size: 10 gloabl.page_num: 5 global.tracking.key: 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 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: some_webservice.user: This file was much http://example.com api some_webservice.key: Sdfihwef $5sdf” SAFAWEF some_webservice.ssl: true loooooonger… # More configuration more.config: 100 even.more.config: true oh_my_option: 123 foo.bar.baz: true application.lock_user: super misc.timeout: 300 http.meta.title: super page http.meta.keywords: - some - keywords - for - this - page http.description: Some crazy description for the homepage that we all love so much#...
  • 5. app/config.ymlparameters: global.page_size: in All one context 10 gloabl.page_num: 5 global.tracking.key: ASFDIPSADPFIHwer234123QSD # Various configuration Correct type? 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.levl: 42 # Connection data for some service some_webservice.url: http://example.com some_webservice.user: api Multiple connections?! Correct key?! some_webservice.key: Sdfihwef $5sdf” SAFAWEF some_webservice.ssl: true
  • 6. app/config.ymlmy_project_tracking: user: rebum api_version: 1_0 use_ssl: true # Do not touch this!!! track_super_users: falsemy_project_info: god_mode: More structure 123456234 true # Ticket level: 42my_project_ webservice: connection: url: http://example.com user: api key: Sdfihwef $5sdf” SAFAWEF ssl: true Correct type?!!11oneparameters: global.page_size: 10 gloabl.page_num: 5 global.tracking.key: ASFDIPSADPFIHwer234123QSD # ...
  • 7. Bad configuration Worse than bad code (In terms of structure)
  • 8. Bad validation Even worse
  • 9. feature_x_bundle.redirct: 302 Correct key?!
  • 10. Config Component http://www.flickr.com/photos/capcase/2735500813/
  • 11. Cache Locate ConfigValidate Load
  • 12. Cache Locate ConfigValidate Load
  • 13. Locate & Load http://www.flickr.com/photos/onthespiral/3406281939/
  • 14. use SymfonyComponentConfigFileLocator;$locator = new FileLocator( __DIR__./../Resources/config); Path(s) of resources
  • 15. Available in path(s)?$locator->locate(config.xml);
  • 16. src/YourBundle/DependencyInjection/YourBundleExtension.php Special Loader for DICuse SymfonyComponentDependencyInjectionLoader;$loader = new LoaderXmlFileLoader( $container, new FileLocator(...)); Path(s) of resources
  • 17. Load configuration in DIC$loader->load(services.xml); Your services
  • 18. $loader->load(services.xml);$loader->load(other_services.xml);
  • 19. Cache Locate ConfigValidate Load
  • 20. Validatehttp://www.flickr.com/photos/jeremybrooks/3214838875/
  • 21. Your bundle contextapp/config.ymlyour_bundle: enabled: true
  • 22. src/YourBundle/DependencyInjection/YourBundleExtension.phppublic function load($configs, /*...*/){ $config = $this unprocessed Matched and ->processConfiguration(/**/); if (true === $config[enabled]) { $loader->load(services.xml); } Processed and validated}
  • 23. your_bundle: enabled: true Validate Convertarray(enabled => true);
  • 24. Build a Config Tree http://www.flickr.com/photos/jlscha/6226656013/
  • 25. src/YourBundle/DependencyInjection/Configuration.phppublic function getConfigTreeBuilder(){ The config tree $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder ->root(your_bundle); // tree definition ... Your root node return $treeBuilder;}
  • 26. your_bundle: enabled: true
  • 27. $rootNode Node type Node name ->children() ->booleanNode(enabled) ->defaultFalse() ->end() ->end() More options;
  • 28. src/YourBundle/DependencyInjection/YourBundleExtension.php Correct typepublic function load($configs, /*...*/){ if (true === $config[enabled]) { // ... }}
  • 29. ScalarBoolean Array Node
  • 30. ScalarBoolean Array Node
  • 31. ->booleanNode(enabled)->end()
  • 32. your_bundle: enabled: troo
  • 33. Type validation
  • 34. ScalarBoolean Array Node
  • 35. your_bundle: timeout: 60
  • 36. All scalar types->scalarNode(timeout) ->defaultValue(3600) ->isRequired() ->cannotBeEmpty()->end() Validation options
  • 37. your_bundle: timeout: sixty seconds
  • 38. ->scalarNode(timeout) Custom validation // ... Validation logic ->validate() ->ifTrue(function ($v) { return !is_int($v); }) ->thenInvalid(No integer) ->end()->end() Custom error
  • 39. Custom error
  • 40. ScalarBoolean Array Node
  • 41. connection: url: http://example.com user: api key: $ome35ecre7Ke$
  • 42. Group of nodes->arrayNode(connection) ->children() ->scalarNode(url) ->isRequired() Specific validation ->end() ->scalarNode(user)->end() ->scalarNode(key)->end() ->end()->end()
  • 43. array(connection => array( url => http://example.com, user => api, key => $ome35ecre7Ke$ ));
  • 44. connections: default: url: http://example.com user: api key: $ome35ecre7Ke$ fallback: url: http://back.example.com user: fallback_api key: $ome35ecre7Ke$
  • 45. ->arrayNode(connections)Prototype variations ->useAttributeAsKey(name) ->prototype(array) ->children() ->scalarNode(url) ->isRequired() ->end() ->scalarNode(user)->end() ->scalarNode(key)->end() ->end() validation Prototype ->end() ->requiresAtLeastOneElement()->end()
  • 46. array ( connections => array ( default => array ( url => ..., user => api, key => $ome35ecre7Ke$ ), fallback => array ( url => ..., user => fallback_api, key => $ome35ecre7Ke$ )));
  • 47. Using Configuration http://www.flickr.com/photos/victornuno/222145881/
  • 48. src/YourBundle/DependencyInjection/YourBundleExtension.phppublic function load(/*...*/){ Load services if (true === $config[enabled]) { $loader->load(services.xml); }}
  • 49. src/YourBundle/DependencyInjection/YourBundleExtension.phppublic function load(/*...*/){ if (true === $config[enabled]) { $loader->load(services.xml); } Split services if (true === $config[add_extras]) { $loader->load(extras.xml); }}
  • 50. Manipulate DICsrc/YourBundle/DependencyInjection/YourBundleExtension.php$container->setParameter( your_bundle.timeout, $config[timeout]);
  • 51. src/YourBundle/Controller/YourController.phppublic function someAction(/*...*/){ $timeout = $container->getParameter( global.timeout);}
  • 52. sfConfig::get(global.timeout); Good old symfony times…
  • 53. Configure Services Not global options
  • 54. src/YourBundle/DependencyInjection/YourBundleExtension.php$container->setParameter( your_bundle.timeout, $config[timeout]);
  • 55. src/YourBundle/Resources/config/services.xml<?xml version="1.0" ?> You config option<container> <parameters> <parameter key="your_service_x.timeout"></parameter> </parameters> <services> <service id="your_service_x"> <argument>%your_service_x.timeout%</argument> </service> </services></container>
  • 56. src/YourBundle/DependencyInjection/YourBundleExtension.php$container->setParameter( your_service_x.timeout, $config[timeout]);
  • 57. Let‘s recaphttp://www.flickr.com/photos/tine72/5464869042/
  • 58. Configurationneeds structure
  • 59. Configurationneeds validation
  • 60. Configurationneeds conversion
  • 61. http://joind.in/7209