Skip to content

[DI][DX] Allow exclude to be an array of patterns #24428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
/**
* 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
* @param string $exclude A globed path of files to exclude
* @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
* @param string|string[] $exclude A globed path of files to exclude or an array of globed paths of files to exclude
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right type should actually be string|string[]|null (the phpdoc should be string|null in 3.4)

*/
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
{
Expand All @@ -54,7 +54,7 @@ public function registerClasses(Definition $prototype, $namespace, $resource, $e
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
}

$classes = $this->findClasses($namespace, $resource, $exclude);
$classes = $this->findClasses($namespace, $resource, (array) $exclude);
// prepare for deep cloning
$serializedPrototype = serialize($prototype);
$interfaces = array();
Expand Down Expand Up @@ -101,21 +101,22 @@ protected function setDefinition($id, Definition $definition)
}
}

private function findClasses($namespace, $pattern, $excludePattern)
private function findClasses($namespace, $pattern, array $excludePatterns)
{
$parameterBag = $this->container->getParameterBag();

$excludePaths = array();
$excludePrefix = null;
if ($excludePattern) {
$excludePattern = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePattern));
foreach ($this->glob($excludePattern, true, $resource) as $path => $info) {
if (null === $excludePrefix) {
$excludePrefix = $resource->getPrefix();
if ($excludePatterns) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could remove this diff, handling the empty array like any other array (the string|null type was needing a dedicated handling of null before)

$excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
foreach ($excludePatterns as $excludePattern) {
foreach ($this->glob($excludePattern, true, $resource) as $path => $info) {
if (null === $excludePrefix) {
$excludePrefix = $resource->getPrefix();
}
// normalize Windows slashes
$excludePaths[str_replace('\\', '/', $path)] = true;
}

// normalize Windows slashes
$excludePaths[str_replace('\\', '/', $path)] = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ public function testMissingParentClass()
);
}

public function testRegisterClassesWithExcludeAsArray()
{
$container = new ContainerBuilder();
$container->setParameter('sub_dir', 'Sub');
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));

$loader->registerClasses(
new Definition(),
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
'Prototype/*', array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileLoader::registerClasses docblock must be updated, right?

* @param string $exclude A globed path of files to exclude

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, I completely missed this one. My bad

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock updated

'Prototype/%sub_dir%',
'Prototype/OtherDir/AnotherSub/DeeperBaz.php',
)
);

$this->assertTrue($container->has(Foo::class));
$this->assertTrue($container->has(Baz::class));
$this->assertFalse($container->has(Bar::class));
$this->assertFalse($container->has(DeeperBaz::class));
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegExp /Expected to find class "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Prototype\\Bar" in file ".+" while importing services from resource "Prototype\/Sub\/\*", but it was not found\! Check the namespace prefix used with the resource/
Expand Down