From a5bfc76a73a213a388126d0f697ba64f27a4b00d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 14 Sep 2014 21:18:20 +0200 Subject: [ticket/13063] Introduces a new \phpbb\routing\router class PHPBB3-13063 --- phpBB/phpbb/routing/router.php | 304 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 phpBB/phpbb/routing/router.php (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php new file mode 100644 index 0000000000..cebf19e702 --- /dev/null +++ b/phpBB/phpbb/routing/router.php @@ -0,0 +1,304 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\routing; + +use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; +use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; +use Symfony\Component\Routing\Matcher\UrlMatcher; +use Symfony\Component\Routing\Generator\UrlGenerator; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Routing\Loader\YamlFileLoader; +use Symfony\Component\Config\FileLocator; +use phpbb\extension\manager; + +/** +* Integration of all pieces of the routing system for easier use. +*/ +class router implements RouterInterface +{ + /** + * @var manager Extension manager + */ + protected $extension_manager; + + /** + * @var string phpBB root path + */ + protected $phpbb_root_path; + + /** + * @var string PHP file extensions + */ + protected $php_ext; + + /** + * @var array YAML file(s) containing route information + */ + protected $routing_files; + + /** + * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null + */ + protected $matcher; + + /** + * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null + */ + protected $generator; + + /** + * @var RequestContext + */ + protected $context; + + /** + * @var RouteCollection|null + */ + protected $route_collection; + + /** + * Construct method + * + * @param manager $extension_manager The extension manager + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param array $routing_files Array of strings containing paths + * to YAML files holding route information + */ + public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $routing_files = array()) + { + $this->extension_manager = $extension_manager; + $this->routing_files = $routing_files; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + $this->context = new RequestContext(); + } + + /** + * Find the list of routing files + * + * @param \phpbb\finder $finder + * @return router + */ + public function find_routing_files(\phpbb\finder $finder) + { + if ($this->routing_files === null || empty($this->routing_files)) + { + // We hardcode the path to the core config directory + // because the finder cannot find it + $this->routing_files = array_merge($this->routing_files, array('config/routing.yml'), array_keys($finder + ->directory('/config') + ->suffix('routing.yml') + ->find() + )); + } + + return $this; + } + + /** + * Find a list of controllers + * + * @param string $base_path Base path to prepend to file paths + * @return router + */ + public function find($base_path = '') + { + if ($this->route_collection === null || $this->route_collection->count() === 0) + { + $this->route_collection = new RouteCollection; + foreach ($this->routing_files as $file_path) + { + $loader = new YamlFileLoader(new FileLocator(phpbb_realpath($base_path))); + $this->route_collection->addCollection($loader->load($file_path)); + } + } + + return $this; + } + + /** + * Get the list of routes + * + * @return RouteCollection Get the route collection + */ + public function get_routes() + { + if ($this->route_collection == null || empty($this->routing_files)) + { + $this->find_routing_files($this->extension_manager->get_finder()) + ->find($this->phpbb_root_path); + } + + return $this->route_collection; + } + + /** + * {@inheritdoc} + */ + public function getRouteCollection() + { + return $this->get_routes(); + } + + /** + * {@inheritdoc} + */ + public function setContext(RequestContext $context) + { + $this->context = $context; + + if ($this->matcher !== null) + { + $this->get_matcher()->setContext($context); + } + if ($this->generator !== null) + { + $this->get_generator()->setContext($context); + } + } + + /** + * {@inheritdoc} + */ + public function getContext() + { + return $this->context; + } + + /** + * {@inheritdoc} + */ + public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) + { + return $this->get_generator()->generate($name, $parameters, $referenceType); + } + + /** + * {@inheritdoc} + */ + public function match($pathinfo) + { + return $this->get_matcher()->match($pathinfo); + } + + /** + * Gets the UrlMatcher instance associated with this Router. + * + * @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance + */ + public function get_matcher() + { + if ($this->matcher !== null) + { + return $this->matcher; + } + + if (defined('DEBUG')) + { + $this->create_new_url_matcher(); + } + else + { + $this->create_dumped_url_matcher(); + } + + return $this->matcher; + } + /** + * Creates a new dumped URL Matcher (dump it if necessary) + */ + protected function create_dumped_url_matcher() + { + if (!file_exists($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext)) + { + $dumper = new PhpMatcherDumper($this->get_routes()); + + $options = array( + 'class' => 'phpbb_url_matcher', + ); + + $dump = $dumper->dump($options); + file_put_contents($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext, $dump); + } + + require_once($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext); + + $this->matcher = new phpbb_url_matcher($this->context); + } + + /** + * Creates a new URL Matcher + */ + protected function create_new_url_matcher() + { + $this->matcher = new UrlMatcher($this->get_routes(), $this->context); + } + + /** + * Gets the UrlGenerator instance associated with this Router. + * + * @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance + */ + public function get_generator() + { + if ($this->generator !== null) + { + return $this->generator; + } + + if (defined('DEBUG')) + { + $this->create_new_url_generator(); + } + else + { + $this->create_dumped_url_generator(); + } + + return $this->generator; + } + + /** + * Creates a new dumped URL Generator (dump it if necessary) + */ + protected function create_dumped_url_generator() + { + if (!file_exists($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext)) + { + $dumper = new PhpGeneratorDumper($this->get_routes()); + + $options = array( + 'class' => 'phpbb_url_generator', + ); + + $dump = $dumper->dump($options); + file_put_contents($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext, $dump); + } + + require_once($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext); + + $this->generator = new phpbb_url_generator($this->context); + } + + /** + * Creates a new URL Generator + */ + protected function create_new_url_generator() + { + $this->generator = new UrlGenerator($this->get_routes(), $this->context); + } +} -- cgit v1.2.1 From 2573dd18ca3327d0b5209a8d465f7911034c1302 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 20 Nov 2014 16:47:24 +0100 Subject: [ticket/13063] Fix coding style PHPBB3-13063 --- phpBB/phpbb/routing/router.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index cebf19e702..f721837bba 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -72,11 +72,10 @@ class router implements RouterInterface /** * Construct method * - * @param manager $extension_manager The extension manager - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension - * @param array $routing_files Array of strings containing paths - * to YAML files holding route information + * @param manager $extension_manager The extension manager + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param array $routing_files Array of strings containing paths to YAML files holding route information */ public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $routing_files = array()) { -- cgit v1.2.1 From 677b5b2cd4937ee6c777728082084c661223dee8 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 20 Nov 2014 22:40:37 +0100 Subject: [ticket/12620] Fix rebase PHPBB3-12620 --- phpBB/phpbb/routing/router.php | 180 +++++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 78 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index f721837bba..0617e94c93 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -25,95 +25,119 @@ use Symfony\Component\Config\FileLocator; use phpbb\extension\manager; /** -* Integration of all pieces of the routing system for easier use. -*/ + * Integration of all pieces of the routing system for easier use. + */ class router implements RouterInterface { /** - * @var manager Extension manager - */ + * Extension manager + * + * @var manager + */ protected $extension_manager; /** - * @var string phpBB root path - */ + * phpBB root path + * + * @var string + */ protected $phpbb_root_path; /** - * @var string PHP file extensions - */ + * PHP file extensions + * + * @var string + */ protected $php_ext; /** - * @var array YAML file(s) containing route information - */ + * Name of the current environment + * + * @var string + */ + protected $environment; + + /** + * YAML file(s) containing route information + * + * @var array + */ protected $routing_files; /** - * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null - */ + * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null + */ protected $matcher; /** - * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null - */ + * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface|null + */ protected $generator; /** - * @var RequestContext - */ + * @var RequestContext + */ protected $context; /** - * @var RouteCollection|null - */ + * @var RouteCollection|null + */ protected $route_collection; /** - * Construct method - * - * @param manager $extension_manager The extension manager - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension - * @param array $routing_files Array of strings containing paths to YAML files holding route information - */ - public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $routing_files = array()) + * Construct method + * + * @param manager $extension_manager Extension manager + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param string $environment Name of the current environment + * @param array $routing_files Array of strings containing paths to YAML files holding route information + */ + public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $environment, $routing_files = array()) { $this->extension_manager = $extension_manager; $this->routing_files = $routing_files; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; + $this->environment = $environment; $this->context = new RequestContext(); } /** - * Find the list of routing files - * - * @param \phpbb\finder $finder - * @return router - */ - public function find_routing_files(\phpbb\finder $finder) + * Find the list of routing files + * + * @param array $paths Array of paths where to look for routing files. + * @return null + */ + public function find_routing_files(array $paths) { - if ($this->routing_files === null || empty($this->routing_files)) + $this->routing_files = array($this->phpbb_root_path . 'config/' . $this->environment . '/routing/environment.yml'); + foreach ($paths as $path) { - // We hardcode the path to the core config directory - // because the finder cannot find it - $this->routing_files = array_merge($this->routing_files, array('config/routing.yml'), array_keys($finder - ->directory('/config') - ->suffix('routing.yml') - ->find() - )); + if (file_exists($path . 'config/' . $this->environment . '/routing/environment.yml')) + { + $this->routing_files[] = $path . 'config/' . $this->environment . '/routing/environment.yml'; + } + else if (!is_dir($path . 'config/' . $this->environment)) + { + if (file_exists($path . 'config/default/routing/environment.yml')) + { + $this->routing_files[] = $path . 'config/default/routing/environment.yml'; + } + else if (!is_dir($path . 'config/default/routing') && file_exists($path . 'config/routing.yml')) + { + $this->routing_files[] = $path . 'config/routing.yml'; + } + } } - - return $this; } /** - * Find a list of controllers - * - * @param string $base_path Base path to prepend to file paths - * @return router - */ + * Find a list of controllers + * + * @param string $base_path Base path to prepend to file paths + * @return router + */ public function find($base_path = '') { if ($this->route_collection === null || $this->route_collection->count() === 0) @@ -130,15 +154,15 @@ class router implements RouterInterface } /** - * Get the list of routes - * - * @return RouteCollection Get the route collection - */ + * Get the list of routes + * + * @return RouteCollection Get the route collection + */ public function get_routes() { if ($this->route_collection == null || empty($this->routing_files)) { - $this->find_routing_files($this->extension_manager->get_finder()) + $this->find_routing_files($this->extension_manager->all_enabled()) ->find($this->phpbb_root_path); } @@ -146,16 +170,16 @@ class router implements RouterInterface } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getRouteCollection() { return $this->get_routes(); } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function setContext(RequestContext $context) { $this->context = $context; @@ -171,34 +195,34 @@ class router implements RouterInterface } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function getContext() { return $this->context; } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) { return $this->get_generator()->generate($name, $parameters, $referenceType); } /** - * {@inheritdoc} - */ + * {@inheritdoc} + */ public function match($pathinfo) { return $this->get_matcher()->match($pathinfo); } /** - * Gets the UrlMatcher instance associated with this Router. - * - * @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance - */ + * Gets the UrlMatcher instance associated with this Router. + * + * @return \Symfony\Component\Routing\Matcher\UrlMatcherInterface A UrlMatcherInterface instance + */ public function get_matcher() { if ($this->matcher !== null) @@ -218,8 +242,8 @@ class router implements RouterInterface return $this->matcher; } /** - * Creates a new dumped URL Matcher (dump it if necessary) - */ + * Creates a new dumped URL Matcher (dump it if necessary) + */ protected function create_dumped_url_matcher() { if (!file_exists($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext)) @@ -240,18 +264,18 @@ class router implements RouterInterface } /** - * Creates a new URL Matcher - */ + * Creates a new URL Matcher + */ protected function create_new_url_matcher() { $this->matcher = new UrlMatcher($this->get_routes(), $this->context); } /** - * Gets the UrlGenerator instance associated with this Router. - * - * @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance - */ + * Gets the UrlGenerator instance associated with this Router. + * + * @return \Symfony\Component\Routing\Generator\UrlGeneratorInterface A UrlGeneratorInterface instance + */ public function get_generator() { if ($this->generator !== null) @@ -272,8 +296,8 @@ class router implements RouterInterface } /** - * Creates a new dumped URL Generator (dump it if necessary) - */ + * Creates a new dumped URL Generator (dump it if necessary) + */ protected function create_dumped_url_generator() { if (!file_exists($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext)) @@ -294,8 +318,8 @@ class router implements RouterInterface } /** - * Creates a new URL Generator - */ + * Creates a new URL Generator + */ protected function create_new_url_generator() { $this->generator = new UrlGenerator($this->get_routes(), $this->context); -- cgit v1.2.1 From f5c5f7df755dd930fef4de6f62e42d4a2a54219e Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 21 Nov 2014 08:26:19 +0100 Subject: [ticket/12620] Fix functionnal tests PHPBB3-12620 --- phpBB/phpbb/routing/router.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 0617e94c93..1003708540 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -107,7 +107,7 @@ class router implements RouterInterface * Find the list of routing files * * @param array $paths Array of paths where to look for routing files. - * @return null + * @return router */ public function find_routing_files(array $paths) { @@ -130,6 +130,8 @@ class router implements RouterInterface } } } + + return $this; } /** -- cgit v1.2.1 From fd94027b40184b4c9eae10c6e5c1f8bee2c2a974 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 25 Nov 2014 12:23:16 +0100 Subject: [ticket/13372] Fix Url Generator/Matcher generation PHPBB3-13372 --- phpBB/phpbb/routing/router.php | 45 ++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 1003708540..c9c50534a2 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -13,6 +13,7 @@ namespace phpbb\routing; +use Symfony\Component\Config\ConfigCache; use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; use Symfony\Component\Routing\Matcher\UrlMatcher; @@ -232,14 +233,7 @@ class router implements RouterInterface return $this->matcher; } - if (defined('DEBUG')) - { - $this->create_new_url_matcher(); - } - else - { - $this->create_dumped_url_matcher(); - } + $this->create_dumped_url_matcher(); return $this->matcher; } @@ -248,21 +242,22 @@ class router implements RouterInterface */ protected function create_dumped_url_matcher() { - if (!file_exists($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext)) + $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_matcher.{$this->php_ext}", defined('DEBUG')); + if (!$cache->isFresh()) { $dumper = new PhpMatcherDumper($this->get_routes()); $options = array( - 'class' => 'phpbb_url_matcher', + 'class' => 'phpbb_url_matcher', + 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher', ); - $dump = $dumper->dump($options); - file_put_contents($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext, $dump); + $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once($this->phpbb_root_path . 'cache/url_matcher.' . $this->php_ext); + require_once $cache; - $this->matcher = new phpbb_url_matcher($this->context); + $this->matcher = new \phpbb_url_matcher($this->context); } /** @@ -285,14 +280,7 @@ class router implements RouterInterface return $this->generator; } - if (defined('DEBUG')) - { - $this->create_new_url_generator(); - } - else - { - $this->create_dumped_url_generator(); - } + $this->create_dumped_url_generator(); return $this->generator; } @@ -302,21 +290,22 @@ class router implements RouterInterface */ protected function create_dumped_url_generator() { - if (!file_exists($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext)) + $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_generator.{$this->php_ext}", defined('DEBUG')); + if (!$cache->isFresh()) { $dumper = new PhpGeneratorDumper($this->get_routes()); $options = array( - 'class' => 'phpbb_url_generator', + 'class' => 'phpbb_url_generator', + 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator', ); - $dump = $dumper->dump($options); - file_put_contents($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext, $dump); + $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once($this->phpbb_root_path . 'cache/url_generator.' . $this->php_ext); + require_once $cache; - $this->generator = new phpbb_url_generator($this->context); + $this->generator = new \phpbb_url_generator($this->context); } /** -- cgit v1.2.1 From f4576c969ab3c3619564e8be7b71547049846bd5 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 25 Nov 2014 12:48:48 +0100 Subject: [ticket/13372] Fix require_once CS PHPBB3-13372 --- phpBB/phpbb/routing/router.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index c9c50534a2..40a829b73f 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -255,7 +255,7 @@ class router implements RouterInterface $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once $cache; + require_once($cache); $this->matcher = new \phpbb_url_matcher($this->context); } @@ -303,7 +303,7 @@ class router implements RouterInterface $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once $cache; + require_once($cache); $this->generator = new \phpbb_url_generator($this->context); } -- cgit v1.2.1 From 6842831d6baa59425ec83cc2ebbae377942824ce Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 17 Jan 2015 11:05:18 +0100 Subject: [ticket/13513] Use paths relative to the phpBB root in the router PHPBB3-13513 --- phpBB/phpbb/routing/router.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 1003708540..601d774129 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -106,25 +106,25 @@ class router implements RouterInterface /** * Find the list of routing files * - * @param array $paths Array of paths where to look for routing files. + * @param array $paths Array of paths where to look for routing files (they must be relative to the phpBB root path). * @return router */ public function find_routing_files(array $paths) { - $this->routing_files = array($this->phpbb_root_path . 'config/' . $this->environment . '/routing/environment.yml'); + $this->routing_files = array('config/' . $this->environment . '/routing/environment.yml'); foreach ($paths as $path) { - if (file_exists($path . 'config/' . $this->environment . '/routing/environment.yml')) + if (file_exists($this->phpbb_root_path . $path . 'config/' . $this->environment . '/routing/environment.yml')) { $this->routing_files[] = $path . 'config/' . $this->environment . '/routing/environment.yml'; } - else if (!is_dir($path . 'config/' . $this->environment)) + else if (!is_dir($this->phpbb_root_path . $path . 'config/' . $this->environment)) { - if (file_exists($path . 'config/default/routing/environment.yml')) + if (file_exists($this->phpbb_root_path . $path . 'config/default/routing/environment.yml')) { $this->routing_files[] = $path . 'config/default/routing/environment.yml'; } - else if (!is_dir($path . 'config/default/routing') && file_exists($path . 'config/routing.yml')) + else if (!is_dir($this->phpbb_root_path . $path . 'config/default/routing') && file_exists($this->phpbb_root_path . $path . 'config/routing.yml')) { $this->routing_files[] = $path . 'config/routing.yml'; } @@ -164,7 +164,7 @@ class router implements RouterInterface { if ($this->route_collection == null || empty($this->routing_files)) { - $this->find_routing_files($this->extension_manager->all_enabled()) + $this->find_routing_files($this->extension_manager->all_enabled(false)) ->find($this->phpbb_root_path); } -- cgit v1.2.1 From 4bdef6fd21a5dcab455b0cd1ee2652de606929c3 Mon Sep 17 00:00:00 2001 From: MateBartus Date: Thu, 12 Mar 2015 00:25:00 +0100 Subject: [ticket/13697] Moving filesystem related functions to filesystem service * Moving filesystem service to \phpbb\filesystem namespace * Wraping Symfony's Filesystem component * Moving filesystem related functions from includes/functions.php into \phpbb\filesystem\filesystem Functions moved (and deprecated): - phpbb_chmod - phpbb_is_writable - phpbb_is_absolute - phpbb_own_realpath - phpbb_realpath * Adding interface for filesystem service PHPBB3-13697 --- phpBB/phpbb/routing/router.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 4ccd3cf5e3..5313106b0a 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -85,17 +85,24 @@ class router implements RouterInterface */ protected $route_collection; + /** + * @var \phpbb\filesystem\filesystem_interface + */ + protected $filesystem; + /** * Construct method * + * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem helper * @param manager $extension_manager Extension manager * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP file extension * @param string $environment Name of the current environment * @param array $routing_files Array of strings containing paths to YAML files holding route information */ - public function __construct(manager $extension_manager, $phpbb_root_path, $php_ext, $environment, $routing_files = array()) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, manager $extension_manager, $phpbb_root_path, $php_ext, $environment, $routing_files = array()) { + $this->filesystem = $filesystem; $this->extension_manager = $extension_manager; $this->routing_files = $routing_files; $this->phpbb_root_path = $phpbb_root_path; @@ -148,7 +155,7 @@ class router implements RouterInterface $this->route_collection = new RouteCollection; foreach ($this->routing_files as $file_path) { - $loader = new YamlFileLoader(new FileLocator(phpbb_realpath($base_path))); + $loader = new YamlFileLoader(new FileLocator($this->filesystem->realpath($base_path))); $this->route_collection->addCollection($loader->load($file_path)); } } -- cgit v1.2.1 From de5a5c41f8e8e47d0740fa97d3d83d57168b18b4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sun, 19 Apr 2015 17:41:29 +0200 Subject: [ticket/13768] Fix deprecations PHPBB3-13768 --- phpBB/phpbb/routing/router.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 5313106b0a..7444f06253 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -262,7 +262,7 @@ class router implements RouterInterface $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once($cache); + require_once($cache->getPath()); $this->matcher = new \phpbb_url_matcher($this->context); } @@ -310,7 +310,7 @@ class router implements RouterInterface $cache->write($dumper->dump($options), $this->get_routes()->getResources()); } - require_once($cache); + require_once($cache->getPath()); $this->generator = new \phpbb_url_generator($this->context); } -- cgit v1.2.1 From 74dbaac0394f45955553ff46515404fe5b06bfba Mon Sep 17 00:00:00 2001 From: MateBartus Date: Thu, 30 Apr 2015 22:40:17 +0200 Subject: [ticket/13800] Make router's extension manager dependency optional PHPBB3-13800 --- phpBB/phpbb/routing/router.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 7444f06253..dd5bffe22b 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -94,13 +94,14 @@ class router implements RouterInterface * Construct method * * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem helper - * @param manager $extension_manager Extension manager - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension - * @param string $environment Name of the current environment - * @param array $routing_files Array of strings containing paths to YAML files holding route information + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param string $environment Name of the current environment + * @param manager|null $extension_manager Extension manager + * @param array $routing_files Array of strings containing paths to YAML files + * holding route information */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, manager $extension_manager, $phpbb_root_path, $php_ext, $environment, $routing_files = array()) + public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, $php_ext, $environment, manager $extension_manager = null, $routing_files = array()) { $this->filesystem = $filesystem; $this->extension_manager = $extension_manager; @@ -172,7 +173,9 @@ class router implements RouterInterface { if ($this->route_collection == null || empty($this->routing_files)) { - $this->find_routing_files($this->extension_manager->all_enabled(false)) + $this->find_routing_files( + ($this->extension_manager !== null) ? $this->extension_manager->all_enabled(false) : array() + ) ->find($this->phpbb_root_path); } -- cgit v1.2.1 From d48e95bb3a17f111e9d4d8111630be44ef9019e4 Mon Sep 17 00:00:00 2001 From: Nicofuma Date: Sun, 10 May 2015 19:18:10 +0200 Subject: [ticket/13829] Don't fail if the cache isn't writeable PHPBB3-13829 --- phpBB/phpbb/routing/router.php | 59 ++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 7444f06253..f74760fa13 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -14,6 +14,7 @@ namespace phpbb\routing; use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; use Symfony\Component\Routing\Matcher\UrlMatcher; @@ -249,22 +250,29 @@ class router implements RouterInterface */ protected function create_dumped_url_matcher() { - $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_matcher.{$this->php_ext}", defined('DEBUG')); - if (!$cache->isFresh()) + try { - $dumper = new PhpMatcherDumper($this->get_routes()); + $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_matcher.{$this->php_ext}", defined('DEBUG')); + if (!$cache->isFresh()) + { + $dumper = new PhpMatcherDumper($this->get_routes()); - $options = array( - 'class' => 'phpbb_url_matcher', - 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher', - ); + $options = array( + 'class' => 'phpbb_url_matcher', + 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher', + ); - $cache->write($dumper->dump($options), $this->get_routes()->getResources()); - } + $cache->write($dumper->dump($options), $this->get_routes()->getResources()); + } - require_once($cache->getPath()); + require_once($cache->getPath()); - $this->matcher = new \phpbb_url_matcher($this->context); + $this->matcher = new \phpbb_url_matcher($this->context); + } + catch (IOException $e) + { + $this->create_new_url_matcher(); + } } /** @@ -297,22 +305,29 @@ class router implements RouterInterface */ protected function create_dumped_url_generator() { - $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_generator.{$this->php_ext}", defined('DEBUG')); - if (!$cache->isFresh()) + try { - $dumper = new PhpGeneratorDumper($this->get_routes()); + $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_generator.{$this->php_ext}", defined('DEBUG')); + if (!$cache->isFresh()) + { + $dumper = new PhpGeneratorDumper($this->get_routes()); - $options = array( - 'class' => 'phpbb_url_generator', - 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator', - ); + $options = array( + 'class' => 'phpbb_url_generator', + 'base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator', + ); - $cache->write($dumper->dump($options), $this->get_routes()->getResources()); - } + $cache->write($dumper->dump($options), $this->get_routes()->getResources()); + } - require_once($cache->getPath()); + require_once($cache->getPath()); - $this->generator = new \phpbb_url_generator($this->context); + $this->generator = new \phpbb_url_generator($this->context); + } + catch (IOException $e) + { + $this->create_new_url_generator(); + } } /** -- cgit v1.2.1 From 66c0e0c6a83fbf091aa078ab06dd6467c8c6aa11 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 4 May 2015 15:10:33 +0200 Subject: [ticket/13388] Integrate routing and di parameters resolution PHPBB3-13388 --- phpBB/phpbb/routing/router.php | 130 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 7 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 2f89d4e884..ca9799ba3e 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -15,6 +15,9 @@ namespace phpbb\routing; use Symfony\Component\Config\ConfigCache; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; +use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; use Symfony\Component\Routing\Matcher\UrlMatcher; @@ -91,18 +94,23 @@ class router implements RouterInterface */ protected $filesystem; + /** + * @var ContainerInterface + */ + protected $container; + /** * Construct method * + * @param ContainerInterface $container DI container * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem helper - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension - * @param string $environment Name of the current environment - * @param manager|null $extension_manager Extension manager - * @param array $routing_files Array of strings containing paths to YAML files - * holding route information + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param string $environment Name of the current environment + * @param manager $extension_manager Extension manager + * @param array $routing_files Array of strings containing paths to YAML files holding route information */ - public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, $php_ext, $environment, manager $extension_manager = null, $routing_files = array()) + public function __construct(ContainerInterface $container, \phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, $php_ext, $environment, manager $extension_manager = null, $routing_files = array()) { $this->filesystem = $filesystem; $this->extension_manager = $extension_manager; @@ -162,9 +170,117 @@ class router implements RouterInterface } } + $this->resolveParameters($this->route_collection); + return $this; } + /** + * Replaces placeholders with service container parameter values in: + * - the route defaults, + * - the route requirements, + * - the route path, + * - the route host, + * - the route schemes, + * - the route methods. + * + * @param RouteCollection $collection + */ + private function resolveParameters(RouteCollection $collection) + { + foreach ($collection as $route) + { + foreach ($route->getDefaults() as $name => $value) + { + $route->setDefault($name, $this->resolve($value)); + } + + foreach ($route->getRequirements() as $name => $value) + { + if ($name === '_scheme' || $name === '_method') + { + continue; // ignore deprecated requirements to not trigger deprecation warnings + } + + $route->setRequirement($name, $this->resolve($value)); + } + + $route->setPath($this->resolve($route->getPath())); + $route->setHost($this->resolve($route->getHost())); + + $schemes = array(); + foreach ($route->getSchemes() as $scheme) + { + $schemes = array_merge($schemes, explode('|', $this->resolve($scheme))); + } + + $route->setSchemes($schemes); + $methods = array(); + foreach ($route->getMethods() as $method) + { + $methods = array_merge($methods, explode('|', $this->resolve($method))); + } + + $route->setMethods($methods); + $route->setCondition($this->resolve($route->getCondition())); + } + } + /** + * Recursively replaces placeholders with the service container parameters. + * + * @param mixed $value The source which might contain "%placeholders%" + * + * @return mixed The source with the placeholders replaced by the container + * parameters. Arrays are resolved recursively. + * + * @throws ParameterNotFoundException When a placeholder does not exist as a container parameter + * @throws RuntimeException When a container value is not a string or a numeric value + */ + private function resolve($value) + { + if (is_array($value)) + { + foreach ($value as $key => $val) + { + $value[$key] = $this->resolve($val); + } + + return $value; + } + + if (!is_string($value)) + { + return $value; + } + + $container = $this->container; + $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) + { + // skip %% + if (!isset($match[1])) + { + return '%%'; + } + + $resolved = $container->getParameter($match[1]); + if (is_string($resolved) || is_numeric($resolved)) + { + return (string) $resolved; + } + + throw new RuntimeException(sprintf( + 'The container parameter "%s", used in the route configuration value "%s", '. + 'must be a string or numeric, but it is of type %s.', + $match[1], + $value, + gettype($resolved) + ) + ); + }, $value); + + return str_replace('%%', '%', $escapedValue); + } + /** * Get the list of routes * -- cgit v1.2.1 From 6656567424843d7118aba1eeb5fc0f4eb61a2e5a Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 29 May 2015 16:37:58 +0200 Subject: [ticket/13388] Address comments PHPBB3-13388 --- phpBB/phpbb/routing/router.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index ca9799ba3e..270b54c4ba 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -195,13 +195,12 @@ class router implements RouterInterface $route->setDefault($name, $this->resolve($value)); } + $requirements = $route->getRequirements(); + unset($requirements['_scheme']); + unset($requirements['_method']); + foreach ($route->getRequirements() as $name => $value) { - if ($name === '_scheme' || $name === '_method') - { - continue; // ignore deprecated requirements to not trigger deprecation warnings - } - $route->setRequirement($name, $this->resolve($value)); } -- cgit v1.2.1 From 80375c98f0f39a957b3348c9384fdc52ba27ea36 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 29 May 2015 16:52:21 +0200 Subject: [ticket/13388] Fix rebase PHPBB3-13388 --- phpBB/phpbb/routing/router.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 270b54c4ba..1c27e95e4d 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -112,6 +112,7 @@ class router implements RouterInterface */ public function __construct(ContainerInterface $container, \phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, $php_ext, $environment, manager $extension_manager = null, $routing_files = array()) { + $this->container = $container; $this->filesystem = $filesystem; $this->extension_manager = $extension_manager; $this->routing_files = $routing_files; -- cgit v1.2.1 From d129ee6c947c6bf494166c47b2d04c7d66e41ab4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 29 May 2015 16:53:54 +0200 Subject: [ticket/13388] Fix deprecations PHPBB3-13388 --- phpBB/phpbb/routing/router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 1c27e95e4d..75726011c4 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -200,7 +200,7 @@ class router implements RouterInterface unset($requirements['_scheme']); unset($requirements['_method']); - foreach ($route->getRequirements() as $name => $value) + foreach ($requirements as $name => $value) { $route->setRequirement($name, $this->resolve($value)); } -- cgit v1.2.1 From ea7114e78528f95fae077568573f2b3363f9aab1 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 30 May 2015 13:14:27 +0200 Subject: [ticket/13388] Fix coding style PHPBB3-13388 --- phpBB/phpbb/routing/router.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 75726011c4..5af005769f 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -225,6 +225,7 @@ class router implements RouterInterface $route->setCondition($this->resolve($route->getCondition())); } } + /** * Recursively replaces placeholders with the service container parameters. * -- cgit v1.2.1 From 403c647b9e84640977ca0f98d21d15ceb4957bdb Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 7 Oct 2015 23:09:13 +0200 Subject: [ticket/14220] Move route loading to services PHPBB3-14220 --- phpBB/phpbb/routing/router.php | 346 +++++++++++++++++------------------------ 1 file changed, 146 insertions(+), 200 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 5af005769f..5d237b6433 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -13,21 +13,20 @@ namespace phpbb\routing; +use phpbb\routing\resources_locator\resources_locator_interface; use Symfony\Component\Config\ConfigCache; -use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; use Symfony\Component\DependencyInjection\Exception\RuntimeException; -use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; +use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; -use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Generator\UrlGenerator; +use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; +use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; -use Symfony\Component\Routing\Loader\YamlFileLoader; -use Symfony\Component\Config\FileLocator; -use phpbb\extension\manager; /** * Integration of all pieces of the routing system for easier use. @@ -35,11 +34,19 @@ use phpbb\extension\manager; class router implements RouterInterface { /** - * Extension manager - * - * @var manager + * @var ContainerInterface */ - protected $extension_manager; + protected $container; + + /** + * @var resources_locator_interface + */ + protected $resources_locator; + + /** + * @var LoaderInterface + */ + protected $loader; /** * phpBB root path @@ -62,13 +69,6 @@ class router implements RouterInterface */ protected $environment; - /** - * YAML file(s) containing route information - * - * @var array - */ - protected $routing_files; - /** * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null */ @@ -85,37 +85,25 @@ class router implements RouterInterface protected $context; /** - * @var RouteCollection|null + * @var RouteCollection */ protected $route_collection; - /** - * @var \phpbb\filesystem\filesystem_interface - */ - protected $filesystem; - - /** - * @var ContainerInterface - */ - protected $container; - /** * Construct method * - * @param ContainerInterface $container DI container - * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem helper - * @param string $phpbb_root_path phpBB root path - * @param string $php_ext PHP file extension - * @param string $environment Name of the current environment - * @param manager $extension_manager Extension manager - * @param array $routing_files Array of strings containing paths to YAML files holding route information + * @param ContainerInterface $container DI container + * @param resources_locator_interface $resources_locator Resources locator + * @param LoaderInterface $loader Resources loader + * @param string $phpbb_root_path phpBB root path + * @param string $php_ext PHP file extension + * @param string $environment Name of the current environment */ - public function __construct(ContainerInterface $container, \phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, $php_ext, $environment, manager $extension_manager = null, $routing_files = array()) + public function __construct(ContainerInterface $container, resources_locator_interface $resources_locator, LoaderInterface $loader, $phpbb_root_path, $php_ext, $environment) { $this->container = $container; - $this->filesystem = $filesystem; - $this->extension_manager = $extension_manager; - $this->routing_files = $routing_files; + $this->resources_locator = $resources_locator; + $this->loader = $loader; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; $this->environment = $environment; @@ -123,178 +111,28 @@ class router implements RouterInterface } /** - * Find the list of routing files + * Get the list of routes * - * @param array $paths Array of paths where to look for routing files (they must be relative to the phpBB root path). - * @return router + * @return RouteCollection Get the route collection */ - public function find_routing_files(array $paths) + public function get_routes() { - $this->routing_files = array('config/' . $this->environment . '/routing/environment.yml'); - foreach ($paths as $path) + if ($this->route_collection === null /*|| $this->route_collection->count() === 0*/) { - if (file_exists($this->phpbb_root_path . $path . 'config/' . $this->environment . '/routing/environment.yml')) - { - $this->routing_files[] = $path . 'config/' . $this->environment . '/routing/environment.yml'; - } - else if (!is_dir($this->phpbb_root_path . $path . 'config/' . $this->environment)) + $this->route_collection = new RouteCollection; + foreach ($this->resources_locator->locate_resources() as $resource) { - if (file_exists($this->phpbb_root_path . $path . 'config/default/routing/environment.yml')) + if (is_array($resource)) { - $this->routing_files[] = $path . 'config/default/routing/environment.yml'; + $this->route_collection->addCollection($this->loader->load($resource[0], $resource[1])); } - else if (!is_dir($this->phpbb_root_path . $path . 'config/default/routing') && file_exists($this->phpbb_root_path . $path . 'config/routing.yml')) + else { - $this->routing_files[] = $path . 'config/routing.yml'; + $this->route_collection->addCollection($this->loader->load($resource)); } } - } - return $this; - } - - /** - * Find a list of controllers - * - * @param string $base_path Base path to prepend to file paths - * @return router - */ - public function find($base_path = '') - { - if ($this->route_collection === null || $this->route_collection->count() === 0) - { - $this->route_collection = new RouteCollection; - foreach ($this->routing_files as $file_path) - { - $loader = new YamlFileLoader(new FileLocator($this->filesystem->realpath($base_path))); - $this->route_collection->addCollection($loader->load($file_path)); - } - } - - $this->resolveParameters($this->route_collection); - - return $this; - } - - /** - * Replaces placeholders with service container parameter values in: - * - the route defaults, - * - the route requirements, - * - the route path, - * - the route host, - * - the route schemes, - * - the route methods. - * - * @param RouteCollection $collection - */ - private function resolveParameters(RouteCollection $collection) - { - foreach ($collection as $route) - { - foreach ($route->getDefaults() as $name => $value) - { - $route->setDefault($name, $this->resolve($value)); - } - - $requirements = $route->getRequirements(); - unset($requirements['_scheme']); - unset($requirements['_method']); - - foreach ($requirements as $name => $value) - { - $route->setRequirement($name, $this->resolve($value)); - } - - $route->setPath($this->resolve($route->getPath())); - $route->setHost($this->resolve($route->getHost())); - - $schemes = array(); - foreach ($route->getSchemes() as $scheme) - { - $schemes = array_merge($schemes, explode('|', $this->resolve($scheme))); - } - - $route->setSchemes($schemes); - $methods = array(); - foreach ($route->getMethods() as $method) - { - $methods = array_merge($methods, explode('|', $this->resolve($method))); - } - - $route->setMethods($methods); - $route->setCondition($this->resolve($route->getCondition())); - } - } - - /** - * Recursively replaces placeholders with the service container parameters. - * - * @param mixed $value The source which might contain "%placeholders%" - * - * @return mixed The source with the placeholders replaced by the container - * parameters. Arrays are resolved recursively. - * - * @throws ParameterNotFoundException When a placeholder does not exist as a container parameter - * @throws RuntimeException When a container value is not a string or a numeric value - */ - private function resolve($value) - { - if (is_array($value)) - { - foreach ($value as $key => $val) - { - $value[$key] = $this->resolve($val); - } - - return $value; - } - - if (!is_string($value)) - { - return $value; - } - - $container = $this->container; - $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) - { - // skip %% - if (!isset($match[1])) - { - return '%%'; - } - - $resolved = $container->getParameter($match[1]); - if (is_string($resolved) || is_numeric($resolved)) - { - return (string) $resolved; - } - - throw new RuntimeException(sprintf( - 'The container parameter "%s", used in the route configuration value "%s", '. - 'must be a string or numeric, but it is of type %s.', - $match[1], - $value, - gettype($resolved) - ) - ); - }, $value); - - return str_replace('%%', '%', $escapedValue); - } - - /** - * Get the list of routes - * - * @return RouteCollection Get the route collection - */ - public function get_routes() - { - if ($this->route_collection == null || empty($this->routing_files)) - { - $this->find_routing_files( - ($this->extension_manager !== null) ? $this->extension_manager->all_enabled(false) : array() - ) - ->find($this->phpbb_root_path); + $this->resolveParameters($this->route_collection); } return $this->route_collection; @@ -365,6 +203,7 @@ class router implements RouterInterface return $this->matcher; } + /** * Creates a new dumped URL Matcher (dump it if necessary) */ @@ -457,4 +296,111 @@ class router implements RouterInterface { $this->generator = new UrlGenerator($this->get_routes(), $this->context); } + + /** + * Replaces placeholders with service container parameter values in: + * - the route defaults, + * - the route requirements, + * - the route path, + * - the route host, + * - the route schemes, + * - the route methods. + * + * @param RouteCollection $collection + */ + protected function resolveParameters(RouteCollection $collection) + { + /** @var \Symfony\Component\Routing\Route $route */ + foreach ($collection as $route) + { + foreach ($route->getDefaults() as $name => $value) + { + $route->setDefault($name, $this->resolve($value)); + } + + $requirements = $route->getRequirements(); + unset($requirements['_scheme']); + unset($requirements['_method']); + + foreach ($requirements as $name => $value) + { + $route->setRequirement($name, $this->resolve($value)); + } + + $route->setPath($this->resolve($route->getPath())); + $route->setHost($this->resolve($route->getHost())); + + $schemes = array(); + foreach ($route->getSchemes() as $scheme) + { + $schemes = array_merge($schemes, explode('|', $this->resolve($scheme))); + } + + $route->setSchemes($schemes); + $methods = array(); + foreach ($route->getMethods() as $method) + { + $methods = array_merge($methods, explode('|', $this->resolve($method))); + } + + $route->setMethods($methods); + $route->setCondition($this->resolve($route->getCondition())); + } + } + + /** + * Recursively replaces placeholders with the service container parameters. + * + * @param mixed $value The source which might contain "%placeholders%" + * + * @return mixed The source with the placeholders replaced by the container + * parameters. Arrays are resolved recursively. + * + * @throws ParameterNotFoundException When a placeholder does not exist as a container parameter + * @throws RuntimeException When a container value is not a string or a numeric value + */ + private function resolve($value) + { + if (is_array($value)) + { + foreach ($value as $key => $val) + { + $value[$key] = $this->resolve($val); + } + + return $value; + } + + if (!is_string($value)) + { + return $value; + } + + $container = $this->container; + $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) + { + // skip %% + if (!isset($match[1])) + { + return '%%'; + } + + $resolved = $container->getParameter($match[1]); + if (is_string($resolved) || is_numeric($resolved)) + { + return (string) $resolved; + } + + throw new RuntimeException(sprintf( + 'The container parameter "%s", used in the route configuration value "%s", '. + 'must be a string or numeric, but it is of type %s.', + $match[1], + $value, + gettype($resolved) + ) + ); + }, $value); + + return str_replace('%%', '%', $escapedValue); + } } -- cgit v1.2.1 From 56c2caf6c0778c0da48fe0ac688c893777b89ee4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 23 Mar 2016 22:48:58 +0100 Subject: [ticket/14555] Uniformize cache directory usages PHPBB3-14555 --- phpBB/phpbb/routing/router.php | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) (limited to 'phpBB/phpbb/routing/router.php') diff --git a/phpBB/phpbb/routing/router.php b/phpBB/phpbb/routing/router.php index 5d237b6433..f19886fb0b 100644 --- a/phpBB/phpbb/routing/router.php +++ b/phpBB/phpbb/routing/router.php @@ -48,13 +48,6 @@ class router implements RouterInterface */ protected $loader; - /** - * phpBB root path - * - * @var string - */ - protected $phpbb_root_path; - /** * PHP file extensions * @@ -62,13 +55,6 @@ class router implements RouterInterface */ protected $php_ext; - /** - * Name of the current environment - * - * @var string - */ - protected $environment; - /** * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|null */ @@ -89,25 +75,28 @@ class router implements RouterInterface */ protected $route_collection; + /** + * @var string + */ + protected $cache_dir; + /** * Construct method * * @param ContainerInterface $container DI container * @param resources_locator_interface $resources_locator Resources locator * @param LoaderInterface $loader Resources loader - * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP file extension - * @param string $environment Name of the current environment + * @param string $cache_dir phpBB cache directory */ - public function __construct(ContainerInterface $container, resources_locator_interface $resources_locator, LoaderInterface $loader, $phpbb_root_path, $php_ext, $environment) + public function __construct(ContainerInterface $container, resources_locator_interface $resources_locator, LoaderInterface $loader, $php_ext, $cache_dir) { $this->container = $container; $this->resources_locator = $resources_locator; $this->loader = $loader; - $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->environment = $environment; $this->context = new RequestContext(); + $this->cache_dir = $cache_dir; } /** @@ -211,7 +200,7 @@ class router implements RouterInterface { try { - $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_matcher.{$this->php_ext}", defined('DEBUG')); + $cache = new ConfigCache("{$this->cache_dir}url_matcher.{$this->php_ext}", defined('DEBUG')); if (!$cache->isFresh()) { $dumper = new PhpMatcherDumper($this->get_routes()); @@ -266,7 +255,7 @@ class router implements RouterInterface { try { - $cache = new ConfigCache("{$this->phpbb_root_path}cache/{$this->environment}/url_generator.{$this->php_ext}", defined('DEBUG')); + $cache = new ConfigCache("{$this->cache_dir}url_generator.{$this->php_ext}", defined('DEBUG')); if (!$cache->isFresh()) { $dumper = new PhpGeneratorDumper($this->get_routes()); -- cgit v1.2.1