[WebProfilerBundle] moved the computation of the Router panel at runtime

This commit is contained in:
Fabien Potencier 2011-12-31 15:51:56 +01:00
parent 3d5ecc0478
commit b46114a0f6
5 changed files with 68 additions and 45 deletions

View File

@ -14,8 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
use Symfony\Component\Routing\RouterInterface;
/**
* RouterDataCollector.
@ -24,37 +22,11 @@ use Symfony\Component\Routing\RouterInterface;
*/
class RouterDataCollector extends DataCollector
{
private $router;
public function __construct(RouterInterface $router = null)
{
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data['path_info'] = $request->getPathInfo();
if (!$this->router) {
$this->data['traces'] = array();
} else {
$matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $this->router->getContext());
$this->data['traces'] = $matcher->getTraces($request->getPathInfo());
}
}
public function getPathInfo()
{
return $this->data['path_info'];
}
public function getTraces()
{
return $this->data['traces'];
}
/**

View File

@ -54,7 +54,6 @@
<service id="data_collector.router" class="%data_collector.router.class%" public="false">
<tag name="data_collector" template="WebProfilerBundle:Collector:router" id="router" priority="255" />
<argument type="service" id="router" on-invalid="ignore" />
</service>
</services>
</container>

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\WebProfilerBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
/**
* RouterController.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouterController extends ContainerAware
{
/**
* Renders the profiler panel for the given token.
*
* @param string $token The profiler token
*
* @return Response A Response instance
*/
public function panelAction($token)
{
if (!$router = $this->container->get('router')) {
return new Response('The Router is not enabled.');
}
$profiler = $this->container->get('profiler');
$profiler->disable();
$profile = $profiler->loadProfile($token);
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());
$pathinfo = $profile->getCollector('request')->getPathInfo();
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Router:panel.html.twig', array(
'pathinfo' => $pathinfo,
'traces' => $matcher->getTraces($pathinfo),
));
}
}

View File

@ -11,20 +11,5 @@
{% endblock %}
{% block panel %}
<h2>Routing for "{{ collector.pathinfo }}"</h2>
<table class="routing">
<tr>
<th>Route name</th>
<th>Pattern</th>
<th>Log</th>
</tr>
{% for trace in collector.traces %}
<tr class="{{ 1 == trace.level ? 'almost' : 2 == trace.level ? 'matches' : '' }}">
<td>{{ trace.name }}</td>
<td>{{ trace.pattern }}</td>
<td>{{ trace.log }}</td>
</tr>
{% endfor %}
</table>
{% render "WebProfilerBundle:Router:panel" with {'token': token} %}
{% endblock %}

View File

@ -0,0 +1,16 @@
<h2>Routing for "{{ pathinfo }}"</h2>
<table class="routing">
<tr>
<th>Route name</th>
<th>Pattern</th>
<th>Log</th>
</tr>
{% for trace in traces %}
<tr class="{{ 1 == trace.level ? 'almost' : 2 == trace.level ? 'matches' : '' }}">
<td>{{ trace.name }}</td>
<td>{{ trace.pattern }}</td>
<td>{{ trace.log }}</td>
</tr>
{% endfor %}
</table>