Skip to content

Commit 54b0653

Browse files
committed
Allowing prototype/PSR4 service loading ability to exclude, because glob doesn't support this
1 parent 3646d08 commit 54b0653

File tree

9 files changed

+53
-7
lines changed

9 files changed

+53
-7
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
4747
* @param Definition $prototype A definition to use as template
4848
* @param string $namespace The namespace prefix of classes in the scanned directory
4949
* @param string $resource The directory to look for classes, glob-patterns allowed
50+
* @param string $exclude Regular expression of paths to exclude
5051
*/
51-
public function registerClasses(Definition $prototype, $namespace, $resource)
52+
public function registerClasses(Definition $prototype, $namespace, $resource, $exclude = null)
5253
{
5354
if ('\\' !== substr($namespace, -1)) {
5455
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": %s.', $namespace));
@@ -57,7 +58,7 @@ public function registerClasses(Definition $prototype, $namespace, $resource)
5758
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: %s.', $namespace));
5859
}
5960

60-
$classes = $this->findClasses($namespace, $resource);
61+
$classes = $this->findClasses($namespace, $resource, $exclude);
6162
// prepare for deep cloning
6263
$prototype = serialize($prototype);
6364

@@ -84,7 +85,7 @@ protected function setDefinition($id, Definition $definition)
8485
}
8586
}
8687

87-
private function findClasses($namespace, $pattern)
88+
private function findClasses($namespace, $pattern, $excludePattern)
8889
{
8990
$parameterBag = $this->container->getParameterBag();
9091
$pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
@@ -96,9 +97,15 @@ private function findClasses($namespace, $pattern)
9697
$prefixLen = strlen($resource->getPrefix());
9798
}
9899

100+
// match against the "local" path relative to the prefix
101+
if ($excludePattern && preg_match('#'.$excludePattern.'#', substr($path, $prefixLen + 1))) {
102+
continue;
103+
}
104+
99105
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
100106
continue;
101107
}
108+
102109
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -strlen($m[0]))), '\\');
103110

104111
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)) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private function parseDefinitions(\DOMDocument $xml, $file, $defaults)
143143
foreach ($services as $service) {
144144
if (null !== $definition = $this->parseDefinition($service, $file, $defaults)) {
145145
if ('prototype' === $service->tagName) {
146-
$this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'));
146+
$this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'), (string) $service->getAttribute('exclude'));
147147
} else {
148148
$this->setDefinition((string) $service->getAttribute('id'), $definition);
149149
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class YamlFileLoader extends FileLoader
6161

6262
private static $prototypeKeywords = array(
6363
'resource' => 'resource',
64+
'exclude' => 'exclude',
6465
'parent' => 'parent',
6566
'shared' => 'shared',
6667
'lazy' => 'lazy',
@@ -528,7 +529,8 @@ private function parseDefinition($id, $service, $file, array $defaults)
528529
if (!is_string($service['resource'])) {
529530
throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
530531
}
531-
$this->registerClasses($definition, $id, $service['resource']);
532+
$exclude = isset($service['exclude']) ? $service['exclude'] : null;
533+
$this->registerClasses($definition, $id, $service['resource'], $exclude);
532534
} else {
533535
$this->setDefinition($id, $definition);
534536
}

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
</xsd:choice>
162162
<xsd:attribute name="namespace" type="xsd:string" use="required" />
163163
<xsd:attribute name="resource" type="xsd:string" use="required" />
164+
<xsd:attribute name="exclude" type="xsd:string" />
164165
<xsd:attribute name="shared" type="boolean" />
165166
<xsd:attribute name="public" type="boolean" />
166167
<xsd:attribute name="lazy" type="boolean" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\AnotherSub;
4+
5+
class DeeperBaz
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir;
4+
5+
class Baz
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
33
<services>
4-
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" />
4+
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="^OtherDir" />
55
</services>
66
</container>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
services:
22
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
33
resource: ../Prototype
4+
exclude: '^OtherDir'

src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2323
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2424
use Symfony\Component\DependencyInjection\Reference;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\AnotherSub\DeeperBaz;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Baz;
2527
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar;
2628

2729
class FileLoaderTest extends TestCase
@@ -82,11 +84,30 @@ public function testRegisterClasses()
8284
$container->setParameter('sub_dir', 'Sub');
8385
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
8486

85-
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
87+
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*', '');
8688

8789
$this->assertTrue($container->has(Bar::class));
8890
}
8991

92+
public function testRegisterClassesWithExclude()
93+
{
94+
$container = new ContainerBuilder();
95+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
96+
97+
$loader->registerClasses(
98+
new Definition(),
99+
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
100+
'Prototype/*',
101+
// load everything, except for OtherDir/AnotherSub & Foo.ph
102+
'^(OtherDir/AnotherSub|Foo\.php)'
103+
);
104+
105+
$this->assertTrue($container->has(Bar::class));
106+
$this->assertTrue($container->has(Baz::class));
107+
$this->assertFalse($container->has(Foo::class));
108+
$this->assertFalse($container->has(DeeperBaz::class));
109+
}
110+
90111
/**
91112
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
92113
* @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/

0 commit comments

Comments
 (0)