ArrayNode
node is used to build the configuration tree. If you want an ArrayNode
accommodate not just pre-defined nodes, but several other ArrayNode
, but with a clearly identical predefined internal structure, you can use PrototypedArrayNode
.Symfony\Component\Config\Definition\Builder\TreeBuilder
about this way: <?php use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('acme_demo'); $rootNode ->children() ->arrayNode('entities') ->addDefaultsIfNotSet() ->prototype('scalar')->end() ->defaultValue( array( 'Acme\BaseBundle\Entity\DefaultEntity1', 'Acme\BaseBundle\Entity\DefaultEntity2', ) ) ->end(); return $rootNode; } }
append
method of the NodeBuilder
. <children> <child> </child> </children>
"children" => Array( [0] => " " )
normalize()
method from Symfony\Component\Config\Definition\NodeInterface
. And besides, Symfony\Component\Config\Definition\BaseNode
has another method preNormalize
. The latter is used to foo_bar
keys like foo_bar
and foo-bar
.finalizeValue
method of the descendants of BaseNode
NodeDefinition
methods and its descendants like isRequired
, as well as using the extended validation delegated to the Symfony\Component\Config\Definition\Builder\ValidationBuilder
class.Symfony\Component\Config\Definition\Builder\MergeBuilder
. Delegation of checks to it is performed by the merge () method of the NodeDefinition
class. For example, you can prohibit overriding the value of a selected configuration key by other configuration files after it was read for the first time. $configs = array($config1, $config2); // $processor = new Processor(); // $configuration = new Configuration(); // Configuration c (. ). $processedConfiguration = $processor->processConfiguration( $configuration, $configs );
TreeBuilder
uses an instance of the class Symfony\Component\Config\Definition\Builder\NodeBuilder
. Therefore, you can easily define your own node types for configuration. To do this, you need to create your own NodeInterface
implementation and your descendant \Symfony\Component\Config\Definition\Builder\NodeDefinition
. After that, just call the setNodeClass
method.Symfony\Component\Config\Definition\Dumper
namespace. Now there are two options: YamlReferenceDumper
and XmlReferenceDumper
. These dumpers are used, for example, when you call from the console ./bin/symfony config:dump-reference
(see Symfony\Bundle\FrameworkBundle\Command\ConfigDumpReferenceCommand
)Symfony\Component\Config\Resource\ResourceInterface
). The concept of a resource is rather abstract. It can be either a file or any other data source. For example, the database table or field in it.Symfony\Component\Config\ResourceCheckerInterface
) monitor resources for changes.Symfony\Component\Config\Loader\LoaderInterface
).Symfony\Component\Config\Loader\LoaderResolverInterface
) search for a suitable loader for the resource.Symfony\Component\Config\Loader\DelegatingLoader
allows you to load a resource by automatically finding the necessary bootloader, iterating over the array of transferred resolvers.Symfony\Component\Config\FileLocator
Symfony\Component\DependencyInjection\Loader\YamlFileLoader
, you can see that the configuration is loaded directly into the container.Symfony\Component\Config\ConfigCache
: <?php use Symfony\Component\Config\ConfigCache; use Symfony\Component\Config\Resource\FileResource; $cachePath = __DIR__.'/cache/appSomeCacheFile.php'; // , , $cacheFile = new ConfigCache($cachePath, true); if (!$cacheFile->isFresh()) { $configFiles = []; // , $resources = array(); foreach ($configFiles as $cfgFile) { // // ..... // $resources[] = new FileResource($cfgFile); } $code = '...'; // // . $cacheFile->write($code, $resources); } // require $cachePath;
Symfony\Component\Config\ConfigCacheFactory
instead of ConfigCache
for further work. ConfigCacheFactory
takes in a callable
constructor that will rebuild the cache. <?php // try { $basepath = __DIR__ . '/config'; $configuration = Yaml::parse($basepath . '/config.yml'); } catch (\InvalidArgumentException $exception) { exit(", "); } // ConfigurationInterface *.yml $yamlConfiguration = new \Configuration(); // ( *.yml) $processor = new Processor(); $configuration = $processor->processConfiguration( $yamlConfiguration, array($configuration) // *.yml ); use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\Builder\TreeBuilder; class Configuration { /** * @return TreeBuilder */ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('arbitary'); $rootNode->children() ->scalarNode('host') ->isRequired() ->cannotBeEmpty() ->end() ->scalarNode('username') ->isRequired() ->cannotBeEmpty() ->end() ->scalarNode('password') ->isRequired() ->cannotBeEmpty() ->end() ->booleanNode('bindRequiresDn') ->defaultTrue() ->end(); return $treeBuilder; } }
Source: https://habr.com/ru/post/271417/
All Articles