2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Loader;
|
|
|
|
|
2017-01-17 13:46:23 +00:00
|
|
|
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2017-01-14 11:46:46 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
|
2012-08-07 08:39:30 +01:00
|
|
|
use Symfony\Component\Config\FileLocatorInterface;
|
2017-01-14 11:46:46 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\Finder\Glob;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-01-04 14:26:20 +00:00
|
|
|
/**
|
|
|
|
* FileLoader is the abstract class used by all built-in loaders that are file based.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-01-04 14:26:20 +00:00
|
|
|
*/
|
2011-02-10 15:15:51 +00:00
|
|
|
abstract class FileLoader extends BaseFileLoader
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-02-10 15:15:51 +00:00
|
|
|
protected $container;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2014-10-13 20:09:32 +01:00
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance
|
|
|
|
* @param FileLocatorInterface $locator A FileLocator instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-08-07 08:39:30 +01:00
|
|
|
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2011-02-10 15:15:51 +00:00
|
|
|
$this->container = $container;
|
2010-07-16 08:12:58 +01:00
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
parent::__construct($locator);
|
2010-07-18 10:26:47 +01:00
|
|
|
}
|
2017-01-14 11:46:46 +00:00
|
|
|
|
2017-01-17 13:46:23 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
foreach ($this->glob($resource, false) as $path => $info) {
|
|
|
|
parent::import($path, $type, $ignoreErrors, $sourceResource);
|
|
|
|
}
|
|
|
|
} catch (FileLocatorFileNotFoundException $e) {
|
|
|
|
if (!$ignoreErrors) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 11:46:46 +00:00
|
|
|
/**
|
|
|
|
* Registers a set of classes as services using PSR-4 for discovery.
|
|
|
|
*
|
|
|
|
* @param Definition $prototype A definition to use as template
|
|
|
|
* @param string $namespace The namespace prefix of classes in the scanned directory
|
|
|
|
* @param string $resource The directory to look for classes, glob-patterns allowed
|
|
|
|
*
|
|
|
|
* @experimental in version 3.3
|
|
|
|
*/
|
|
|
|
public function registerClasses(Definition $prototype, $namespace, $resource)
|
|
|
|
{
|
|
|
|
if ('\\' !== substr($namespace, -1)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace));
|
|
|
|
}
|
|
|
|
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes = $this->findClasses($namespace, $resource);
|
|
|
|
// prepare for deep cloning
|
|
|
|
$prototype = serialize($prototype);
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$this->container->setDefinition($class, unserialize($prototype));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findClasses($namespace, $resource)
|
|
|
|
{
|
|
|
|
$classes = array();
|
|
|
|
$extRegexp = defined('HHVM_VERSION') ? '/\\.(?:php|hh)$/' : '/\\.php$/';
|
|
|
|
|
|
|
|
foreach ($this->glob($resource, true, $prefixLen) as $path => $info) {
|
2017-01-17 13:46:23 +00:00
|
|
|
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
|
2017-01-14 11:46:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -strlen($m[0]))), '\\');
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$r = $this->container->getReflectionClass($class, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$r->isInterface() && !$r->isTrait()) {
|
|
|
|
$classes[] = $class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function glob($resource, $recursive, &$prefixLen = null)
|
|
|
|
{
|
|
|
|
if (strlen($resource) === $i = strcspn($resource, '*?{[')) {
|
2017-01-17 13:46:23 +00:00
|
|
|
if (!$recursive) {
|
|
|
|
yield $resource => new \SplFileInfo($resource);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2017-01-14 11:46:46 +00:00
|
|
|
$resourcePrefix = $resource;
|
|
|
|
$resource = '';
|
|
|
|
} elseif (0 === $i) {
|
|
|
|
$resourcePrefix = '.';
|
|
|
|
$resource = '/'.$resource;
|
|
|
|
} else {
|
|
|
|
$resourcePrefix = dirname(substr($resource, 0, 1 + $i));
|
|
|
|
$resource = substr($resource, strlen($resourcePrefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
$resourcePrefix = $this->locator->locate($resourcePrefix, $this->currentDir, true);
|
|
|
|
$resourcePrefix = realpath($resourcePrefix) ?: $resourcePrefix;
|
|
|
|
$prefixLen = strlen($resourcePrefix);
|
|
|
|
|
|
|
|
// track directories only for new & removed files
|
|
|
|
$this->container->fileExists($resourcePrefix, '/^$/');
|
|
|
|
|
|
|
|
if (false === strpos($resource, '/**/') && (defined('GLOB_BRACE') || false === strpos($resource, '{'))) {
|
|
|
|
foreach (glob($resourcePrefix.$resource, defined('GLOB_BRACE') ? GLOB_BRACE : 0) as $path) {
|
|
|
|
if ($recursive && is_dir($path)) {
|
|
|
|
$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS;
|
|
|
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $flags)) as $path => $info) {
|
2017-01-17 13:46:23 +00:00
|
|
|
if ($info->isFile()) {
|
|
|
|
yield $path => $info;
|
|
|
|
}
|
2017-01-14 11:46:46 +00:00
|
|
|
}
|
2017-01-17 13:46:23 +00:00
|
|
|
} elseif (is_file($path)) {
|
2017-01-14 11:46:46 +00:00
|
|
|
yield $path => new \SplFileInfo($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists(Finder::class)) {
|
|
|
|
throw new LogicException(sprintf('Extended glob pattern "%s" cannot be used as the Finder component is not installed.', $resource));
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
|
|
|
$regex = Glob::toRegex($resource);
|
|
|
|
if ($recursive) {
|
|
|
|
$regex = substr_replace($regex, '(/|$)', -2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($finder->followLinks()->in($resourcePrefix) as $path => $info) {
|
2017-01-17 13:46:23 +00:00
|
|
|
if (preg_match($regex, substr($path, $prefixLen)) && $info->isFile()) {
|
2017-01-14 11:46:46 +00:00
|
|
|
yield $path => $info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|