mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 14:18:52 +00:00
[feature/controller] Use a dumped url matcher class to improve performance
PHPBB3-10864
This commit is contained in:
parent
5877bf1b1b
commit
d3aa8823b2
4 changed files with 106 additions and 55 deletions
|
@ -123,7 +123,7 @@ services:
|
||||||
- @ext.manager
|
- @ext.manager
|
||||||
- %core.root_path%
|
- %core.root_path%
|
||||||
- @cache.driver
|
- @cache.driver
|
||||||
- .%core.php_ext%
|
- %core.php_ext%
|
||||||
- _ext_finder
|
- _ext_finder
|
||||||
|
|
||||||
http_kernel:
|
http_kernel:
|
||||||
|
@ -137,22 +137,15 @@ services:
|
||||||
arguments:
|
arguments:
|
||||||
- @template
|
- @template
|
||||||
- @user
|
- @user
|
||||||
|
- @ext.finder
|
||||||
|
- %core.root_path%
|
||||||
|
- .%core.php_ext%
|
||||||
tags:
|
tags:
|
||||||
- { name: kernel.event_subscriber }
|
- { name: kernel.event_subscriber }
|
||||||
|
|
||||||
request:
|
request:
|
||||||
class: phpbb_request
|
class: phpbb_request
|
||||||
|
|
||||||
request.context:
|
|
||||||
class: Symfony\Component\Routing\RequestContext
|
|
||||||
|
|
||||||
router_listener:
|
|
||||||
class: Symfony\Component\HttpKernel\EventListener\RouterListener
|
|
||||||
arguments:
|
|
||||||
- @url_matcher
|
|
||||||
tags:
|
|
||||||
- { name: kernel.event_subscriber }
|
|
||||||
|
|
||||||
style:
|
style:
|
||||||
class: phpbb_style
|
class: phpbb_style
|
||||||
arguments:
|
arguments:
|
||||||
|
@ -189,11 +182,5 @@ services:
|
||||||
template_context:
|
template_context:
|
||||||
class: phpbb_template_context
|
class: phpbb_template_context
|
||||||
|
|
||||||
url_matcher:
|
|
||||||
class: Symfony\Component\Routing\Matcher\UrlMatcher
|
|
||||||
arguments:
|
|
||||||
- @controller.route_collection
|
|
||||||
- @request.context
|
|
||||||
|
|
||||||
user:
|
user:
|
||||||
class: phpbb_user
|
class: phpbb_user
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @package controller
|
|
||||||
* @copyright (c) 2012 phpBB Group
|
|
||||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ignore
|
|
||||||
*/
|
|
||||||
if (!defined('IN_PHPBB'))
|
|
||||||
{
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Controller manager class
|
|
||||||
* @package phpBB3
|
|
||||||
*/
|
|
||||||
class phpbb_controller_route_collection extends RouteCollection
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Construct method
|
|
||||||
*
|
|
||||||
* @param phpbb_extension_finder $finder Finder object
|
|
||||||
*/
|
|
||||||
public function __construct(phpbb_extension_finder $finder, phpbb_controller_provider $provider)
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
$this->addCollection($provider->get_paths($finder)->find());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -18,8 +18,11 @@ if (!defined('IN_PHPBB'))
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\EventListener\RouterListener;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
|
||||||
class phpbb_event_kernel_subscriber implements EventSubscriberInterface
|
class phpbb_event_kernel_subscriber implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
|
@ -35,16 +38,40 @@ class phpbb_event_kernel_subscriber implements EventSubscriberInterface
|
||||||
*/
|
*/
|
||||||
protected $user;
|
protected $user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension finder object
|
||||||
|
* @var phpbb_extension_finder
|
||||||
|
*/
|
||||||
|
protected $finder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHP extension
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $php_ext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Root path
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $root_path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct method
|
* Construct method
|
||||||
*
|
*
|
||||||
* @param phpbb_template $template Template object
|
* @param phpbb_template $template Template object
|
||||||
* @param phpbb_user $user User object
|
* @param phpbb_user $user User object
|
||||||
|
* @param phpbb_extension_finder $finder Extension finder object
|
||||||
|
* @param string $root_path Root path
|
||||||
|
* @param string $php_ext PHP extension
|
||||||
*/
|
*/
|
||||||
public function __construct(phpbb_template $template, phpbb_user $user)
|
public function __construct(phpbb_template $template, phpbb_user $user, phpbb_extension_finder $finder, $root_path, $php_ext)
|
||||||
{
|
{
|
||||||
$this->template = $template;
|
$this->template = $template;
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
|
$this->finder = $finder;
|
||||||
|
$this->root_path = $root_path;
|
||||||
|
$this->php_ext = $php_ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,12 +108,33 @@ class phpbb_event_kernel_subscriber implements EventSubscriberInterface
|
||||||
|
|
||||||
page_footer(true, false, false);
|
page_footer(true, false, false);
|
||||||
|
|
||||||
$event->setResponse(new Response($this->template->return_display('body'), 404));
|
$event->setResponse(new Response($this->template->assign_display('body'), 404));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This listener is run when the KernelEvents::REQUEST event is triggered
|
||||||
|
*
|
||||||
|
* This is responsible for setting up the routing information
|
||||||
|
*
|
||||||
|
* @param GetResponseEvent $event
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
public function on_kernel_request(GetResponseEvent $event)
|
||||||
|
{
|
||||||
|
$request = $event->getRequest();
|
||||||
|
$context = new RequestContext();
|
||||||
|
$context->fromRequest($request);
|
||||||
|
|
||||||
|
$matcher = phpbb_create_url_matcher($this->finder, $context, $this->root_path, $this->php_ext);
|
||||||
|
|
||||||
|
$router_listener = new RouterListener($matcher, $context);
|
||||||
|
$router_listener->onKernelRequest($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getSubscribedEvents()
|
public static function getSubscribedEvents()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
KernelEvents::REQUEST => 'on_kernel_request',
|
||||||
KernelEvents::TERMINATE => 'on_kernel_terminate',
|
KernelEvents::TERMINATE => 'on_kernel_terminate',
|
||||||
KernelEvents::EXCEPTION => 'on_kernel_exception',
|
KernelEvents::EXCEPTION => 'on_kernel_exception',
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
|
@ -5444,3 +5447,52 @@ function phpbb_to_numeric($input)
|
||||||
{
|
{
|
||||||
return ($input > PHP_INT_MAX) ? (float) $input : (int) $input;
|
return ($input > PHP_INT_MAX) ? (float) $input : (int) $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and/or return the cached phpbb_url_matcher class
|
||||||
|
*
|
||||||
|
* If the class already exists, it instantiates it
|
||||||
|
*
|
||||||
|
* @param phpbb_extension_finder $finder Extension finder
|
||||||
|
* @param RequestContext $context Symfony RequestContext object
|
||||||
|
* @param string $root_path Root path
|
||||||
|
* @param string $php_ext PHP extension
|
||||||
|
* @return phpbb_url_matcher
|
||||||
|
*/
|
||||||
|
function phpbb_create_url_matcher(phpbb_extension_finder $finder, RequestContext $context, $root_path, $php_ext)
|
||||||
|
{
|
||||||
|
$matcher = phpbb_load_url_matcher($finder, $context, $root_path, $php_ext);
|
||||||
|
if ($matcher === false)
|
||||||
|
{
|
||||||
|
$provider = new phpbb_controller_provider();
|
||||||
|
$dumper = new PhpMatcherDumper($provider->get_paths($finder)->find());
|
||||||
|
$cached_url_matcher_dump = $dumper->dump(array(
|
||||||
|
'class' => 'phpbb_url_matcher',
|
||||||
|
));
|
||||||
|
|
||||||
|
file_put_contents($root_path . 'cache/url_matcher' . $php_ext, $cached_url_matcher_dump);
|
||||||
|
return phpbb_load_url_matcher($finder, $context, $root_path, $php_ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $matcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the cached phpbb_url_matcher class
|
||||||
|
*
|
||||||
|
* @param phpbb_extension_finder $finder Extension finder
|
||||||
|
* @param RequestContext $context Symfony RequestContext object
|
||||||
|
* @param string $root_path Root path
|
||||||
|
* @param string $php_ext PHP extension
|
||||||
|
* @return phpbb_url_matcher|bool False if the file doesn't exist
|
||||||
|
*/
|
||||||
|
function phpbb_load_url_matcher(phpbb_extension_finder $finder, RequestContext $context, $root_path, $php_ext)
|
||||||
|
{
|
||||||
|
if (file_exists($root_path . 'cache/url_matcher' . $php_ext))
|
||||||
|
{
|
||||||
|
include($root_path . 'cache/url_matcher' . $php_ext);
|
||||||
|
return new phpbb_url_matcher($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue