Contenu connexe Similaire à What mom never told you about bundle configurations - Symfony Live Paris 2012 Similaire à What mom never told you about bundle configurations - Symfony Live Paris 2012 (20) What mom never told you about bundle configurations - Symfony Live Paris 20127. 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
8. my_project_tracking:
user: rebum
api_version: 1_0
use_ssl: true # Do not touch this!!!
track_super_users: false
my_project_info:
god_mode: true # Ticket 123456234
level: 42
my_project_ webservice:
connection:
url: http://example.com
user: api
key: Sdfihwef $5sdf” SAFAWEF
ssl: true
parameters:
global.page_size: 10
gloabl.page_num: 5
global.tracking.key: ASFDIPSADPFIHwer234123QSD
# ...
12. Cache Locate
Config
Validate Load
13. Cache Locate
Config
Validate Load
14. Locate & Load
http://www.flickr.com/photos/onthespiral/3406281939/
15. $locator = new FileLocator(
__DIR__.'/../Resources/config'
);
Path(s) of resources
20. Cache Locate
Config
Validate Load
24. your_bundle:
enabled: true
Validate
Convert
array('enabled' => true);
25. Build a Config Tree
http://www.flickr.com/photos/jlscha/6226656013/
28. $rootNode Node type Node name
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->end() More options
;
38. ->scalarNode('timeout')
Custom validation
// ... Validation logic
->validate()
->ifTrue(function ($v) {
return !is_int($v);
})
->thenInvalid('No integer')
->end()
->end() Custom error
41. connection:
url: http://example.com
user: api
key: $ome35ecre7Ke$
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() Key generation
->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$'
)
));
49. public function load(/*...*/)
{
if (true === $config['enabled']) {
$loader->load('services.xml');
} Split services
if (true === $config['add_extras']) {
$loader->load('extras.xml');
}
}
52. 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);
54. /** Custom error
* @expectedException Exception
* @expectedMessage No integer
*/
public function testConfiguration()
{}