Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Routing] Fixed the importing of files using glob patterns that match multiple resources #26600

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
final public function import($resource, $type = null, $ignoreErrors = false)
{
$this->loader->setCurrentDir(dirname($this->path));
$subCollection = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
if (!is_array($imported)) {
return new ImportConfigurator($this->collection, $imported);
}

return new ImportConfigurator($this->collection, $subCollection);
$mergedCollection = new RouteCollection();
foreach ($imported as $subCollection) {
$mergedCollection->addCollection($subCollection);
}

return new ImportConfigurator($this->collection, $mergedCollection);
}

/**
Expand Down
43 changes: 25 additions & 18 deletions 43 src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,33 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $

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

$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file);

if (!is_array($imported)) {
$imported = array($imported);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection);
foreach ($imported as $subCollection) {
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection);
}
}

/**
Expand Down
43 changes: 25 additions & 18 deletions 43 src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,33 @@ protected function parseImport(RouteCollection $collection, array $config, $path

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

$subCollection = $this->import($config['resource'], $type, false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
$imported = $this->import($config['resource'], $type, false, $file);

if (!is_array($imported)) {
$imported = array($imported);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection);
foreach ($imported as $subCollection) {
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection);
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions 8 src/Symfony/Component/Routing/Tests/Fixtures/glob/bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="bar_route" path="/bar" controller="AppBundle:Bar:view" />
</routes>
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Routing/Tests/Fixtures/glob/bar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bar_route:
path: /bar
defaults:
_controller: AppBundle:Bar:view
8 changes: 8 additions & 0 deletions 8 src/Symfony/Component/Routing/Tests/Fixtures/glob/baz.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="baz_route" path="/baz" controller="AppBundle:Baz:view" />
</routes>
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Routing/Tests/Fixtures/glob/baz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
baz_route:
path: /baz
defaults:
_controller: AppBundle:Baz:view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="ba?.xml" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_static:
resource: ba?.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="b?r.xml" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_static:
resource: b?r.yml
7 changes: 7 additions & 0 deletions 7 src/Symfony/Component/Routing/Tests/Fixtures/glob/php_dsl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
return $routes->import('php_dsl_ba?.php');
};
12 changes: 12 additions & 0 deletions 12 src/Symfony/Component/Routing/Tests/Fixtures/glob/php_dsl_bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$collection = $routes->collection();

$collection->add('bar_route', '/bar')
->defaults(array('_controller' => 'AppBundle:Bar:view'));

return $collection;
};
12 changes: 12 additions & 0 deletions 12 src/Symfony/Component/Routing/Tests/Fixtures/glob/php_dsl_baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$collection = $routes->collection();

$collection->add('baz_route', '/baz')
->defaults(array('_controller' => 'AppBundle:Baz:view'));

return $collection;
};
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,17 @@ public function testRoutingConfigurator()

$this->assertEquals($expectedCollection, $routeCollection);
}

public function testRoutingConfiguratorCanImportGlobPatterns()
{
$locator = new FileLocator(array(__DIR__.'/../Fixtures/glob'));
$loader = new PhpFileLoader($locator);
$routeCollection = $loader->load('php_dsl.php');

$route = $routeCollection->get('bar_route');
$this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));

$route = $routeCollection->get('baz_route');
$this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
}
}
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,25 @@ public function testImportWithOverriddenController()
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.xml');
}

public function testImportRouteWithGlobMatchingSingleFile()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
$routeCollection = $loader->load('import_single.xml');

$route = $routeCollection->get('bar_route');
$this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
}

public function testImportRouteWithGlobMatchingMultipleFiles()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
$routeCollection = $loader->load('import_multiple.xml');

$route = $routeCollection->get('bar_route');
$this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));

$route = $routeCollection->get('baz_route');
$this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
}
}
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,25 @@ public function testImportWithOverriddenController()
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller')));
$loader->load('import_override_defaults.yml');
}

public function testImportRouteWithGlobMatchingSingleFile()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
$routeCollection = $loader->load('import_single.yml');

$route = $routeCollection->get('bar_route');
$this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
}

public function testImportRouteWithGlobMatchingMultipleFiles()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/glob')));
$routeCollection = $loader->load('import_multiple.yml');

$route = $routeCollection->get('bar_route');
$this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));

$route = $routeCollection->get('baz_route');
$this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.