AbstractConfigurator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
  12. use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15. use Symfony\Component\DependencyInjection\Parameter;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\ExpressionLanguage\Expression;
  18. abstract class AbstractConfigurator
  19. {
  20. public const FACTORY = 'unknown';
  21. /**
  22. * @var callable(mixed, bool $allowService)|null
  23. */
  24. public static $valuePreProcessor;
  25. /** @internal */
  26. protected $definition;
  27. public function __call(string $method, array $args)
  28. {
  29. if (method_exists($this, 'set'.$method)) {
  30. return $this->{'set'.$method}(...$args);
  31. }
  32. throw new \BadMethodCallException(sprintf('Call to undefined method "%s::%s()".', static::class, $method));
  33. }
  34. public function __sleep()
  35. {
  36. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  37. }
  38. public function __wakeup()
  39. {
  40. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  41. }
  42. /**
  43. * Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value.
  44. *
  45. * @param mixed $value
  46. * @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are
  47. *
  48. * @return mixed the value, optionally cast to a Definition/Reference
  49. */
  50. public static function processValue($value, $allowServices = false)
  51. {
  52. if (\is_array($value)) {
  53. foreach ($value as $k => $v) {
  54. $value[$k] = static::processValue($v, $allowServices);
  55. }
  56. return self::$valuePreProcessor ? (self::$valuePreProcessor)($value, $allowServices) : $value;
  57. }
  58. if (self::$valuePreProcessor) {
  59. $value = (self::$valuePreProcessor)($value, $allowServices);
  60. }
  61. if ($value instanceof ReferenceConfigurator) {
  62. return new Reference($value->id, $value->invalidBehavior);
  63. }
  64. if ($value instanceof InlineServiceConfigurator) {
  65. $def = $value->definition;
  66. $value->definition = null;
  67. return $def;
  68. }
  69. if ($value instanceof self) {
  70. throw new InvalidArgumentException(sprintf('"%s()" can be used only at the root of service configuration files.', $value::FACTORY));
  71. }
  72. switch (true) {
  73. case null === $value:
  74. case is_scalar($value):
  75. return $value;
  76. case $value instanceof ArgumentInterface:
  77. case $value instanceof Definition:
  78. case $value instanceof Expression:
  79. case $value instanceof Parameter:
  80. case $value instanceof AbstractArgument:
  81. case $value instanceof Reference:
  82. if ($allowServices) {
  83. return $value;
  84. }
  85. }
  86. throw new InvalidArgumentException(sprintf('Cannot use values of type "%s" in service configuration files.', get_debug_type($value)));
  87. }
  88. }