1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-10 02:36:38 +02:00

[ticket/13063] Introduces a new \phpbb\routing\router class

PHPBB3-13063
This commit is contained in:
Tristan Darricau
2014-09-14 21:18:20 +02:00
parent 3b1c611566
commit a5bfc76a73
11 changed files with 379 additions and 209 deletions

View File

@@ -73,7 +73,7 @@ services:
- @template
- @user
- @config
- @controller.provider
- @router
- @ext.manager
- @symfony_request
- @request
@@ -89,11 +89,6 @@ services:
- %core.root_path%
- @template
controller.provider:
class: phpbb\controller\provider
calls:
- [find, [%core.root_path%]]
ext.manager:
class: phpbb\extension\manager
arguments:
@@ -157,6 +152,20 @@ services:
- null
- %core.disable_super_globals%
router:
class: phpbb\routing\router
arguments:
- @ext.manager
- %core.root_path%
- %core.php_ext%
router.listener:
class: Symfony\Component\HttpKernel\EventListener\RouterListener
arguments:
- @router
tags:
- { name: kernel.event_subscriber }
# WARNING: The Symfony request does not escape the input and should be used very carefully
# prefer the phpbb request (service @request) as possible
symfony_request:

View File

@@ -50,6 +50,7 @@ abstract class base implements \phpbb\cache\driver\driver_interface
}
else if (strpos($filename, 'container_') === 0 ||
strpos($filename, 'url_matcher') === 0 ||
strpos($filename, 'url_generator') === 0 ||
strpos($filename, 'sql_') === 0 ||
strpos($filename, 'data_') === 0)
{

View File

@@ -14,7 +14,6 @@
namespace phpbb\controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
@@ -41,6 +40,11 @@ class helper
*/
protected $config;
/**
* @var \phpbb\routing\router phpBB router
*/
protected $router;
/* @var \phpbb\symfony_request */
protected $symfony_request;
@@ -70,7 +74,7 @@ class helper
* @param \phpbb\template\template $template Template object
* @param \phpbb\user $user User object
* @param \phpbb\config\config $config Config object
* @param \phpbb\controller\provider $provider Path provider
* @param \phpbb\routing\router $router phpBB router
* @param \phpbb\extension\manager $manager Extension manager object
* @param \phpbb\symfony_request $symfony_request Symfony Request object
* @param \phpbb\request\request_interface $request phpBB request object
@@ -78,18 +82,17 @@ class helper
* @param string $phpbb_root_path phpBB root path
* @param string $php_ext PHP file extension
*/
public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\controller\provider $provider, \phpbb\extension\manager $manager, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, \phpbb\filesystem $filesystem, $phpbb_root_path, $php_ext)
public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\routing\router $router, \phpbb\extension\manager $manager, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, \phpbb\filesystem $filesystem, $phpbb_root_path, $php_ext)
{
$this->template = $template;
$this->user = $user;
$this->config = $config;
$this->router = $router;
$this->symfony_request = $symfony_request;
$this->request = $request;
$this->filesystem = $filesystem;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$provider->find_routing_files($manager->get_finder());
$this->route_collection = $provider->find($phpbb_root_path)->get_routes();
}
/**
@@ -162,8 +165,8 @@ class helper
$context->setBaseUrl($base_url);
$url_generator = new UrlGenerator($this->route_collection, $context);
$route_url = $url_generator->generate($route, $params, $reference_type);
$this->router->setContext($context);
$route_url = $this->router->generate($route, $params, $reference_type);
if ($is_amp)
{

View File

@@ -1,92 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\controller;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
/**
* Controller interface
*/
class provider
{
/**
* YAML file(s) containing route information
* @var array
*/
protected $routing_files;
/**
* Collection of the routes in phpBB and all found extensions
* @var RouteCollection
*/
protected $routes;
/**
* Construct method
*
* @param array $routing_files Array of strings containing paths
* to YAML files holding route information
*/
public function __construct($routing_files = array())
{
$this->routing_files = $routing_files;
}
/**
* Find the list of routing files
*
* @param \phpbb\finder $finder
* @return null
*/
public function find_routing_files(\phpbb\finder $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()
));
}
/**
* Find a list of controllers
*
* @param string $base_path Base path to prepend to file paths
* @return provider
*/
public function find($base_path = '')
{
$this->routes = new RouteCollection;
foreach ($this->routing_files as $file_path)
{
$loader = new YamlFileLoader(new FileLocator(phpbb_realpath($base_path)));
$this->routes->addCollection($loader->load($file_path));
}
return $this;
}
/**
* Get the list of routes
*
* @return RouteCollection Get the route collection
*/
public function get_routes()
{
return $this->routes;
}
}

View File

@@ -1,82 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\Routing\RequestContext;
class kernel_request_subscriber implements EventSubscriberInterface
{
/**
* Extension manager object
* @var \phpbb\extension\manager
*/
protected $manager;
/**
* PHP file extension
* @var string
*/
protected $php_ext;
/**
* Root path
* @var string
*/
protected $root_path;
/**
* Construct method
*
* @param \phpbb\extension\manager $manager Extension manager object
* @param string $root_path Root path
* @param string $php_ext PHP file extension
*/
public function __construct(\phpbb\extension\manager $manager, $root_path, $php_ext)
{
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->manager = $manager;
}
/**
* This listener is run when the KernelEvents::REQUEST event is triggered
*
* This is responsible for setting up the routing information
*
* @param GetResponseEvent $event
* @throws \BadMethodCallException
* @return null
*/
public function on_kernel_request(GetResponseEvent $event)
{
$request = $event->getRequest();
$context = new RequestContext();
$context->fromRequest($request);
$matcher = phpbb_get_url_matcher($this->manager, $context, $this->root_path, $this->php_ext);
$router_listener = new RouterListener($matcher, $context);
$router_listener->onKernelRequest($event);
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => 'on_kernel_request',
);
}
}

View File

@@ -0,0 +1,304 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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);
}
}