Chris@0: container = $container; Chris@0: $this->fileCache = FileCacheFactory::get('container_yaml_loader'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads a Yaml file. Chris@0: * Chris@0: * @param mixed $file Chris@0: * The resource Chris@0: */ Chris@0: public function load($file) Chris@0: { Chris@0: // Load from the file cache, fall back to loading the file. Chris@0: $content = $this->fileCache->get($file); Chris@0: if (!$content) { Chris@0: $content = $this->loadFile($file); Chris@0: $this->fileCache->set($file, $content); Chris@0: } Chris@0: Chris@0: // Not supported. 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: // Not supported. Chris@0: //$this->parseImports($content, $file); 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.', $file)); 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: // Not supported. Chris@0: //$this->loadFromExtensions($content); Chris@0: Chris@0: // services Chris@0: $this->parseDefinitions($content, $file); 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($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: // Some extensions split up their dependencies into multiple files. Chris@0: if (isset($content['_provider'])) { Chris@0: $provider = $content['_provider']; Chris@0: } Chris@0: else { Chris@0: $basename = basename($file); Chris@0: list($provider, ) = explode('.', $basename, 2); Chris@0: } Chris@0: foreach ($content['services'] as $id => $service) { Chris@0: $service['tags'][] = ['name' => '_provider', 'provider' => $provider]; 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 $service Chris@0: * @param string $file Chris@0: * Chris@0: * @throws InvalidArgumentException Chris@0: * 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: 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: return; Chris@0: } Chris@0: Chris@0: if (isset($service['parent'])) { Chris@14: $definition = new ChildDefinition($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: if (is_string($service['factory'])) { Chris@0: if (strpos($service['factory'], ':') !== false && strpos($service['factory'], '::') === false) { Chris@0: $parts = explode(':', $service['factory']); Chris@0: $definition->setFactory(array($this->resolveServices('@'.$parts[0]), $parts[1])); Chris@0: } else { Chris@0: $definition->setFactory($service['factory']); Chris@0: } Chris@0: } else { Chris@0: $definition->setFactory(array($this->resolveServices($service['factory'][0]), $service['factory'][1])); Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($service['factory_class'])) { Chris@0: $definition->setFactory($service['factory_class']); Chris@0: } Chris@0: Chris@0: if (isset($service['factory_method'])) { Chris@0: $definition->setFactory($service['factory_method']); Chris@0: } Chris@0: Chris@0: if (isset($service['factory_service'])) { Chris@0: $definition->setFactory($service['factory_service']); 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: if (is_string($service['configurator'])) { Chris@0: $definition->setConfigurator($service['configurator']); Chris@0: } else { Chris@0: $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1])); Chris@0: } 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: $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: $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: * 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 Chris@0: * 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 (!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 service file "%s" is not valid.', $file)); Chris@0: } Chris@0: Chris@0: return $this->validate(Yaml::decode(file_get_contents($file)), $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 Chris@0: * 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: if ($invalid_keys = array_diff_key($content, array('parameters' => 1, 'services' => 1))) { Chris@0: throw new InvalidArgumentException(sprintf('The service file "%s" is not valid: it contains invalid keys %s. Services have to be added under "services" and Parameters under "parameters".', $file, $invalid_keys)); 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: // Not supported. Chris@0: //return new Expression(substr($value, 2)); Chris@0: throw new InvalidArgumentException(sprintf("'%s' is an Expression, but expressions are not supported.", $value)); 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: $strict = false; Chris@0: } else { Chris@0: $strict = true; Chris@0: } Chris@0: Chris@0: if (null !== $invalidBehavior) { Chris@0: $value = new Reference($value, $invalidBehavior, $strict); Chris@0: } Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@0: }