AbstractConfigurator.php 3.8 KB

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