AbstractConfigurator.php 2.8 KB

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