Skip to content

Commit 917d4fb

Browse files
Merge branch '4.4'
* 4.4: (39 commits) [Console] Fix #33915, Detect dimensions using mode CON if vt100 is supported [PhpUnitBridge] Also search for composer.phar in git root folder [HttpKernel][DataCollectorInterface] Ease compatibility Add tests to ensure defaultLocale is properly passed to the URL generator [DependencyInjection] Fix broken references in tests [VarDumper] display the method we're in when dumping stack traces [HttpClient] Retry safe requests when then fail before the body arrives [Console] Rename some methods related to redraw frequency Avoid using of kernel after shutdown Simplify PHP CS Fixer configuration [PropertyInfo] Fixed type extraction for nullable collections of non-nullable elements [FrameworkBundle] [HttpKernel] fixed correct EOL and EOM month Fix CS [Serializer] Fix property name usage for denormalization Name test accordingly to the tested class Fix MockFileSessionStorageTest::sessionDir being used after it's unset [Security] Fix SwitchUserToken wrongly deauthenticated Supporting Bootstrap 4 custom switches Add new Form WeekType bumped Symfony version to 4.3.7 ...
2 parents 26c9f51 + 0ac14b0 commit 917d4fb

File tree

8 files changed

+84
-7
lines changed

8 files changed

+84
-7
lines changed

CHANGELOG.md

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

1616
* Deprecated `ServiceRouterLoader` in favor of `ContainerLoader`.
1717
* Deprecated `ObjectRouteLoader` in favor of `ObjectLoader`.
18+
* Added a way to exclude patterns of resources from being imported by the `import()` method
1819

1920
4.3.0
2021
-----

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, s
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, strin
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, strin
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" />

Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ function (ConfigCacheInterface $cache) {
325325
}
326326
);
327327

328-
$this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger);
328+
$this->generator = new $this->options['generator_class'](require $cache->getPath(), $this->context, $this->logger, $this->defaultLocale);
329329
}
330330

331331
if ($this->generator instanceof ConfigurableRequirementsInterface) {

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, string $type = null, bool $ignoreErrors = false, string $sourceResource = null)
41+
public function import($resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null, $exclude = null)
4242
{
4343
return new RouteCollection();
4444
}

Tests/RouterTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,26 @@ class RouterTest extends TestCase
2222

2323
private $loader = null;
2424

25+
private $cacheDir;
26+
2527
protected function setUp(): void
2628
{
2729
$this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
2830
$this->router = new Router($this->loader, 'routing.yml');
31+
32+
$this->cacheDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('router_', true);
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
if (is_dir($this->cacheDir)) {
38+
array_map('unlink', glob($this->cacheDir.\DIRECTORY_SEPARATOR.'*'));
39+
rmdir($this->cacheDir);
40+
}
41+
42+
$this->loader = null;
43+
$this->router = null;
44+
$this->cacheDir = null;
2945
}
3046

3147
public function testSetOptionsWithSupportedOptions()
@@ -132,4 +148,44 @@ public function testMatchRequestWithRequestMatcherInterface()
132148

133149
$this->router->matchRequest(Request::create('/'));
134150
}
151+
152+
public function testDefaultLocaleIsPassedToGeneratorClass()
153+
{
154+
$this->loader->expects($this->once())
155+
->method('load')->with('routing.yml', null)
156+
->willReturn(new RouteCollection());
157+
158+
$router = new Router($this->loader, 'routing.yml', [
159+
'cache_dir' => null,
160+
], null, null, 'hr');
161+
162+
$generator = $router->getGenerator();
163+
164+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
165+
166+
$p = new \ReflectionProperty($generator, 'defaultLocale');
167+
$p->setAccessible(true);
168+
169+
$this->assertSame('hr', $p->getValue($generator));
170+
}
171+
172+
public function testDefaultLocaleIsPassedToCompiledGeneratorCacheClass()
173+
{
174+
$this->loader->expects($this->once())
175+
->method('load')->with('routing.yml', null)
176+
->willReturn(new RouteCollection());
177+
178+
$router = new Router($this->loader, 'routing.yml', [
179+
'cache_dir' => $this->cacheDir,
180+
], null, null, 'hr');
181+
182+
$generator = $router->getGenerator();
183+
184+
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGeneratorInterface', $generator);
185+
186+
$p = new \ReflectionProperty($generator, 'defaultLocale');
187+
$p->setAccessible(true);
188+
189+
$this->assertSame('hr', $p->getValue($generator));
190+
}
135191
}

0 commit comments

Comments
 (0)