Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection\Loader\Configurator; Chris@14: Chris@14: use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; Chris@14: use Symfony\Component\DependencyInjection\Definition; Chris@14: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@14: use Symfony\Component\DependencyInjection\Parameter; Chris@14: use Symfony\Component\DependencyInjection\Reference; Chris@14: use Symfony\Component\ExpressionLanguage\Expression; Chris@14: Chris@14: abstract class AbstractConfigurator Chris@14: { Chris@14: const FACTORY = 'unknown'; Chris@14: Chris@14: /** @internal */ Chris@14: protected $definition; Chris@14: Chris@14: public function __call($method, $args) Chris@14: { Chris@14: if (method_exists($this, 'set'.$method)) { Chris@17: return \call_user_func_array([$this, 'set'.$method], $args); Chris@14: } Chris@14: Chris@17: throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s()', \get_class($this), $method)); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value. Chris@14: * Chris@14: * @param mixed $value Chris@14: * @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are Chris@14: * Chris@14: * @return mixed the value, optionally cast to a Definition/Reference Chris@14: */ Chris@14: public static function processValue($value, $allowServices = false) Chris@14: { Chris@17: if (\is_array($value)) { Chris@14: foreach ($value as $k => $v) { Chris@14: $value[$k] = static::processValue($v, $allowServices); Chris@14: } Chris@14: Chris@14: return $value; Chris@14: } Chris@14: Chris@14: if ($value instanceof ReferenceConfigurator) { Chris@14: return new Reference($value->id, $value->invalidBehavior); Chris@14: } Chris@14: Chris@14: if ($value instanceof InlineServiceConfigurator) { Chris@14: $def = $value->definition; Chris@14: $value->definition = null; Chris@14: Chris@14: return $def; Chris@14: } Chris@14: Chris@14: if ($value instanceof self) { Chris@14: throw new InvalidArgumentException(sprintf('"%s()" can be used only at the root of service configuration files.', $value::FACTORY)); Chris@14: } Chris@14: Chris@14: switch (true) { Chris@14: case null === $value: Chris@14: case is_scalar($value): Chris@14: return $value; Chris@14: Chris@14: case $value instanceof ArgumentInterface: Chris@14: case $value instanceof Definition: Chris@14: case $value instanceof Expression: Chris@14: case $value instanceof Parameter: Chris@14: case $value instanceof Reference: Chris@14: if ($allowServices) { Chris@14: return $value; Chris@14: } Chris@14: } Chris@14: Chris@17: throw new InvalidArgumentException(sprintf('Cannot use values of type "%s" in service configuration files.', \is_object($value) ? \get_class($value) : \gettype($value))); Chris@14: } Chris@14: }