Processor.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Definition;
  11. /**
  12. * This class is the entry point for config normalization/merging/finalization.
  13. *
  14. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  15. */
  16. class Processor
  17. {
  18. /**
  19. * Processes an array of configurations.
  20. *
  21. * @param NodeInterface $configTree The node tree describing the configuration
  22. * @param array $configs An array of configuration items to process
  23. *
  24. * @return array The processed configuration
  25. */
  26. public function process(NodeInterface $configTree, array $configs)
  27. {
  28. $currentConfig = array();
  29. foreach ($configs as $config) {
  30. $config = $configTree->normalize($config);
  31. $currentConfig = $configTree->merge($currentConfig, $config);
  32. }
  33. return $configTree->finalize($currentConfig);
  34. }
  35. /**
  36. * Processes an array of configurations.
  37. *
  38. * @param ConfigurationInterface $configuration The configuration class
  39. * @param array $configs An array of configuration items to process
  40. *
  41. * @return array The processed configuration
  42. */
  43. public function processConfiguration(ConfigurationInterface $configuration, array $configs)
  44. {
  45. return $this->process($configuration->getConfigTreeBuilder()->buildTree(), $configs);
  46. }
  47. /**
  48. * Normalizes a configuration entry.
  49. *
  50. * This method returns a normalize configuration array for a given key
  51. * to remove the differences due to the original format (YAML and XML mainly).
  52. *
  53. * Here is an example.
  54. *
  55. * The configuration in XML:
  56. *
  57. * <twig:extension>twig.extension.foo</twig:extension>
  58. * <twig:extension>twig.extension.bar</twig:extension>
  59. *
  60. * And the same configuration in YAML:
  61. *
  62. * extensions: ['twig.extension.foo', 'twig.extension.bar']
  63. *
  64. * @param array $config A config array
  65. * @param string $key The key to normalize
  66. * @param string $plural The plural form of the key if it is irregular
  67. *
  68. * @return array
  69. */
  70. public static function normalizeConfig($config, $key, $plural = null)
  71. {
  72. if (null === $plural) {
  73. $plural = $key.'s';
  74. }
  75. if (isset($config[$plural])) {
  76. return $config[$plural];
  77. }
  78. if (isset($config[$key])) {
  79. if (\is_string($config[$key]) || !\is_int(key($config[$key]))) {
  80. // only one
  81. return array($config[$key]);
  82. }
  83. return $config[$key];
  84. }
  85. return array();
  86. }
  87. }