Processor.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * @return array The processed configuration
  26. */
  27. public function process(NodeInterface $configTree, array $configs): array
  28. {
  29. $currentConfig = [];
  30. foreach ($configs as $config) {
  31. $config = $configTree->normalize($config);
  32. $currentConfig = $configTree->merge($currentConfig, $config);
  33. }
  34. return $configTree->finalize($currentConfig);
  35. }
  36. /**
  37. * Processes an array of configurations.
  38. *
  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): array
  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. public static function normalizeConfig(array $config, string $key, string $plural = null): array
  69. {
  70. if (null === $plural) {
  71. $plural = $key.'s';
  72. }
  73. if (isset($config[$plural])) {
  74. return $config[$plural];
  75. }
  76. if (isset($config[$key])) {
  77. if (\is_string($config[$key]) || !\is_int(key($config[$key]))) {
  78. // only one
  79. return [$config[$key]];
  80. }
  81. return $config[$key];
  82. }
  83. return [];
  84. }
  85. }