php - How can I validate configurations that live under the "parameters" root? -
I am working on a project in Symphony 2.3.x. I have a service defined:
Services: myservice: Category: MyServiceClass Logic: [% settings%]
In the logic of the service settings defined in a MyBundle \ resources \ config \ parameters.yml file : Parameter: Settings: Settings 1: 15 Settings 2: True Everything works great, but I validate this file Want to Therefore, I have this in the MyBundle \ DependencyInjection \ configuration file: class configuration tools configuration infraface {public function getConfigTreeBuilder () {$ treeBuilder = New TreeBuilder (); $ Routodeod = $ Treebuilder- & gt; Root ('parameter'); $ RootNode- & gt; Children () - & gt; Array node ('setting') - & gt; IsRequired () - & gt; CannotBeEmpty () - & gt; Children () - & gt; Integer node ('setting 1') - & gt; IsRequired () - & gt; Canobbeeplity () - & gt; Min (1) - & gt; End () - & gt; Boolean node ('setting 2') - & gt; IsRequired () - & gt; Canotbeeplline () - & gt; Default Tray () - & gt; () - & gt; End () - & gt; the ending (); Return $ Tree Builder; }} << Code> I have this in my MyBundle \ DependencyInjection \ MyExtension file: class increases the MyExtension extension { Public Function Load (array $ configs, containerbuilder $ containers) {$ processor = new processor (); $ Configuration = new configuration (); $ Processed configuration = $ processor- & gt; Process configuration ($ config $); $ ConfigLoader = New Loader \ YumFile Loader ($ Container, New File Locator (__DIR __. '/ .. / Resource / Config')); $ ConfigLoader- & gt; Load ('services.yml'); $ ConfigLoader- & gt; Load ('parameters.yml'); }}
Everything is fine but the problem is, if I have configuration min (1) in Minimum (100) will break the functionality and tell me that my parameters are not correct even then, it just keeps working even if I do not make any difference. Therefore the recognition has somehow been ignored. I think I'm doing something wrong by default (like the root node should be initialized in some other way), but I can not know what's going on.
Only one document can be found on it, but there is no mention for those configuration files in the form of the parameter as the root node (which I know Is done in a particular way). How do I make my validation code actually validate the configuration file?
Thanks in advance.
UPDATE Another clue I've just received, if I first file the parameters.yml Loads and then only call $ processor-> process configuration , then I get an exception: InvalidConfigurationException: path "parameters" on the child node "Settings" should be configured.
parameter section is part of the container configuration , Not bundle configuration so you can either see the logic inside the constructor whether the parameters were correct or you do not use the parameter section and move them to the bundle configuration. Since the settings are properly configured in the configuration settings, I advise you that they are put in the configuration class.
The name of the root element in your configuration class does nothing. Your bundle's di-aliases are used (like AcmeDemoBundle -> gt; ). I would suggest removing the "settings" node from your configuration, since everything has a setting, now you can put it in your config.yml : After that, in your bundle extension, now in the $ Processed Configuration variable (which should not be "Bundle" in Bit): acme_demo: setting1: 10 setting2: false array ('setting1' = & gt; 10; 'setting2' = & gt; wrong) . You can use it to set parameters: Public function loads (array $ configs, ContainerBuilder $ containers) {// ... $ container- & gt; SetParameter ('acme_demo.settings', $ processedConfiguration); } I also recommend using more intelligent names, do not say much about "setting 1" and "setting 2" settings.
Comments
Post a Comment