-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null) | ||
{ | ||
|
@@ -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(); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
$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; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right, I completely missed this one. My bad There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/ | ||||
|
There was a problem hiding this comment.
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 bestring|null
in 3.4)