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\DefinitionDecorator; Chris@0: use Symfony\Component\DependencyInjection\Alias; Chris@0: use Symfony\Component\DependencyInjection\ContainerInterface; Chris@0: use Symfony\Component\DependencyInjection\Definition; Chris@0: use Symfony\Component\DependencyInjection\Reference; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@0: use Symfony\Component\Config\Resource\FileResource; Chris@0: use Symfony\Component\Yaml\Exception\ParseException; Chris@0: use Symfony\Component\Yaml\Parser as YamlParser; Chris@0: use Symfony\Component\Yaml\Yaml; Chris@0: use Symfony\Component\ExpressionLanguage\Expression; Chris@0: Chris@0: /** Chris@0: * YamlFileLoader loads YAML files service definitions. Chris@0: * Chris@0: * The YAML format does not support anonymous services (cf. the XML loader). Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class YamlFileLoader extends FileLoader Chris@0: { Chris@0: private static $keywords = array( 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@0: ); Chris@0: Chris@0: private $yamlParser; Chris@0: 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@0: $this->container->addResource(new FileResource($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@0: if (!is_array($content['parameters'])) { Chris@0: throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $resource)); Chris@0: } Chris@0: Chris@0: foreach ($content['parameters'] as $key => $value) { Chris@0: $this->container->setParameter($key, $this->resolveServices($value)); Chris@0: } Chris@0: } Chris@0: Chris@0: // extensions Chris@0: $this->loadFromExtensions($content); Chris@0: Chris@0: // services Chris@0: $this->parseDefinitions($content, $resource); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supports($resource, $type = null) Chris@0: { Chris@0: return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), 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@0: 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@0: $defaultDirectory = dirname($file); Chris@0: foreach ($content['imports'] as $import) { Chris@0: if (!is_array($import)) { Chris@0: throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file)); Chris@0: } Chris@0: Chris@0: $this->setCurrentDir($defaultDirectory); Chris@0: $this->import($import['resource'], 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@0: 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@0: foreach ($content['services'] as $id => $service) { Chris@0: $this->parseDefinition($id, $service, $file); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a definition. Chris@0: * Chris@0: * @param string $id Chris@0: * @param array|string $service Chris@0: * @param string $file Chris@0: * Chris@0: * @throws InvalidArgumentException When tags are invalid Chris@0: */ Chris@0: private function parseDefinition($id, $service, $file) Chris@0: { Chris@0: if (is_string($service) && 0 === strpos($service, '@')) { Chris@0: $this->container->setAlias($id, substr($service, 1)); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!is_array($service)) { Chris@0: 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@0: static::checkDefinition($id, $service, $file); Chris@0: Chris@0: if (isset($service['alias'])) { Chris@0: $public = !array_key_exists('public', $service) || (bool) $service['public']; Chris@0: $this->container->setAlias($id, new Alias($service['alias'], $public)); Chris@0: Chris@0: foreach ($service as $key => $value) { Chris@0: if (!in_array($key, array('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@0: if (isset($service['parent'])) { Chris@0: $definition = new DefinitionDecorator($service['parent']); Chris@0: } else { Chris@0: $definition = new Definition(); 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@0: 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@0: $definition->setArguments($this->resolveServices($service['arguments'])); Chris@0: } Chris@0: Chris@0: if (isset($service['properties'])) { Chris@0: $definition->setProperties($this->resolveServices($service['properties'])); 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@0: 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@0: $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array(); Chris@0: } else { Chris@0: $method = $call[0]; Chris@0: $args = isset($call[1]) ? $this->resolveServices($call[1]) : array(); Chris@0: } Chris@0: Chris@0: $definition->addMethodCall($method, $args); Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($service['tags'])) { Chris@0: if (!is_array($service['tags'])) { Chris@0: throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@0: } Chris@0: Chris@0: foreach ($service['tags'] as $tag) { Chris@0: if (!is_array($tag)) { Chris@0: throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file)); Chris@0: } Chris@0: Chris@0: if (!isset($tag['name'])) { Chris@0: throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file)); Chris@0: } Chris@0: Chris@0: if (!is_string($tag['name']) || '' === $tag['name']) { Chris@0: throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file)); Chris@0: } Chris@0: Chris@0: $name = $tag['name']; Chris@0: unset($tag['name']); Chris@0: Chris@0: foreach ($tag as $attribute => $value) { Chris@0: if (!is_scalar($value) && null !== $value) { Chris@0: 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@0: } Chris@0: Chris@0: $definition->addTag($name, $tag); Chris@0: } 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@0: if (is_string($service['autowiring_types'])) { Chris@0: $definition->addAutowiringType($service['autowiring_types']); Chris@0: } else { Chris@0: 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@0: 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@0: $this->container->setDefinition($id, $definition); 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@0: * @throws InvalidArgumentException When errors are occuried Chris@0: * Chris@0: * @return string|array A parsed callable Chris@0: */ Chris@0: private function parseCallable($callable, $parameter, $id, $file) Chris@0: { Chris@0: 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@0: return array($this->resolveServices('@'.$parts[0]), $parts[1]); Chris@0: } Chris@0: Chris@0: return $callable; Chris@0: } Chris@0: Chris@0: if (is_array($callable)) { Chris@0: if (isset($callable[0]) && isset($callable[1])) { Chris@0: return array($this->resolveServices($callable[0]), $callable[1]); 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@0: try { Chris@0: $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT); Chris@0: } catch (ParseException $e) { Chris@0: throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); 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@0: 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@0: if (in_array($namespace, array('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@0: throw new InvalidArgumentException(sprintf( Chris@0: 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s', Chris@0: $namespace, Chris@0: $file, Chris@0: $namespace, Chris@0: $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none' Chris@0: )); Chris@0: } Chris@0: } Chris@0: Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves services. Chris@0: * Chris@0: * @param string|array $value Chris@0: * Chris@0: * @return array|string|Reference Chris@0: */ Chris@0: private function resolveServices($value) Chris@0: { Chris@0: if (is_array($value)) { Chris@0: $value = array_map(array($this, 'resolveServices'), $value); Chris@0: } elseif (is_string($value) && 0 === strpos($value, '@=')) { Chris@0: return new Expression(substr($value, 2)); Chris@0: } elseif (is_string($value) && 0 === strpos($value, '@')) { Chris@0: if (0 === strpos($value, '@@')) { Chris@0: $value = substr($value, 1); Chris@0: $invalidBehavior = null; 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@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: * @param array $content Chris@0: */ Chris@0: private function loadFromExtensions(array $content) Chris@0: { Chris@0: foreach ($content as $namespace => $values) { Chris@0: if (in_array($namespace, array('imports', 'parameters', 'services'))) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (!is_array($values)) { Chris@0: $values = array(); 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@0: private static function checkDefinition($id, array $definition, $file) Chris@0: { Chris@0: foreach ($definition as $key => $value) { Chris@0: if (!isset(static::$keywords[$key])) { Chris@0: @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('", "', static::$keywords)), E_USER_DEPRECATED); Chris@0: // @deprecated Uncomment the following statement in Symfony 4.0 Chris@0: // and also update the corresponding unit test to make it expect Chris@0: // an InvalidArgumentException exception. Chris@0: //throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', static::$keywords))); Chris@0: } Chris@0: } Chris@0: } Chris@0: }