Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Loader; Chris@0: Chris@17: use Symfony\Component\Config\FileLocatorInterface; Chris@17: use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader; Chris@17: use Symfony\Component\Config\Resource\GlobResource; Chris@14: use Symfony\Component\DependencyInjection\ChildDefinition; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@14: use Symfony\Component\DependencyInjection\Definition; Chris@14: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * FileLoader is the abstract class used by all built-in loaders that are file based. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class FileLoader extends BaseFileLoader Chris@0: { Chris@0: protected $container; Chris@14: protected $isLoadingInstanceof = false; Chris@17: protected $instanceof = []; Chris@0: Chris@0: public function __construct(ContainerBuilder $container, FileLocatorInterface $locator) Chris@0: { Chris@0: $this->container = $container; Chris@0: Chris@0: parent::__construct($locator); Chris@0: } Chris@14: Chris@14: /** Chris@14: * Registers a set of classes as services using PSR-4 for discovery. Chris@14: * Chris@14: * @param Definition $prototype A definition to use as template Chris@14: * @param string $namespace The namespace prefix of classes in the scanned directory Chris@14: * @param string $resource The directory to look for classes, glob-patterns allowed Chris@14: * @param string $exclude A globed path of files to exclude Chris@14: */ Chris@14: public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null) Chris@14: { Chris@14: if ('\\' !== substr($namespace, -1)) { Chris@14: throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace)); Chris@14: } Chris@14: if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) { Chris@14: throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace)); Chris@14: } Chris@14: Chris@14: $classes = $this->findClasses($namespace, $resource, $exclude); Chris@14: // prepare for deep cloning Chris@14: $serializedPrototype = serialize($prototype); Chris@17: $interfaces = []; Chris@17: $singlyImplemented = []; Chris@14: Chris@14: foreach ($classes as $class => $errorMessage) { Chris@14: if (interface_exists($class, false)) { Chris@14: $interfaces[] = $class; Chris@14: } else { Chris@14: $this->setDefinition($class, $definition = unserialize($serializedPrototype)); Chris@14: if (null !== $errorMessage) { Chris@14: $definition->addError($errorMessage); Chris@14: Chris@14: continue; Chris@14: } Chris@14: foreach (class_implements($class, false) as $interface) { Chris@14: $singlyImplemented[$interface] = isset($singlyImplemented[$interface]) ? false : $class; Chris@14: } Chris@14: } Chris@14: } Chris@14: foreach ($interfaces as $interface) { Chris@14: if (!empty($singlyImplemented[$interface])) { Chris@14: $this->container->setAlias($interface, $singlyImplemented[$interface]) Chris@14: ->setPublic(false); Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * Registers a definition in the container with its instanceof-conditionals. Chris@14: * Chris@14: * @param string $id Chris@14: * @param Definition $definition Chris@14: */ Chris@14: protected function setDefinition($id, Definition $definition) Chris@14: { Chris@18: $this->container->removeBindings($id); Chris@18: Chris@14: if ($this->isLoadingInstanceof) { Chris@14: if (!$definition instanceof ChildDefinition) { Chris@17: throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition))); Chris@14: } Chris@14: $this->instanceof[$id] = $definition; Chris@14: } else { Chris@14: $this->container->setDefinition($id, $definition instanceof ChildDefinition ? $definition : $definition->setInstanceofConditionals($this->instanceof)); Chris@14: } Chris@14: } Chris@14: Chris@14: private function findClasses($namespace, $pattern, $excludePattern) Chris@14: { Chris@14: $parameterBag = $this->container->getParameterBag(); Chris@14: Chris@17: $excludePaths = []; Chris@14: $excludePrefix = null; Chris@14: if ($excludePattern) { Chris@14: $excludePattern = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePattern)); Chris@14: foreach ($this->glob($excludePattern, true, $resource) as $path => $info) { Chris@14: if (null === $excludePrefix) { Chris@14: $excludePrefix = $resource->getPrefix(); Chris@14: } Chris@14: Chris@14: // normalize Windows slashes Chris@14: $excludePaths[str_replace('\\', '/', $path)] = true; Chris@14: } Chris@14: } Chris@14: Chris@14: $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern)); Chris@17: $classes = []; Chris@17: $extRegexp = \defined('HHVM_VERSION') ? '/\\.(?:php|hh)$/' : '/\\.php$/'; Chris@14: $prefixLen = null; Chris@14: foreach ($this->glob($pattern, true, $resource) as $path => $info) { Chris@14: if (null === $prefixLen) { Chris@17: $prefixLen = \strlen($resource->getPrefix()); Chris@14: Chris@14: if ($excludePrefix && 0 !== strpos($excludePrefix, $resource->getPrefix())) { Chris@14: 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: } Chris@14: } Chris@14: Chris@14: if (isset($excludePaths[str_replace('\\', '/', $path)])) { Chris@14: continue; Chris@14: } Chris@14: Chris@14: if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) { Chris@14: continue; Chris@14: } Chris@17: $class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\'); Chris@14: Chris@14: 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: continue; Chris@14: } Chris@14: Chris@14: try { Chris@14: $r = $this->container->getReflectionClass($class); Chris@14: } catch (\ReflectionException $e) { Chris@14: $classes[$class] = sprintf( Chris@14: 'While discovering services from namespace "%s", an error was thrown when processing the class "%s": "%s".', Chris@14: $namespace, Chris@14: $class, Chris@14: $e->getMessage() Chris@14: ); Chris@14: continue; Chris@14: } Chris@14: // check to make sure the expected class exists Chris@14: if (!$r) { Chris@14: 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: } Chris@14: Chris@14: if ($r->isInstantiable() || $r->isInterface()) { Chris@14: $classes[$class] = null; Chris@14: } Chris@14: } Chris@14: Chris@14: // track only for new & removed files Chris@14: if ($resource instanceof GlobResource) { Chris@14: $this->container->addResource($resource); Chris@14: } else { Chris@14: foreach ($resource as $path) { Chris@14: $this->container->fileExists($path, false); Chris@14: } Chris@14: } Chris@14: Chris@14: return $classes; Chris@14: } Chris@0: }