Skip to content

Commit 8ac2ebc

Browse files
feature #31587 [Routing][Config] Allow patterns of resources to be excluded from config loading (tristanbes)
This PR was merged into the 4.4 branch. Discussion ---------- [Routing][Config] Allow patterns of resources to be excluded from config loading | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #31516 | License | MIT | Doc PR | not yet The PR will fix the following RFC: #31516 Like resource loading for services, this PR offers a way to exclude patterns of resources like: ```yml // config/routes/annotations.yaml controllers: resource: ../../src/Controller/* type: annotation exclude: '../src/Controller/{DebugEmailController}.php' ``` All the annotation routes inside `Controller/` will be loaded in this example except all the one present inside the `Controller/DebugEmailController.php` Commits ------- 332ff8811c [Routing][Config] Allow patterns of resources to be excluded from config loading
2 parents 28b1d19 + e26c708 commit 8ac2ebc

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecated `ServiceRouterLoader` in favor of `ContainerLoader`.
88
* Deprecated `ObjectRouteLoader` in favor of `ObjectLoader`.
9+
* Added a way to exclude patterns of resources from being imported by the `import()` method
910

1011
4.3.0
1112
-----

Loader/Configurator/RoutingConfigurator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
3333
$this->file = $file;
3434
}
3535

36-
final public function import($resource, string $type = null, bool $ignoreErrors = false): ImportConfigurator
36+
/**
37+
* @param string|string[]|null $exclude Glob patterns to exclude from the import
38+
*/
39+
final public function import($resource, string $type = null, bool $ignoreErrors = false, $exclude = null): ImportConfigurator
3740
{
3841
$this->loader->setCurrentDir(\dirname($this->path));
39-
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file) ?: [];
4042

43+
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: [];
4144
if (!\is_array($imported)) {
4245
return new ImportConfigurator($this->collection, $imported);
4346
}

Loader/XmlFileLoader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,24 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
163163
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "prefix" attribute and <prefix> child nodes.', $path));
164164
}
165165

166+
$exclude = [];
167+
foreach ($node->childNodes as $child) {
168+
if ($child instanceof \DOMElement && $child->localName === $exclude && self::NAMESPACE_URI === $child->namespaceURI) {
169+
$exclude[] = $child->nodeValue;
170+
}
171+
}
172+
173+
if ($node->hasAttribute('exclude')) {
174+
if ($exclude) {
175+
throw new \InvalidArgumentException('You cannot use both the attribute "exclude" and <exclude> tags at the same time.');
176+
}
177+
$exclude = [$node->getAttribute('exclude')];
178+
}
179+
166180
$this->setCurrentDir(\dirname($path));
167181

168182
/** @var RouteCollection[] $imported */
169-
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file) ?: [];
183+
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file, $exclude) ?: [];
170184

171185
if (!\is_array($imported)) {
172186
$imported = [$imported];

Loader/YamlFileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class YamlFileLoader extends FileLoader
2929
{
3030
private static $availableKeys = [
31-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8',
31+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude',
3232
];
3333
private $yamlParser;
3434

@@ -169,6 +169,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
169169
$schemes = isset($config['schemes']) ? $config['schemes'] : null;
170170
$methods = isset($config['methods']) ? $config['methods'] : null;
171171
$trailingSlashOnRoot = $config['trailing_slash_on_root'] ?? true;
172+
$exclude = $config['exclude'] ?? null;
172173

173174
if (isset($config['controller'])) {
174175
$defaults['_controller'] = $config['controller'];
@@ -185,7 +186,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
185186

186187
$this->setCurrentDir(\dirname($path));
187188

188-
$imported = $this->import($config['resource'], $type, false, $file) ?: [];
189+
$imported = $this->import($config['resource'], $type, false, $file, $exclude) ?: [];
189190

190191
if (!\is_array($imported)) {
191192
$imported = [$imported];

Loader/schema/routing/routing-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@
6161
<xsd:sequence maxOccurs="unbounded" minOccurs="0">
6262
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
6363
<xsd:element name="prefix" type="localized-path" minOccurs="0" maxOccurs="unbounded" />
64+
<xsd:element name="exclude" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
6465
</xsd:sequence>
6566
<xsd:attribute name="resource" type="xsd:string" use="required" />
6667
<xsd:attribute name="type" type="xsd:string" />
68+
<xsd:attribute name="exclude" type="xsd:string" />
6769
<xsd:attribute name="prefix" type="xsd:string" />
6870
<xsd:attribute name="name-prefix" type="xsd:string" />
6971
<xsd:attribute name="host" type="xsd:string" />

Tests/Loader/GlobFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testLoadAddsTheGlobResourceToTheContainer()
3838

3939
class GlobFileLoaderWithoutImport extends GlobFileLoader
4040
{
41-
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
41+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null, $exclude = null)
4242
{
4343
return new RouteCollection();
4444
}

0 commit comments

Comments
 (0)