Processor.php 2.5 KB

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