annotate vendor/symfony/dependency-injection/Loader/FileLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Loader;
Chris@0 13
Chris@17 14 use Symfony\Component\Config\FileLocatorInterface;
Chris@17 15 use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
Chris@17 16 use Symfony\Component\Config\Resource\GlobResource;
Chris@14 17 use Symfony\Component\DependencyInjection\ChildDefinition;
Chris@0 18 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@14 19 use Symfony\Component\DependencyInjection\Definition;
Chris@14 20 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * FileLoader is the abstract class used by all built-in loaders that are file based.
Chris@0 24 *
Chris@0 25 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 26 */
Chris@0 27 abstract class FileLoader extends BaseFileLoader
Chris@0 28 {
Chris@0 29 protected $container;
Chris@14 30 protected $isLoadingInstanceof = false;
Chris@17 31 protected $instanceof = [];
Chris@0 32
Chris@0 33 public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
Chris@0 34 {
Chris@0 35 $this->container = $container;
Chris@0 36
Chris@0 37 parent::__construct($locator);
Chris@0 38 }
Chris@14 39
Chris@14 40 /**
Chris@14 41 * Registers a set of classes as services using PSR-4 for discovery.
Chris@14 42 *
Chris@14 43 * @param Definition $prototype A definition to use as template
Chris@14 44 * @param string $namespace The namespace prefix of classes in the scanned directory
Chris@14 45 * @param string $resource The directory to look for classes, glob-patterns allowed
Chris@14 46 * @param string $exclude A globed path of files to exclude
Chris@14 47 */
Chris@14 48 public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
Chris@14 49 {
Chris@14 50 if ('\\' !== substr($namespace, -1)) {
Chris@14 51 throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace));
Chris@14 52 }
Chris@14 53 if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
Chris@14 54 throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
Chris@14 55 }
Chris@14 56
Chris@14 57 $classes = $this->findClasses($namespace, $resource, $exclude);
Chris@14 58 // prepare for deep cloning
Chris@14 59 $serializedPrototype = serialize($prototype);
Chris@17 60 $interfaces = [];
Chris@17 61 $singlyImplemented = [];
Chris@14 62
Chris@14 63 foreach ($classes as $class => $errorMessage) {
Chris@14 64 if (interface_exists($class, false)) {
Chris@14 65 $interfaces[] = $class;
Chris@14 66 } else {
Chris@14 67 $this->setDefinition($class, $definition = unserialize($serializedPrototype));
Chris@14 68 if (null !== $errorMessage) {
Chris@14 69 $definition->addError($errorMessage);
Chris@14 70
Chris@14 71 continue;
Chris@14 72 }
Chris@14 73 foreach (class_implements($class, false) as $interface) {
Chris@14 74 $singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class;
Chris@14 75 }
Chris@14 76 }
Chris@14 77 }
Chris@14 78 foreach ($interfaces as $interface) {
Chris@14 79 if (!empty($singlyImplemented[$interface])) {
Chris@14 80 $this->container->setAlias($interface, $singlyImplemented[$interface])
Chris@14 81 ->setPublic(false);
Chris@14 82 }
Chris@14 83 }
Chris@14 84 }
Chris@14 85
Chris@14 86 /**
Chris@14 87 * Registers a definition in the container with its instanceof-conditionals.
Chris@14 88 *
Chris@14 89 * @param string $id
Chris@14 90 * @param Definition $definition
Chris@14 91 */
Chris@14 92 protected function setDefinition($id, Definition $definition)
Chris@14 93 {
Chris@18 94 $this->container->removeBindings($id);
Chris@18 95
Chris@14 96 if ($this->isLoadingInstanceof) {
Chris@14 97 if (!$definition instanceof ChildDefinition) {
Chris@17 98 throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
Chris@14 99 }
Chris@14 100 $this->instanceof[$id] = $definition;
Chris@14 101 } else {
Chris@14 102 $this->container->setDefinition($id, $definition instanceof ChildDefinition ? $definition : $definition->setInstanceofConditionals($this->instanceof));
Chris@14 103 }
Chris@14 104 }
Chris@14 105
Chris@14 106 private function findClasses($namespace, $pattern, $excludePattern)
Chris@14 107 {
Chris@14 108 $parameterBag = $this->container->getParameterBag();
Chris@14 109
Chris@17 110 $excludePaths = [];
Chris@14 111 $excludePrefix = null;
Chris@14 112 if ($excludePattern) {
Chris@14 113 $excludePattern = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePattern));
Chris@14 114 foreach ($this->glob($excludePattern, true, $resource) as $path => $info) {
Chris@14 115 if (null === $excludePrefix) {
Chris@14 116 $excludePrefix = $resource->getPrefix();
Chris@14 117 }
Chris@14 118
Chris@14 119 // normalize Windows slashes
Chris@14 120 $excludePaths[str_replace('\\', '/', $path)] = true;
Chris@14 121 }
Chris@14 122 }
Chris@14 123
Chris@14 124 $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
Chris@17 125 $classes = [];
Chris@17 126 $extRegexp = \defined('HHVM_VERSION') ? '/\\.(?:php|hh)$/' : '/\\.php$/';
Chris@14 127 $prefixLen = null;
Chris@14 128 foreach ($this->glob($pattern, true, $resource) as $path => $info) {
Chris@14 129 if (null === $prefixLen) {
Chris@17 130 $prefixLen = \strlen($resource->getPrefix());
Chris@14 131
Chris@14 132 if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
Chris@14 133 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));
Chris@14 134 }
Chris@14 135 }
Chris@14 136
Chris@14 137 if (isset($excludePaths[str_replace('\\', '/', $path)])) {
Chris@14 138 continue;
Chris@14 139 }
Chris@14 140
Chris@14 141 if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
Chris@14 142 continue;
Chris@14 143 }
Chris@17 144 $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
Chris@14 145
Chris@14 146 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)) {
Chris@14 147 continue;
Chris@14 148 }
Chris@14 149
Chris@14 150 try {
Chris@14 151 $r = $this->container->getReflectionClass($class);
Chris@14 152 } catch (\ReflectionException $e) {
Chris@14 153 $classes[$class] = sprintf(
Chris@14 154 'While discovering services from namespace "%s", an error was thrown when processing the class "%s": "%s".',
Chris@14 155 $namespace,
Chris@14 156 $class,
Chris@14 157 $e->getMessage()
Chris@14 158 );
Chris@14 159 continue;
Chris@14 160 }
Chris@14 161 // check to make sure the expected class exists
Chris@14 162 if (!$r) {
Chris@14 163 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));
Chris@14 164 }
Chris@14 165
Chris@14 166 if ($r->isInstantiable() || $r->isInterface()) {
Chris@14 167 $classes[$class] = null;
Chris@14 168 }
Chris@14 169 }
Chris@14 170
Chris@14 171 // track only for new & removed files
Chris@14 172 if ($resource instanceof GlobResource) {
Chris@14 173 $this->container->addResource($resource);
Chris@14 174 } else {
Chris@14 175 foreach ($resource as $path) {
Chris@14 176 $this->container->fileExists($path, false);
Chris@14 177 }
Chris@14 178 }
Chris@14 179
Chris@14 180 return $classes;
Chris@14 181 }
Chris@0 182 }