Processor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @final since version 4.1
  17. */
  18. class Processor
  19. {
  20. /**
  21. * Processes an array of configurations.
  22. *
  23. * @param NodeInterface $configTree The node tree describing the configuration
  24. * @param array $configs An array of configuration items to process
  25. *
  26. * @return array The processed configuration
  27. */
  28. public function process(NodeInterface $configTree, array $configs)
  29. {
  30. $currentConfig = [];
  31. foreach ($configs as $config) {
  32. $config = $configTree->normalize($config);
  33. $currentConfig = $configTree->merge($currentConfig, $config);
  34. }
  35. return $configTree->finalize($currentConfig);
  36. }
  37. /**
  38. * Processes an array of configurations.
  39. *
  40. * @param ConfigurationInterface $configuration The configuration class
  41. * @param array $configs An array of configuration items to process
  42. *
  43. * @return array The processed configuration
  44. */
  45. public function processConfiguration(ConfigurationInterface $configuration, array $configs)
  46. {
  47. return $this->process($configuration->getConfigTreeBuilder()->buildTree(), $configs);
  48. }
  49. /**
  50. * Normalizes a configuration entry.
  51. *
  52. * This method returns a normalize configuration array for a given key
  53. * to remove the differences due to the original format (YAML and XML mainly).
  54. *
  55. * Here is an example.
  56. *
  57. * The configuration in XML:
  58. *
  59. * <twig:extension>twig.extension.foo</twig:extension>
  60. * <twig:extension>twig.extension.bar</twig:extension>
  61. *
  62. * And the same configuration in YAML:
  63. *
  64. * extensions: ['twig.extension.foo', 'twig.extension.bar']
  65. *
  66. * @param array $config A config array
  67. * @param string $key The key to normalize
  68. * @param string $plural The plural form of the key if it is irregular
  69. *
  70. * @return array
  71. */
  72. public static function normalizeConfig($config, $key, $plural = null)
  73. {
  74. if (null === $plural) {
  75. $plural = $key.'s';
  76. }
  77. if (isset($config[$plural])) {
  78. return $config[$plural];
  79. }
  80. if (isset($config[$key])) {
  81. if (\is_string($config[$key]) || !\is_int(key($config[$key]))) {
  82. // only one
  83. return [$config[$key]];
  84. }
  85. return $config[$key];
  86. }
  87. return [];
  88. }
  89. }