Skip to content

Commit 4730c41

Browse files
bug #26600 [Routing] Fixed the importing of files using glob patterns that match multiple resources (skalpa)
This PR was squashed before being merged into the 3.4 branch (closes #26600). Discussion ---------- [Routing] Fixed the importing of files using glob patterns that match multiple resources | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22160 | License | MIT | Doc PR | n/a This fixes the import of resources specified using glob patterns in `XmlFileLoader` and `YamlFileLoader`. @nicolas-grekas This supersedes #25633 that's been in limbo since December despite your comments, so I decided to take care of it as I need this to work. I took care of the two loaders that are affected, and added tests. Commits ------- 948b4cf [Routing] Fixed the importing of files using glob patterns that match multiple resources
2 parents 3e4e92c + 0778af1 commit 4730c41

17 files changed

+190
-38
lines changed

Loader/Configurator/RoutingConfigurator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
3939
final public function import($resource, $type = null, $ignoreErrors = false)
4040
{
4141
$this->loader->setCurrentDir(dirname($this->path));
42-
$subCollection = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
42+
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
43+
if (!is_array($imported)) {
44+
return new ImportConfigurator($this->collection, $imported);
45+
}
4346

44-
return new ImportConfigurator($this->collection, $subCollection);
47+
$mergedCollection = new RouteCollection();
48+
foreach ($imported as $subCollection) {
49+
$mergedCollection->addCollection($subCollection);
50+
}
51+
52+
return new ImportConfigurator($this->collection, $mergedCollection);
4553
}
4654

4755
/**

Loader/XmlFileLoader.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,33 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
146146

147147
$this->setCurrentDir(dirname($path));
148148

149-
$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
150-
/* @var $subCollection RouteCollection */
151-
$subCollection->addPrefix($prefix);
152-
if (null !== $host) {
153-
$subCollection->setHost($host);
154-
}
155-
if (null !== $condition) {
156-
$subCollection->setCondition($condition);
157-
}
158-
if (null !== $schemes) {
159-
$subCollection->setSchemes($schemes);
160-
}
161-
if (null !== $methods) {
162-
$subCollection->setMethods($methods);
149+
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file);
150+
151+
if (!is_array($imported)) {
152+
$imported = array($imported);
163153
}
164-
$subCollection->addDefaults($defaults);
165-
$subCollection->addRequirements($requirements);
166-
$subCollection->addOptions($options);
167154

168-
$collection->addCollection($subCollection);
155+
foreach ($imported as $subCollection) {
156+
/* @var $subCollection RouteCollection */
157+
$subCollection->addPrefix($prefix);
158+
if (null !== $host) {
159+
$subCollection->setHost($host);
160+
}
161+
if (null !== $condition) {
162+
$subCollection->setCondition($condition);
163+
}
164+
if (null !== $schemes) {
165+
$subCollection->setSchemes($schemes);
166+
}
167+
if (null !== $methods) {
168+
$subCollection->setMethods($methods);
169+
}
170+
$subCollection->addDefaults($defaults);
171+
$subCollection->addRequirements($requirements);
172+
$subCollection->addOptions($options);
173+
174+
$collection->addCollection($subCollection);
175+
}
169176
}
170177

171178
/**

Loader/YamlFileLoader.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,33 @@ protected function parseImport(RouteCollection $collection, array $config, $path
158158

159159
$this->setCurrentDir(dirname($path));
160160

161-
$subCollection = $this->import($config['resource'], $type, false, $file);
162-
/* @var $subCollection RouteCollection */
163-
$subCollection->addPrefix($prefix);
164-
if (null !== $host) {
165-
$subCollection->setHost($host);
166-
}
167-
if (null !== $condition) {
168-
$subCollection->setCondition($condition);
169-
}
170-
if (null !== $schemes) {
171-
$subCollection->setSchemes($schemes);
172-
}
173-
if (null !== $methods) {
174-
$subCollection->setMethods($methods);
161+
$imported = $this->import($config['resource'], $type, false, $file);
162+
163+
if (!is_array($imported)) {
164+
$imported = array($imported);
175165
}
176-
$subCollection->addDefaults($defaults);
177-
$subCollection->addRequirements($requirements);
178-
$subCollection->addOptions($options);
179166

180-
$collection->addCollection($subCollection);
167+
foreach ($imported as $subCollection) {
168+
/* @var $subCollection RouteCollection */
169+
$subCollection->addPrefix($prefix);
170+
if (null !== $host) {
171+
$subCollection->setHost($host);
172+
}
173+
if (null !== $condition) {
174+
$subCollection->setCondition($condition);
175+
}
176+
if (null !== $schemes) {
177+
$subCollection->setSchemes($schemes);
178+
}
179+
if (null !== $methods) {
180+
$subCollection->setMethods($methods);
181+
}
182+
$subCollection->addDefaults($defaults);
183+
$subCollection->addRequirements($requirements);
184+
$subCollection->addOptions($options);
185+
186+
$collection->addCollection($subCollection);
187+
}
181188
}
182189

183190
/**

Tests/Fixtures/glob/bar.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="bar_route" path="/bar" controller="AppBundle:Bar:view" />
8+
</routes>

Tests/Fixtures/glob/bar.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bar_route:
2+
path: /bar
3+
defaults:
4+
_controller: AppBundle:Bar:view

Tests/Fixtures/glob/baz.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="baz_route" path="/baz" controller="AppBundle:Baz:view" />
8+
</routes>

Tests/Fixtures/glob/baz.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
baz_route:
2+
path: /baz
3+
defaults:
4+
_controller: AppBundle:Baz:view
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<import resource="ba?.xml" />
8+
</routes>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_static:
2+
resource: ba?.yml

Tests/Fixtures/glob/import_single.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<import resource="b?r.xml" />
8+
</routes>

0 commit comments

Comments
 (0)