PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

https://github.com/Taluu/symfony
PHP | 245 lines | 203 code | 16 blank | 26 comment | 12 complexity | dc58fb3ef2be8e7d15cf29f94f05d2df MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader;
  11. use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
  12. use Symfony\Component\Config\Exception\LoaderLoadException;
  13. use Symfony\Component\Config\FileLocatorInterface;
  14. use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
  15. use Symfony\Component\Config\Loader\Loader;
  16. use Symfony\Component\Config\Resource\GlobResource;
  17. use Symfony\Component\DependencyInjection\Attribute\When;
  18. use Symfony\Component\DependencyInjection\ChildDefinition;
  19. use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass;
  20. use Symfony\Component\DependencyInjection\ContainerBuilder;
  21. use Symfony\Component\DependencyInjection\Definition;
  22. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  23. /**
  24. * FileLoader is the abstract class used by all built-in loaders that are file based.
  25. *
  26. * @author Fabien Potencier <fabien@symfony.com>
  27. */
  28. abstract class FileLoader extends BaseFileLoader
  29. {
  30. public const ANONYMOUS_ID_REGEXP = '/^\.\d+_[^~]*+~[._a-zA-Z\d]{7}$/';
  31. protected $container;
  32. protected $isLoadingInstanceof = false;
  33. protected $instanceof = [];
  34. protected $interfaces = [];
  35. protected $singlyImplemented = [];
  36. protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
  37. public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
  38. {
  39. $this->container = $container;
  40. parent::__construct($locator, $env);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found
  46. */
  47. public function import($resource, string $type = null, $ignoreErrors = false, string $sourceResource = null, $exclude = null)
  48. {
  49. $args = \func_get_args();
  50. if ($ignoreNotFound = 'not_found' === $ignoreErrors) {
  51. $args[2] = false;
  52. } elseif (!\is_bool($ignoreErrors)) {
  53. throw new \TypeError(sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, get_debug_type($ignoreErrors)));
  54. }
  55. try {
  56. parent::import(...$args);
  57. } catch (LoaderLoadException $e) {
  58. if (!$ignoreNotFound || !($prev = $e->getPrevious()) instanceof FileLocatorFileNotFoundException) {
  59. throw $e;
  60. }
  61. foreach ($prev->getTrace() as $frame) {
  62. if ('import' === ($frame['function'] ?? null) && is_a($frame['class'] ?? '', Loader::class, true)) {
  63. break;
  64. }
  65. }
  66. if (__FILE__ !== $frame['file']) {
  67. throw $e;
  68. }
  69. }
  70. }
  71. /**
  72. * Registers a set of classes as services using PSR-4 for discovery.
  73. *
  74. * @param Definition $prototype A definition to use as template
  75. * @param string $namespace The namespace prefix of classes in the scanned directory
  76. * @param string $resource The directory to look for classes, glob-patterns allowed
  77. * @param string|string[]|null $exclude A globbed path of files to exclude or an array of globbed paths of files to exclude
  78. */
  79. public function registerClasses(Definition $prototype, string $namespace, string $resource, $exclude = null)
  80. {
  81. if ('\\' !== substr($namespace, -1)) {
  82. throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
  83. }
  84. if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
  85. throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
  86. }
  87. $autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
  88. $autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null;
  89. $classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes);
  90. // prepare for deep cloning
  91. $serializedPrototype = serialize($prototype);
  92. foreach ($classes as $class => $errorMessage) {
  93. if (null === $errorMessage && $autoconfigureAttributes && $this->env) {
  94. $r = $this->container->getReflectionClass($class);
  95. $attribute = null;
  96. foreach ($r->getAttributes(When::class) as $attribute) {
  97. if ($this->env === $attribute->newInstance()->env) {
  98. $attribute = null;
  99. break;
  100. }
  101. }
  102. if (null !== $attribute) {
  103. continue;
  104. }
  105. }
  106. if (interface_exists($class, false)) {
  107. $this->interfaces[] = $class;
  108. } else {
  109. $this->setDefinition($class, $definition = unserialize($serializedPrototype));
  110. if (null !== $errorMessage) {
  111. $definition->addError($errorMessage);
  112. continue;
  113. }
  114. foreach (class_implements($class, false) as $interface) {
  115. $this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
  116. }
  117. }
  118. }
  119. if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
  120. $this->registerAliasesForSinglyImplementedInterfaces();
  121. }
  122. }
  123. public function registerAliasesForSinglyImplementedInterfaces()
  124. {
  125. foreach ($this->interfaces as $interface) {
  126. if (!empty($this->singlyImplemented[$interface]) && !$this->container->has($interface)) {
  127. $this->container->setAlias($interface, $this->singlyImplemented[$interface]);
  128. }
  129. }
  130. $this->interfaces = $this->singlyImplemented = [];
  131. }
  132. /**
  133. * Registers a definition in the container with its instanceof-conditionals.
  134. */
  135. protected function setDefinition(string $id, Definition $definition)
  136. {
  137. $this->container->removeBindings($id);
  138. if ($this->isLoadingInstanceof) {
  139. if (!$definition instanceof ChildDefinition) {
  140. throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));
  141. }
  142. $this->instanceof[$id] = $definition;
  143. } else {
  144. $this->container->setDefinition($id, $definition->setInstanceofConditionals($this->instanceof));
  145. }
  146. }
  147. private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes): array
  148. {
  149. $parameterBag = $this->container->getParameterBag();
  150. $excludePaths = [];
  151. $excludePrefix = null;
  152. $excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
  153. foreach ($excludePatterns as $excludePattern) {
  154. foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) {
  155. if (null === $excludePrefix) {
  156. $excludePrefix = $resource->getPrefix();
  157. }
  158. // normalize Windows slashes and remove trailing slashes
  159. $excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
  160. }
  161. }
  162. $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
  163. $classes = [];
  164. $extRegexp = '/\\.php$/';
  165. $prefixLen = null;
  166. foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
  167. if (null === $prefixLen) {
  168. $prefixLen = \strlen($resource->getPrefix());
  169. if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) {
  170. 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));
  171. }
  172. }
  173. if (isset($excludePaths[str_replace('\\', '/', $path)])) {
  174. continue;
  175. }
  176. if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
  177. continue;
  178. }
  179. $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
  180. 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)) {
  181. continue;
  182. }
  183. try {
  184. $r = $this->container->getReflectionClass($class);
  185. } catch (\ReflectionException $e) {
  186. $classes[$class] = $e->getMessage();
  187. continue;
  188. }
  189. // check to make sure the expected class exists
  190. if (!$r) {
  191. 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));
  192. }
  193. if ($r->isInstantiable() || $r->isInterface()) {
  194. $classes[$class] = null;
  195. }
  196. if ($autoconfigureAttributes && !$r->isInstantiable()) {
  197. $autoconfigureAttributes->processClass($this->container, $r);
  198. }
  199. }
  200. // track only for new & removed files
  201. if ($resource instanceof GlobResource) {
  202. $this->container->addResource($resource);
  203. } else {
  204. foreach ($resource as $path) {
  205. $this->container->fileExists($path, false);
  206. }
  207. }
  208. return $classes;
  209. }
  210. }