4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Loader;
14 use Symfony\Component\Config\FileLocatorInterface;
15 use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
16 use Symfony\Component\Config\Resource\GlobResource;
17 use Symfony\Component\DependencyInjection\ChildDefinition;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\DependencyInjection\Definition;
20 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
23 * FileLoader is the abstract class used by all built-in loaders that are file based.
25 * @author Fabien Potencier <fabien@symfony.com>
27 abstract class FileLoader extends BaseFileLoader
30 protected $isLoadingInstanceof = false;
31 protected $instanceof = array();
33 public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
35 $this->container = $container;
37 parent::__construct($locator);
41 * Registers a set of classes as services using PSR-4 for discovery.
43 * @param Definition $prototype A definition to use as template
44 * @param string $namespace The namespace prefix of classes in the scanned directory
45 * @param string $resource The directory to look for classes, glob-patterns allowed
46 * @param string $exclude A globed path of files to exclude
48 public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
50 if ('\\' !== substr($namespace, -1)) {
51 throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace));
53 if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
54 throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
57 $classes = $this->findClasses($namespace, $resource, $exclude);
58 // prepare for deep cloning
59 $serializedPrototype = serialize($prototype);
60 $interfaces = array();
61 $singlyImplemented = array();
63 foreach ($classes as $class => $errorMessage) {
64 if (interface_exists($class, false)) {
65 $interfaces[] = $class;
67 $this->setDefinition($class, $definition = unserialize($serializedPrototype));
68 if (null !== $errorMessage) {
69 $definition->addError($errorMessage);
73 foreach (class_implements($class, false) as $interface) {
74 $singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
78 foreach ($interfaces as $interface) {
79 if (!empty($singlyImplemented[$interface])) {
80 $this->container->setAlias($interface, $singlyImplemented[$interface])
87 * Registers a definition in the container with its instanceof-conditionals.
90 * @param Definition $definition
92 protected function setDefinition($id, Definition $definition)
94 if ($this->isLoadingInstanceof) {
95 if (!$definition instanceof ChildDefinition) {
96 throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
98 $this->instanceof[$id] = $definition;
100 $this->container->setDefinition($id, $definition instanceof ChildDefinition ? $definition : $definition->setInstanceofConditionals($this->instanceof));
104 private function findClasses($namespace, $pattern, $excludePattern)
106 $parameterBag = $this->container->getParameterBag();
108 $excludePaths = array();
109 $excludePrefix = null;
110 if ($excludePattern) {
111 $excludePattern = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePattern));
112 foreach ($this->glob($excludePattern, true, $resource) as $path => $info) {
113 if (null === $excludePrefix) {
114 $excludePrefix = $resource->getPrefix();
117 // normalize Windows slashes
118 $excludePaths[str_replace('\\', '/', $path)] = true;
122 $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
124 $extRegexp = \defined('HHVM_VERSION') ? '/\\.(?:php|hh)$/' : '/\\.php$/';
126 foreach ($this->glob($pattern, true, $resource) as $path => $info) {
127 if (null === $prefixLen) {
128 $prefixLen = \strlen($resource->getPrefix());
130 if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
131 throw new InvalidArgumentException(sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s)', $namespace, $excludePattern, $pattern));
135 if (isset($excludePaths[str_replace('\\', '/', $path)])) {
139 if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
142 $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
144 if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
149 $r = $this->container->getReflectionClass($class);
150 } catch (\ReflectionException $e) {
151 $classes[$class] = sprintf(
152 'While discovering services from namespace "%s", an error was thrown when processing the class "%s": "%s".',
159 // check to make sure the expected class exists
161 throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
164 if ($r->isInstantiable() || $r->isInterface()) {
165 $classes[$class] = null;
169 // track only for new & removed files
170 if ($resource instanceof GlobResource) {
171 $this->container->addResource($resource);
173 foreach ($resource as $path) {
174 $this->container->fileExists($path, false);