From 51273f6fb1421b68c1931c3960f68cd483f1ee95 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 7 Mar 2014 12:32:38 +0100 Subject: [ticket/12090] Pass route name to url() to allow admins to change the routes PHPBB3-12090 --- phpBB/phpbb/controller/helper.php | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'phpBB/phpbb/controller') diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index 05a05d1e57..daa52ec672 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -10,6 +10,8 @@ namespace phpbb\controller; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Generator\UrlGenerator; +use Symfony\Component\Routing\RequestContext; /** * Controller helper class, contains methods that do things for controllers @@ -56,13 +58,17 @@ class helper * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP extension */ - public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\extension\finder $finder, \phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, $phpbb_root_path, $php_ext) { $this->template = $template; $this->user = $user; $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; + + $provider = new \phpbb\controller\provider(); + $this->route_collection = $provider->import_paths_from_finder($finder)->find($this->phpbb_root_path); + } /** @@ -87,21 +93,33 @@ class helper } /** - * Generate a URL + * Generate a URL to a route * - * @param string $route The route to travel - * @param mixed $params String or array of additional url parameters + * @param string $route Name of the route to travel + * @param array $params String or array of additional url parameters * @param bool $is_amp Is url using & (true) or & (false) * @param string $session_id Possibility to use a custom session id instead of the global one * @return string The URL already passed through append_sid() */ - public function url($route, $params = false, $is_amp = true, $session_id = false) + public function route($route, array $params = array(), $is_amp = true, $session_id = false) { - $route_params = ''; - if (($route_delim = strpos($route, '?')) !== false) + $anchor = ''; + if (isset($params['#'])) + { + $anchor = '#' . $params['#']; + unset($params['#']); + } + $url_generator = new UrlGenerator($this->route_collection, new RequestContext()); + $route_url = $url_generator->generate($route, $params); + + if (strpos($route_url, '/') === 0) + { + $route_url = substr($route_url, 1); + } + + if ($is_amp) { - $route_params = substr($route, $route_delim); - $route = substr($route, 0, $route_delim); + $route_url = str_replace(array('&', '&'), array('&', '&'), $route_url); } // If enable_mod_rewrite is false, we need to include app.php @@ -111,7 +129,7 @@ class helper $route_prefix .= 'app.' . $this->php_ext . '/'; } - return append_sid($route_prefix . "$route" . $route_params, $params, $is_amp, $session_id); + return append_sid($route_prefix . $route_url . $anchor, false, $is_amp, $session_id); } /** -- cgit v1.2.1 From 07c07171f9b70a49b592473b8a8400d3838333a3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 8 Mar 2014 16:32:43 +0100 Subject: [ticket/12090] Make provider a service and inject it into the helper PHPBB3-12090 --- phpBB/phpbb/controller/helper.php | 6 ++---- phpBB/phpbb/controller/provider.php | 30 +++++++++++------------------- 2 files changed, 13 insertions(+), 23 deletions(-) (limited to 'phpBB/phpbb/controller') diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index daa52ec672..c97e0883f4 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -58,16 +58,14 @@ class helper * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP extension */ - public function __construct(\phpbb\extension\finder $finder, \phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\controller\provider $provider, $phpbb_root_path, $php_ext) { $this->template = $template; $this->user = $user; $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - - $provider = new \phpbb\controller\provider(); - $this->route_collection = $provider->import_paths_from_finder($finder)->find($this->phpbb_root_path); + $this->route_collection = $provider->find($this->phpbb_root_path); } diff --git a/phpBB/phpbb/controller/provider.php b/phpBB/phpbb/controller/provider.php index fde51696e8..fbe717f1af 100644 --- a/phpBB/phpbb/controller/provider.php +++ b/phpBB/phpbb/controller/provider.php @@ -31,28 +31,20 @@ class provider * @param array() $routing_files Array of strings containing paths * to YAML files holding route information */ - public function __construct($routing_files = array()) + public function __construct(\phpbb\extension\finder $finder = null, $routing_files = array()) { $this->routing_files = $routing_files; - } - - /** - * Locate paths containing routing files - * This sets an internal property but does not return the paths. - * - * @return The current instance of this object for method chaining - */ - public function import_paths_from_finder(\phpbb\extension\finder $finder) - { - // We hardcode the path to the core config directory - // because the finder cannot find it - $this->routing_files = array_merge(array('config/routing.yml'), array_keys($finder - ->directory('config') - ->suffix('routing.yml') - ->find() - )); - return $this; + if ($finder) + { + // 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() + )); + } } /** -- cgit v1.2.1 From 436b1d3577cd1b66af568023e566d2de53c255a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 9 Mar 2014 18:22:32 +0100 Subject: [ticket/12090] Fix parameter list of controller helper constructor PHPBB3-12090 --- phpBB/phpbb/controller/helper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb/controller') diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index c97e0883f4..8e33aaf605 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -53,8 +53,9 @@ class helper * Constructor * * @param \phpbb\template\template $template Template object - * @param \phpbb\user $user User object - * @param \phpbb\config\config $config Config object + * @param \phpbb\user $user User object + * @param \phpbb\config\config $config Config object + * @param \phpbb\controller\provider $provider Path provider * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP extension */ -- cgit v1.2.1 From 2eb24d0ace239324086002db4582eaaddd07aa28 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 9 Mar 2014 18:38:21 +0100 Subject: [ticket/12090] Split finding routes and returning routes into 2 methods PHPBB3-12090 --- phpBB/phpbb/controller/helper.php | 3 +-- phpBB/phpbb/controller/provider.php | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'phpBB/phpbb/controller') diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index 8e33aaf605..2d11a54c08 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -66,8 +66,7 @@ class helper $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->route_collection = $provider->find($this->phpbb_root_path); - + $this->route_collection = $provider->find($this->phpbb_root_path)->get_routes(); } /** diff --git a/phpBB/phpbb/controller/provider.php b/phpBB/phpbb/controller/provider.php index fbe717f1af..9df8130210 100644 --- a/phpBB/phpbb/controller/provider.php +++ b/phpBB/phpbb/controller/provider.php @@ -25,6 +25,12 @@ class provider */ protected $routing_files; + /** + * Collection of the routes in phpBB and all found extensions + * @var RouteCollection + */ + protected $routes; + /** * Construct method * @@ -48,20 +54,30 @@ class provider } /** - * Get a list of controllers and return it + * Find a list of controllers and return it * * @param string $base_path Base path to prepend to file paths - * @return array Array of controllers and their route information + * @return null */ public function find($base_path = '') { - $routes = new RouteCollection; + $this->routes = new RouteCollection; foreach ($this->routing_files as $file_path) { $loader = new YamlFileLoader(new FileLocator($base_path)); - $routes->addCollection($loader->load($file_path)); + $this->routes->addCollection($loader->load($file_path)); } - return $routes; + return $this; + } + + /** + * Get the list of routes + * + * @return RouteCollection Get the route collection + */ + public function get_routes() + { + return $this->routes; } } -- cgit v1.2.1 From b29c4c635865444cacb67407c767951c83040036 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 9 Mar 2014 19:03:15 +0100 Subject: [ticket/12090] Move find() call to container and fix tests PHPBB3-12090 --- phpBB/phpbb/controller/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb/controller') diff --git a/phpBB/phpbb/controller/helper.php b/phpBB/phpbb/controller/helper.php index 2d11a54c08..10fdbb1375 100644 --- a/phpBB/phpbb/controller/helper.php +++ b/phpBB/phpbb/controller/helper.php @@ -66,7 +66,7 @@ class helper $this->config = $config; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $this->route_collection = $provider->find($this->phpbb_root_path)->get_routes(); + $this->route_collection = $provider->get_routes(); } /** -- cgit v1.2.1