Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Loader; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Alias; Chris@14: use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; Chris@14: use Symfony\Component\DependencyInjection\Argument\BoundArgument; Chris@14: use Symfony\Component\DependencyInjection\Argument\IteratorArgument; Chris@14: use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; Chris@14: use Symfony\Component\DependencyInjection\ChildDefinition; Chris@14: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\ContainerInterface; Chris@0: use Symfony\Component\DependencyInjection\Definition; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@17: use Symfony\Component\DependencyInjection\Reference; Chris@17: use Symfony\Component\ExpressionLanguage\Expression; Chris@0: use Symfony\Component\Yaml\Exception\ParseException; Chris@0: use Symfony\Component\Yaml\Parser as YamlParser; Chris@14: use Symfony\Component\Yaml\Tag\TaggedValue; Chris@0: use Symfony\Component\Yaml\Yaml; Chris@0: Chris@0: /** Chris@0: * YamlFileLoader loads YAML files service definitions. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class YamlFileLoader extends FileLoader Chris@0: { Chris@17: private static $serviceKeywords = [ Chris@0: 'alias' => 'alias', Chris@0: 'parent' => 'parent', Chris@0: 'class' => 'class', Chris@0: 'shared' => 'shared', Chris@0: 'synthetic' => 'synthetic', Chris@0: 'lazy' => 'lazy', Chris@0: 'public' => 'public', Chris@0: 'abstract' => 'abstract', Chris@0: 'deprecated' => 'deprecated', Chris@0: 'factory' => 'factory', Chris@0: 'file' => 'file', Chris@0: 'arguments' => 'arguments', Chris@0: 'properties' => 'properties', Chris@0: 'configurator' => 'configurator', Chris@0: 'calls' => 'calls', Chris@0: 'tags' => 'tags', Chris@0: 'decorates' => 'decorates', Chris@0: 'decoration_inner_name' => 'decoration_inner_name', Chris@0: 'decoration_priority' => 'decoration_priority', Chris@0: 'autowire' => 'autowire', Chris@0: 'autowiring_types' => 'autowiring_types', Chris@14: 'autoconfigure' => 'autoconfigure', Chris@14: 'bind' => 'bind', Chris@17: ]; Chris@14: Chris@17: private static $prototypeKeywords = [ Chris@14: 'resource' => 'resource', Chris@14: 'namespace' => 'namespace', Chris@14: 'exclude' => 'exclude', Chris@14: 'parent' => 'parent', Chris@14: 'shared' => 'shared', Chris@14: 'lazy' => 'lazy', Chris@14: 'public' => 'public', Chris@14: 'abstract' => 'abstract', Chris@14: 'deprecated' => 'deprecated', Chris@14: 'factory' => 'factory', Chris@14: 'arguments' => 'arguments', Chris@14: 'properties' => 'properties', Chris@14: 'configurator' => 'configurator', Chris@14: 'calls' => 'calls', Chris@14: 'tags' => 'tags', Chris@14: 'autowire' => 'autowire', Chris@14: 'autoconfigure' => 'autoconfigure', Chris@14: 'bind' => 'bind', Chris@17: ]; Chris@14: Chris@17: private static $instanceofKeywords = [ Chris@14: 'shared' => 'shared', Chris@14: 'lazy' => 'lazy', Chris@14: 'public' => 'public', Chris@14: 'properties' => 'properties', Chris@14: 'configurator' => 'configurator', Chris@14: 'calls' => 'calls', Chris@14: 'tags' => 'tags', Chris@14: 'autowire' => 'autowire', Chris@17: ]; Chris@14: Chris@17: private static $defaultsKeywords = [ Chris@14: 'public' => 'public', Chris@14: 'tags' => 'tags', Chris@14: 'autowire' => 'autowire', Chris@14: 'autoconfigure' => 'autoconfigure', Chris@14: 'bind' => 'bind', Chris@17: ]; Chris@0: Chris@0: private $yamlParser; Chris@0: Chris@14: private $anonymousServicesCount; Chris@14: private $anonymousServicesSuffix; Chris@14: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function load($resource, $type = null) Chris@0: { Chris@0: $path = $this->locator->locate($resource); Chris@0: Chris@0: $content = $this->loadFile($path); Chris@0: Chris@14: $this->container->fileExists($path); Chris@0: Chris@0: // empty file Chris@0: if (null === $content) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // imports Chris@0: $this->parseImports($content, $path); Chris@0: Chris@0: // parameters Chris@0: if (isset($content['parameters'])) { Chris@17: if (!\is_array($content['parameters'])) { Chris@14: throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $path)); Chris@0: } Chris@0: Chris@0: foreach ($content['parameters'] as $key => $value) { Chris@14: $this->container->setParameter($key, $this->resolveServices($value, $path, true)); Chris@0: } Chris@0: } Chris@0: Chris@0: // extensions Chris@0: $this->loadFromExtensions($content); Chris@0: Chris@0: // services Chris@14: $this->anonymousServicesCount = 0; Chris@17: $this->anonymousServicesSuffix = '~'.ContainerBuilder::hash($path); Chris@17: $this->setCurrentDir(\dirname($path)); Chris@14: try { Chris@14: $this->parseDefinitions($content, $path); Chris@14: } finally { Chris@17: $this->instanceof = []; Chris@14: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supports($resource, $type = null) Chris@0: { Chris@17: if (!\is_string($resource)) { Chris@14: return false; Chris@14: } Chris@14: Chris@17: if (null === $type && \in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yaml', 'yml'], true)) { Chris@14: return true; Chris@14: } Chris@14: Chris@17: return \in_array($type, ['yaml', 'yml'], true); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses all imports. Chris@0: * Chris@0: * @param array $content Chris@0: * @param string $file Chris@0: */ Chris@0: private function parseImports(array $content, $file) Chris@0: { Chris@0: if (!isset($content['imports'])) { Chris@0: return; Chris@0: } Chris@0: Chris@17: if (!\is_array($content['imports'])) { Chris@0: throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file)); Chris@0: } Chris@0: Chris@17: $defaultDirectory = \dirname($file); Chris@0: foreach ($content['imports'] as $import) { Chris@17: if (!\is_array($import)) { Chris@17: $import = ['resource' => $import]; Chris@14: } Chris@14: if (!isset($import['resource'])) { Chris@14: throw new InvalidArgumentException(sprintf('An import should provide a resource in %s. Check your YAML syntax.', $file)); Chris@0: } Chris@0: Chris@0: $this->setCurrentDir($defaultDirectory); Chris@14: $this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses definitions. Chris@0: * Chris@0: * @param array $content Chris@0: * @param string $file Chris@0: */ Chris@0: private function parseDefinitions(array $content, $file) Chris@0: { Chris@0: if (!isset($content['services'])) { Chris@0: return; Chris@0: } Chris@0: Chris@17: if (!\is_array($content['services'])) { Chris@0: throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file)); Chris@0: } Chris@0: Chris@18: if (\array_key_exists('_instanceof', $content['services'])) { Chris@14: $instanceof = $content['services']['_instanceof']; Chris@14: unset($content['services']['_instanceof']); Chris@14: Chris@17: if (!\is_array($instanceof)) { Chris@17: throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', \gettype($instanceof), $file)); Chris@14: } Chris@17: $this->instanceof = []; Chris@14: $this->isLoadingInstanceof = true; Chris@14: foreach ($instanceof as $id => $service) { Chris@17: if (!$service || !\is_array($service)) { Chris@14: throw new InvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@17: if (\is_string($service) && 0 === strpos($service, '@')) { Chris@14: throw new InvalidArgumentException(sprintf('Type definition "%s" cannot be an alias within "_instanceof" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@17: $this->parseDefinition($id, $service, $file, []); Chris@14: } Chris@14: } Chris@14: Chris@14: $this->isLoadingInstanceof = false; Chris@14: $defaults = $this->parseDefaults($content, $file); Chris@0: foreach ($content['services'] as $id => $service) { Chris@14: $this->parseDefinition($id, $service, $file, $defaults); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * @param array $content Chris@14: * @param string $file Chris@14: * Chris@14: * @return array Chris@14: * Chris@14: * @throws InvalidArgumentException Chris@14: */ Chris@14: private function parseDefaults(array &$content, $file) Chris@14: { Chris@18: if (!\array_key_exists('_defaults', $content['services'])) { Chris@17: return []; Chris@14: } Chris@14: $defaults = $content['services']['_defaults']; Chris@14: unset($content['services']['_defaults']); Chris@14: Chris@17: if (!\is_array($defaults)) { Chris@17: throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', \gettype($defaults), $file)); Chris@14: } Chris@14: Chris@14: foreach ($defaults as $key => $default) { Chris@14: if (!isset(self::$defaultsKeywords[$key])) { Chris@14: throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::$defaultsKeywords))); Chris@14: } Chris@14: } Chris@14: Chris@14: if (isset($defaults['tags'])) { Chris@17: if (!\is_array($tags = $defaults['tags'])) { Chris@14: throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file)); Chris@14: } Chris@14: Chris@14: foreach ($tags as $tag) { Chris@17: if (!\is_array($tag)) { Chris@17: $tag = ['name' => $tag]; Chris@14: } Chris@14: Chris@14: if (!isset($tag['name'])) { Chris@14: throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file)); Chris@14: } Chris@14: $name = $tag['name']; Chris@14: unset($tag['name']); Chris@14: Chris@17: if (!\is_string($name) || '' === $name) { Chris@14: throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file)); Chris@14: } Chris@14: Chris@14: foreach ($tag as $attribute => $value) { Chris@14: if (!is_scalar($value) && null !== $value) { Chris@14: throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file)); Chris@14: } Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: if (isset($defaults['bind'])) { Chris@17: if (!\is_array($defaults['bind'])) { Chris@14: throw new InvalidArgumentException(sprintf('Parameter "bind" in "_defaults" must be an array in %s. Check your YAML syntax.', $file)); Chris@14: } Chris@14: Chris@14: $defaults['bind'] = array_map(function ($v) { return new BoundArgument($v); }, $this->resolveServices($defaults['bind'], $file)); Chris@14: } Chris@14: Chris@14: return $defaults; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @param array $service Chris@14: * Chris@14: * @return bool Chris@14: */ Chris@14: private function isUsingShortSyntax(array $service) Chris@14: { Chris@14: foreach ($service as $key => $value) { Chris@17: if (\is_string($key) && ('' === $key || '$' !== $key[0])) { Chris@14: return false; Chris@14: } Chris@14: } Chris@14: Chris@14: return true; Chris@14: } Chris@14: Chris@14: /** Chris@0: * Parses a definition. Chris@0: * Chris@0: * @param string $id Chris@0: * @param array|string $service Chris@0: * @param string $file Chris@14: * @param array $defaults Chris@0: * Chris@0: * @throws InvalidArgumentException When tags are invalid Chris@0: */ Chris@14: private function parseDefinition($id, $service, $file, array $defaults) Chris@0: { Chris@14: if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) { Chris@14: @trigger_error(sprintf('Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "%s" service or define it in XML instead.', $id), E_USER_DEPRECATED); Chris@14: } Chris@17: if (\is_string($service) && 0 === strpos($service, '@')) { Chris@14: $this->container->setAlias($id, $alias = new Alias(substr($service, 1))); Chris@14: if (isset($defaults['public'])) { Chris@14: $alias->setPublic($defaults['public']); Chris@14: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@17: if (\is_array($service) && $this->isUsingShortSyntax($service)) { Chris@17: $service = ['arguments' => $service]; Chris@14: } Chris@14: Chris@14: if (null === $service) { Chris@17: $service = []; Chris@14: } Chris@14: Chris@17: if (!\is_array($service)) { Chris@17: throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', \gettype($service), $id, $file)); Chris@0: } Chris@0: Chris@14: $this->checkDefinition($id, $service, $file); Chris@0: Chris@0: if (isset($service['alias'])) { Chris@14: $this->container->setAlias($id, $alias = new Alias($service['alias'])); Chris@18: if (\array_key_exists('public', $service)) { Chris@14: $alias->setPublic($service['public']); Chris@14: } elseif (isset($defaults['public'])) { Chris@14: $alias->setPublic($defaults['public']); Chris@14: } Chris@0: Chris@0: foreach ($service as $key => $value) { Chris@17: if (!\in_array($key, ['alias', 'public'])) { Chris@0: @trigger_error(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@14: if ($this->isLoadingInstanceof) { Chris@14: $definition = new ChildDefinition(''); Chris@14: } elseif (isset($service['parent'])) { Chris@14: if (!empty($this->instanceof)) { Chris@14: throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "_instanceof" configuration is defined as using both is not supported. Move your child definitions to a separate file.', $id)); Chris@14: } Chris@14: Chris@14: foreach ($defaults as $k => $v) { Chris@14: if ('tags' === $k) { Chris@14: // since tags are never inherited from parents, there is no confusion Chris@14: // thus we can safely add them as defaults to ChildDefinition Chris@14: continue; Chris@14: } Chris@14: if ('bind' === $k) { Chris@14: throw new InvalidArgumentException(sprintf('Attribute "bind" on service "%s" cannot be inherited from "_defaults" when a "parent" is set. Move your child definitions to a separate file.', $id)); Chris@14: } Chris@14: if (!isset($service[$k])) { Chris@14: throw new InvalidArgumentException(sprintf('Attribute "%s" on service "%s" cannot be inherited from "_defaults" when a "parent" is set. Move your child definitions to a separate file or define this attribute explicitly.', $k, $id)); Chris@14: } Chris@14: } Chris@14: Chris@14: $definition = new ChildDefinition($service['parent']); Chris@0: } else { Chris@0: $definition = new Definition(); Chris@14: Chris@14: if (isset($defaults['public'])) { Chris@14: $definition->setPublic($defaults['public']); Chris@14: } Chris@14: if (isset($defaults['autowire'])) { Chris@14: $definition->setAutowired($defaults['autowire']); Chris@14: } Chris@14: if (isset($defaults['autoconfigure'])) { Chris@14: $definition->setAutoconfigured($defaults['autoconfigure']); Chris@14: } Chris@14: Chris@17: $definition->setChanges([]); Chris@0: } Chris@0: Chris@0: if (isset($service['class'])) { Chris@0: $definition->setClass($service['class']); Chris@0: } Chris@0: Chris@0: if (isset($service['shared'])) { Chris@0: $definition->setShared($service['shared']); Chris@0: } Chris@0: Chris@0: if (isset($service['synthetic'])) { Chris@0: $definition->setSynthetic($service['synthetic']); Chris@0: } Chris@0: Chris@0: if (isset($service['lazy'])) { Chris@0: $definition->setLazy($service['lazy']); Chris@0: } Chris@0: Chris@0: if (isset($service['public'])) { Chris@0: $definition->setPublic($service['public']); Chris@0: } Chris@0: Chris@0: if (isset($service['abstract'])) { Chris@0: $definition->setAbstract($service['abstract']); Chris@0: } Chris@0: Chris@18: if (\array_key_exists('deprecated', $service)) { Chris@0: $definition->setDeprecated(true, $service['deprecated']); Chris@0: } Chris@0: Chris@0: if (isset($service['factory'])) { Chris@0: $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file)); Chris@0: } Chris@0: Chris@0: if (isset($service['file'])) { Chris@0: $definition->setFile($service['file']); Chris@0: } Chris@0: Chris@0: if (isset($service['arguments'])) { Chris@14: $definition->setArguments($this->resolveServices($service['arguments'], $file)); Chris@0: } Chris@0: Chris@0: if (isset($service['properties'])) { Chris@14: $definition->setProperties($this->resolveServices($service['properties'], $file)); Chris@0: } Chris@0: Chris@0: if (isset($service['configurator'])) { Chris@0: $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file)); Chris@0: } Chris@0: Chris@0: if (isset($service['calls'])) { Chris@17: if (!\is_array($service['calls'])) { Chris@0: throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@0: } Chris@0: Chris@0: foreach ($service['calls'] as $call) { Chris@0: if (isset($call['method'])) { Chris@0: $method = $call['method']; Chris@17: $args = isset($call['arguments']) ? $this->resolveServices($call['arguments'], $file) : []; Chris@0: } else { Chris@0: $method = $call[0]; Chris@17: $args = isset($call[1]) ? $this->resolveServices($call[1], $file) : []; Chris@0: } Chris@0: Chris@17: if (!\is_array($args)) { Chris@14: throw new InvalidArgumentException(sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in %s. Check your YAML syntax.', $method, $id, $file)); Chris@14: } Chris@0: $definition->addMethodCall($method, $args); Chris@0: } Chris@0: } Chris@0: Chris@17: $tags = isset($service['tags']) ? $service['tags'] : []; Chris@17: if (!\is_array($tags)) { Chris@14: throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@14: Chris@14: if (isset($defaults['tags'])) { Chris@14: $tags = array_merge($tags, $defaults['tags']); Chris@14: } Chris@14: Chris@14: foreach ($tags as $tag) { Chris@17: if (!\is_array($tag)) { Chris@17: $tag = ['name' => $tag]; Chris@0: } Chris@0: Chris@14: if (!isset($tag['name'])) { Chris@14: throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); Chris@14: } Chris@14: $name = $tag['name']; Chris@14: unset($tag['name']); Chris@14: Chris@17: if (!\is_string($name) || '' === $name) { Chris@14: throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); Chris@14: } Chris@14: Chris@14: foreach ($tag as $attribute => $value) { Chris@14: if (!is_scalar($value) && null !== $value) { Chris@14: throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file)); Chris@0: } Chris@14: } Chris@0: Chris@14: $definition->addTag($name, $tag); Chris@0: } Chris@0: Chris@0: if (isset($service['decorates'])) { Chris@0: if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) { Chris@0: throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1))); Chris@0: } Chris@0: Chris@0: $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null; Chris@0: $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0; Chris@0: $definition->setDecoratedService($service['decorates'], $renameId, $priority); Chris@0: } Chris@0: Chris@0: if (isset($service['autowire'])) { Chris@0: $definition->setAutowired($service['autowire']); Chris@0: } Chris@0: Chris@0: if (isset($service['autowiring_types'])) { Chris@17: if (\is_string($service['autowiring_types'])) { Chris@0: $definition->addAutowiringType($service['autowiring_types']); Chris@0: } else { Chris@17: if (!\is_array($service['autowiring_types'])) { Chris@0: throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@0: } Chris@0: Chris@0: foreach ($service['autowiring_types'] as $autowiringType) { Chris@17: if (!\is_string($autowiringType)) { Chris@0: throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@0: } Chris@0: Chris@0: $definition->addAutowiringType($autowiringType); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@14: if (isset($defaults['bind']) || isset($service['bind'])) { Chris@14: // deep clone, to avoid multiple process of the same instance in the passes Chris@17: $bindings = isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : []; Chris@14: Chris@14: if (isset($service['bind'])) { Chris@17: if (!\is_array($service['bind'])) { Chris@14: throw new InvalidArgumentException(sprintf('Parameter "bind" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@14: Chris@14: $bindings = array_merge($bindings, $this->resolveServices($service['bind'], $file)); Chris@14: } Chris@14: Chris@14: $definition->setBindings($bindings); Chris@14: } Chris@14: Chris@14: if (isset($service['autoconfigure'])) { Chris@14: if (!$definition instanceof ChildDefinition) { Chris@14: $definition->setAutoconfigured($service['autoconfigure']); Chris@14: } elseif ($service['autoconfigure']) { Chris@14: throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting "autoconfigure: false" for the service.', $id)); Chris@14: } Chris@14: } Chris@14: Chris@18: if (\array_key_exists('namespace', $service) && !\array_key_exists('resource', $service)) { Chris@14: throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@14: Chris@18: if (\array_key_exists('resource', $service)) { Chris@17: if (!\is_string($service['resource'])) { Chris@14: throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@14: } Chris@14: $exclude = isset($service['exclude']) ? $service['exclude'] : null; Chris@14: $namespace = isset($service['namespace']) ? $service['namespace'] : $id; Chris@14: $this->registerClasses($definition, $namespace, $service['resource'], $exclude); Chris@14: } else { Chris@14: $this->setDefinition($id, $definition); Chris@14: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a callable. Chris@0: * Chris@0: * @param string|array $callable A callable Chris@0: * @param string $parameter A parameter (e.g. 'factory' or 'configurator') Chris@0: * @param string $id A service identifier Chris@0: * @param string $file A parsed file Chris@0: * Chris@17: * @throws InvalidArgumentException When errors occur Chris@0: * Chris@0: * @return string|array A parsed callable Chris@0: */ Chris@0: private function parseCallable($callable, $parameter, $id, $file) Chris@0: { Chris@17: if (\is_string($callable)) { Chris@0: if ('' !== $callable && '@' === $callable[0]) { Chris@0: throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $parameter, $id, $callable, substr($callable, 1))); Chris@0: } Chris@0: Chris@0: if (false !== strpos($callable, ':') && false === strpos($callable, '::')) { Chris@0: $parts = explode(':', $callable); Chris@0: Chris@17: return [$this->resolveServices('@'.$parts[0], $file), $parts[1]]; Chris@0: } Chris@0: Chris@0: return $callable; Chris@0: } Chris@0: Chris@17: if (\is_array($callable)) { Chris@0: if (isset($callable[0]) && isset($callable[1])) { Chris@17: return [$this->resolveServices($callable[0], $file), $callable[1]]; Chris@14: } Chris@14: Chris@14: if ('factory' === $parameter && isset($callable[1]) && null === $callable[0]) { Chris@14: return $callable; Chris@0: } Chris@0: Chris@0: throw new InvalidArgumentException(sprintf('Parameter "%s" must contain an array with two elements for service "%s" in %s. Check your YAML syntax.', $parameter, $id, $file)); Chris@0: } Chris@0: Chris@0: throw new InvalidArgumentException(sprintf('Parameter "%s" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $parameter, $id, $file)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads a YAML file. Chris@0: * Chris@0: * @param string $file Chris@0: * Chris@0: * @return array The file content Chris@0: * Chris@0: * @throws InvalidArgumentException when the given file is not a local file or when it does not exist Chris@0: */ Chris@0: protected function loadFile($file) Chris@0: { Chris@0: if (!class_exists('Symfony\Component\Yaml\Parser')) { Chris@0: throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.'); Chris@0: } Chris@0: Chris@0: if (!stream_is_local($file)) { Chris@0: throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); Chris@0: } Chris@0: Chris@0: if (!file_exists($file)) { Chris@0: throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file)); Chris@0: } Chris@0: Chris@0: if (null === $this->yamlParser) { Chris@0: $this->yamlParser = new YamlParser(); Chris@0: } Chris@0: Chris@14: $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) { Chris@14: $message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message; Chris@14: Chris@14: return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false; Chris@14: }); Chris@14: Chris@0: try { Chris@14: $configuration = $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS); Chris@0: } catch (ParseException $e) { Chris@17: throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: %s', $file, $e->getMessage()), 0, $e); Chris@14: } finally { Chris@14: restore_error_handler(); Chris@0: } Chris@0: Chris@0: return $this->validate($configuration, $file); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validates a YAML file. Chris@0: * Chris@0: * @param mixed $content Chris@0: * @param string $file Chris@0: * Chris@0: * @return array Chris@0: * Chris@0: * @throws InvalidArgumentException When service file is not valid Chris@0: */ Chris@0: private function validate($content, $file) Chris@0: { Chris@0: if (null === $content) { Chris@0: return $content; Chris@0: } Chris@0: Chris@17: if (!\is_array($content)) { Chris@0: throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)); Chris@0: } Chris@0: Chris@0: foreach ($content as $namespace => $data) { Chris@17: if (\in_array($namespace, ['imports', 'parameters', 'services'])) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (!$this->container->hasExtension($namespace)) { Chris@0: $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions())); Chris@17: throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s', $namespace, $file, $namespace, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none')); Chris@0: } Chris@0: } Chris@0: Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves services. Chris@0: * Chris@14: * @param mixed $value Chris@14: * @param string $file Chris@14: * @param bool $isParameter Chris@0: * Chris@14: * @return array|string|Reference|ArgumentInterface Chris@0: */ Chris@14: private function resolveServices($value, $file, $isParameter = false) Chris@0: { Chris@14: if ($value instanceof TaggedValue) { Chris@14: $argument = $value->getValue(); Chris@14: if ('iterator' === $value->getTag()) { Chris@17: if (!\is_array($argument)) { Chris@14: throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".', $file)); Chris@14: } Chris@14: $argument = $this->resolveServices($argument, $file, $isParameter); Chris@14: try { Chris@14: return new IteratorArgument($argument); Chris@14: } catch (InvalidArgumentException $e) { Chris@14: throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts arrays of "@service" references in "%s".', $file)); Chris@14: } Chris@14: } Chris@14: if ('tagged' === $value->getTag()) { Chris@17: if (!\is_string($argument) || !$argument) { Chris@14: throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts non empty string in "%s".', $file)); Chris@14: } Chris@14: Chris@14: return new TaggedIteratorArgument($argument); Chris@14: } Chris@14: if ('service' === $value->getTag()) { Chris@14: if ($isParameter) { Chris@14: throw new InvalidArgumentException(sprintf('Using an anonymous service in a parameter is not allowed in "%s".', $file)); Chris@14: } Chris@14: Chris@14: $isLoadingInstanceof = $this->isLoadingInstanceof; Chris@14: $this->isLoadingInstanceof = false; Chris@14: $instanceof = $this->instanceof; Chris@17: $this->instanceof = []; Chris@14: Chris@14: $id = sprintf('%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', isset($argument['class']) ? $argument['class'] : '').$this->anonymousServicesSuffix); Chris@17: $this->parseDefinition($id, $argument, $file, []); Chris@14: Chris@14: if (!$this->container->hasDefinition($id)) { Chris@14: throw new InvalidArgumentException(sprintf('Creating an alias using the tag "!service" is not allowed in "%s".', $file)); Chris@14: } Chris@14: Chris@14: $this->container->getDefinition($id)->setPublic(false); Chris@14: Chris@14: $this->isLoadingInstanceof = $isLoadingInstanceof; Chris@14: $this->instanceof = $instanceof; Chris@14: Chris@14: return new Reference($id); Chris@14: } Chris@14: Chris@14: throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag())); Chris@14: } Chris@14: Chris@17: if (\is_array($value)) { Chris@14: foreach ($value as $k => $v) { Chris@14: $value[$k] = $this->resolveServices($v, $file, $isParameter); Chris@14: } Chris@17: } elseif (\is_string($value) && 0 === strpos($value, '@=')) { Chris@14: if (!class_exists(Expression::class)) { Chris@14: throw new \LogicException(sprintf('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".')); Chris@14: } Chris@14: Chris@0: return new Expression(substr($value, 2)); Chris@17: } elseif (\is_string($value) && 0 === strpos($value, '@')) { Chris@0: if (0 === strpos($value, '@@')) { Chris@0: $value = substr($value, 1); Chris@0: $invalidBehavior = null; Chris@14: } elseif (0 === strpos($value, '@!')) { Chris@14: $value = substr($value, 2); Chris@14: $invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE; Chris@0: } elseif (0 === strpos($value, '@?')) { Chris@0: $value = substr($value, 2); Chris@0: $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; Chris@0: } else { Chris@0: $value = substr($value, 1); Chris@0: $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; Chris@0: } Chris@0: Chris@0: if ('=' === substr($value, -1)) { Chris@14: @trigger_error(sprintf('The "=" suffix that used to disable strict references in Symfony 2.x is deprecated since Symfony 3.3 and will be unsupported in 4.0. Remove it in "%s".', $value), E_USER_DEPRECATED); Chris@0: $value = substr($value, 0, -1); Chris@0: } Chris@0: Chris@0: if (null !== $invalidBehavior) { Chris@0: $value = new Reference($value, $invalidBehavior); Chris@0: } Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads from Extensions. Chris@0: */ Chris@0: private function loadFromExtensions(array $content) Chris@0: { Chris@0: foreach ($content as $namespace => $values) { Chris@17: if (\in_array($namespace, ['imports', 'parameters', 'services'])) { Chris@0: continue; Chris@0: } Chris@0: Chris@17: if (!\is_array($values) && null !== $values) { Chris@17: $values = []; Chris@0: } Chris@0: Chris@0: $this->container->loadFromExtension($namespace, $values); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks the keywords used to define a service. Chris@0: * Chris@0: * @param string $id The service name Chris@0: * @param array $definition The service definition to check Chris@0: * @param string $file The loaded YAML file Chris@0: */ Chris@14: private function checkDefinition($id, array $definition, $file) Chris@0: { Chris@14: if ($throw = $this->isLoadingInstanceof) { Chris@14: $keywords = self::$instanceofKeywords; Chris@14: } elseif ($throw = (isset($definition['resource']) || isset($definition['namespace']))) { Chris@14: $keywords = self::$prototypeKeywords; Chris@14: } else { Chris@14: $keywords = self::$serviceKeywords; Chris@14: } Chris@14: Chris@0: foreach ($definition as $key => $value) { Chris@14: if (!isset($keywords[$key])) { Chris@14: if ($throw) { Chris@14: throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords))); Chris@14: } Chris@14: Chris@14: @trigger_error(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s". The YamlFileLoader object will raise an exception instead in Symfony 4.0 when detecting an unsupported service configuration key.', $key, $id, $file, implode('", "', $keywords)), E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: } Chris@0: }