From 0504caa118b388f47d468f92be4ab1ec59acc026 Mon Sep 17 00:00:00 2001 From: Thomas Decaux Date: Sat, 30 Dec 2017 13:51:12 +0100 Subject: [PATCH 1/2] Use glob pattern to include routing annotations ``FileLoader::import`` method can return an array, this PR fix the error "Call to a member function addPrefix() on array". Use case "I want to import routes from controllers in different directories"; given a ``route/annotation.yaml`` like this: ``` controllers: resource: ../../src/*/Controller/*Controller.php type: annotation ``` (``/*Controller.php`` is required to make the glob return something). I dont know how to code this properly, that why I just copied/paste the code like this, but the idea is here. --- .../Routing/Loader/YamlFileLoader.php | 71 +++++++++++++------ 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index b3ed099f8149..82e0717ad437 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -152,29 +152,58 @@ 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); - } - $subCollection->addDefaults($defaults); - $subCollection->addRequirements($requirements); - $subCollection->addOptions($options); + + if (is_array($subCollection)) { + /* @var $value RouteCollection */ + foreach($subCollection as $value) { + $value->addPrefix($prefix); + if (null !== $host) { + $value->setHost($host); + } + if (null !== $condition) { + $value->setCondition($condition); + } + if (null !== $schemes) { + $value->setSchemes($schemes); + } + if (null !== $methods) { + $value->setMethods($methods); + } + $value->addDefaults($defaults); + $value->addRequirements($requirements); + $value->addOptions($options); + + if (isset($config['name_prefix'])) { + $value->addNamePrefix($config['name_prefix']); + } + + $collection->addCollection($value); + } + } else { + /* @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); - if (isset($config['name_prefix'])) { - $subCollection->addNamePrefix($config['name_prefix']); - } + if (isset($config['name_prefix'])) { + $subCollection->addNamePrefix($config['name_prefix']); + } - $collection->addCollection($subCollection); + $collection->addCollection($subCollection); + } } /** From 57bdac6bde836f4fba4880669d5d031cbaf1f808 Mon Sep 17 00:00:00 2001 From: Thomas Decaux Date: Sat, 30 Dec 2017 14:31:42 +0100 Subject: [PATCH 2/2] Update YamlFileLoader.php --- .../Routing/Loader/YamlFileLoader.php | 54 ++++++------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index 82e0717ad437..71503e4dc8f8 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -153,56 +153,34 @@ protected function parseImport(RouteCollection $collection, array $config, $path $subCollection = $this->import($config['resource'], $type, false, $file); - if (is_array($subCollection)) { - /* @var $value RouteCollection */ - foreach($subCollection as $value) { - $value->addPrefix($prefix); - if (null !== $host) { - $value->setHost($host); - } - if (null !== $condition) { - $value->setCondition($condition); - } - if (null !== $schemes) { - $value->setSchemes($schemes); - } - if (null !== $methods) { - $value->setMethods($methods); - } - $value->addDefaults($defaults); - $value->addRequirements($requirements); - $value->addOptions($options); - - if (isset($config['name_prefix'])) { - $value->addNamePrefix($config['name_prefix']); - } - - $collection->addCollection($value); - } - } else { - /* @var $subCollection RouteCollection */ - $subCollection->addPrefix($prefix); + if (!is_array($subCollection)) { + $subCollection = array($subCollection); + } + + /* @var $value RouteCollection */ + foreach ($subCollection as $value) { + $value->addPrefix($prefix); if (null !== $host) { - $subCollection->setHost($host); + $value->setHost($host); } if (null !== $condition) { - $subCollection->setCondition($condition); + $value->setCondition($condition); } if (null !== $schemes) { - $subCollection->setSchemes($schemes); + $value->setSchemes($schemes); } if (null !== $methods) { - $subCollection->setMethods($methods); + $value->setMethods($methods); } - $subCollection->addDefaults($defaults); - $subCollection->addRequirements($requirements); - $subCollection->addOptions($options); + $value->addDefaults($defaults); + $value->addRequirements($requirements); + $value->addOptions($options); if (isset($config['name_prefix'])) { - $subCollection->addNamePrefix($config['name_prefix']); + $value->addNamePrefix($config['name_prefix']); } - $collection->addCollection($subCollection); + $collection->addCollection($value); } }