use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
+use Symfony\Component\Yaml\Yaml;
use Symfony\Component\ExpressionLanguage\Expression;
/**
*/
class YamlFileLoader extends FileLoader
{
+ private static $keywords = array(
+ 'alias' => 'alias',
+ 'parent' => 'parent',
+ 'class' => 'class',
+ 'shared' => 'shared',
+ 'synthetic' => 'synthetic',
+ 'lazy' => 'lazy',
+ 'public' => 'public',
+ 'abstract' => 'abstract',
+ 'deprecated' => 'deprecated',
+ 'factory' => 'factory',
+ 'file' => 'file',
+ 'arguments' => 'arguments',
+ 'properties' => 'properties',
+ 'configurator' => 'configurator',
+ 'calls' => 'calls',
+ 'tags' => 'tags',
+ 'decorates' => 'decorates',
+ 'decoration_inner_name' => 'decoration_inner_name',
+ 'decoration_priority' => 'decoration_priority',
+ 'autowire' => 'autowire',
+ 'autowiring_types' => 'autowiring_types',
+ );
+
private $yamlParser;
/**
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));
}
+ static::checkDefinition($id, $service, $file);
+
if (isset($service['alias'])) {
$public = !array_key_exists('public', $service) || (bool) $service['public'];
$this->container->setAlias($id, new Alias($service['alias'], $public));
+ foreach ($service as $key => $value) {
+ if (!in_array($key, array('alias', 'public'))) {
+ @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);
+ }
+ }
+
return;
}
$definition->setShared($service['shared']);
}
- if (isset($service['scope'])) {
- if ('request' !== $id) {
- @trigger_error(sprintf('The "scope" key of service "%s" in file "%s" is deprecated since version 2.8 and will be removed in 3.0.', $id, $file), E_USER_DEPRECATED);
- }
- $definition->setScope($service['scope'], false);
- }
-
if (isset($service['synthetic'])) {
$definition->setSynthetic($service['synthetic']);
}
- if (isset($service['synchronized'])) {
- @trigger_error(sprintf('The "synchronized" key of service "%s" in file "%s" is deprecated since version 2.7 and will be removed in 3.0.', $id, $file), E_USER_DEPRECATED);
- $definition->setSynchronized($service['synchronized'], 'request' !== $id);
- }
-
if (isset($service['lazy'])) {
$definition->setLazy($service['lazy']);
}
}
if (isset($service['factory'])) {
- if (is_string($service['factory'])) {
- if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) {
- $parts = explode(':', $service['factory']);
- $definition->setFactory(array($this->resolveServices('@'.$parts[0]), $parts[1]));
- } else {
- $definition->setFactory($service['factory']);
- }
- } else {
- $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1]));
- }
- }
-
- if (isset($service['factory_class'])) {
- @trigger_error(sprintf('The "factory_class" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
- $definition->setFactoryClass($service['factory_class']);
- }
-
- if (isset($service['factory_method'])) {
- @trigger_error(sprintf('The "factory_method" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
- $definition->setFactoryMethod($service['factory_method']);
- }
-
- if (isset($service['factory_service'])) {
- @trigger_error(sprintf('The "factory_service" key of service "%s" in file "%s" is deprecated since version 2.6 and will be removed in 3.0. Use "factory" instead.', $id, $file), E_USER_DEPRECATED);
- $definition->setFactoryService($service['factory_service']);
+ $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
}
if (isset($service['file'])) {
}
if (isset($service['configurator'])) {
- if (is_string($service['configurator'])) {
- $definition->setConfigurator($service['configurator']);
- } else {
- $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
- }
+ $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file));
}
if (isset($service['calls'])) {
$this->container->setDefinition($id, $definition);
}
+ /**
+ * Parses a callable.
+ *
+ * @param string|array $callable A callable
+ * @param string $parameter A parameter (e.g. 'factory' or 'configurator')
+ * @param string $id A service identifier
+ * @param string $file A parsed file
+ *
+ * @throws InvalidArgumentException When errors are occuried
+ *
+ * @return string|array A parsed callable
+ */
+ private function parseCallable($callable, $parameter, $id, $file)
+ {
+ if (is_string($callable)) {
+ if ('' !== $callable && '@' === $callable[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)));
+ }
+
+ if (false !== strpos($callable, ':') && false === strpos($callable, '::')) {
+ $parts = explode(':', $callable);
+
+ return array($this->resolveServices('@'.$parts[0]), $parts[1]);
+ }
+
+ return $callable;
+ }
+
+ if (is_array($callable)) {
+ if (isset($callable[0]) && isset($callable[1])) {
+ return array($this->resolveServices($callable[0]), $callable[1]);
+ }
+
+ 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));
+ }
+
+ 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));
+ }
+
/**
* Loads a YAML file.
*
}
try {
- $configuration = $this->yamlParser->parse(file_get_contents($file));
+ $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
} catch (ParseException $e) {
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
if ('=' === substr($value, -1)) {
$value = substr($value, 0, -1);
- $strict = false;
- } else {
- $strict = true;
}
if (null !== $invalidBehavior) {
- $value = new Reference($value, $invalidBehavior, $strict);
+ $value = new Reference($value, $invalidBehavior);
}
}
$this->container->loadFromExtension($namespace, $values);
}
}
+
+ /**
+ * Checks the keywords used to define a service.
+ *
+ * @param string $id The service name
+ * @param array $definition The service definition to check
+ * @param string $file The loaded YAML file
+ */
+ private static function checkDefinition($id, array $definition, $file)
+ {
+ foreach ($definition as $key => $value) {
+ if (!isset(static::$keywords[$key])) {
+ @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);
+ // @deprecated Uncomment the following statement in Symfony 4.0
+ // and also update the corresponding unit test to make it expect
+ // an InvalidArgumentException exception.
+ //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)));
+ }
+ }
+ }
}