Skip to content

Improve error reporting in router panel of web profiler #17744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;

/**
* RouterController.
Expand Down Expand Up @@ -62,16 +63,39 @@ public function panelAction($token)

$profile = $this->profiler->loadProfile($token);

$context = $this->matcher->getContext();
$context->setMethod($profile->getMethod());
$matcher = new TraceableUrlMatcher($this->routes, $context);

/** @var RequestDataCollector $request */
$request = $profile->getCollector('request');

return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
'request' => $request,
'router' => $profile->getCollector('router'),
'traces' => $matcher->getTraces($request->getPathInfo()),
'traces' => $this->getTraces($request, $profile->getMethod()),
)), 200, array('Content-Type' => 'text/html'));
}

/**
* Returns the routing traces associated to the given request.
*
* @param RequestDataCollector $request
* @param string $method
*
* @return array
*/
private function getTraces(RequestDataCollector $request, $method)
{
$traceRequest = Request::create(
$request->getPathInfo(),
$request->getRequestServer()->get('REQUEST_METHOD'),
$request->getRequestAttributes()->all(),
$request->getRequestCookies()->all(),
array(),
$request->getRequestServer()->all()
);

$context = $this->matcher->getContext();
$context->setMethod($method);
$matcher = new TraceableUrlMatcher($this->routes, $context);

return $matcher->getTracesForRequest($traceRequest);
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Routing\Matcher;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -40,6 +41,15 @@ public function getTraces($pathinfo)
return $this->traces;
}

public function getTracesForRequest(Request $request)
{
$this->request = $request;
$traces = $this->getTraces($request->getPathInfo());
$this->request = null;

return $traces;
}

protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Routing\Tests\Matcher;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
Expand Down Expand Up @@ -98,4 +99,23 @@ public function getLevels($traces)

return $levels;
}

public function testRoutesWithConditions()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo', array(), array(), array(), 'baz', array(), array(), "request.headers.get('User-Agent') matches '/firefox/i'"));

$context = new RequestContext();
$context->setHost('baz');

$matcher = new TraceableUrlMatcher($routes, $context);

$notMatchingRequest = Request::create('/foo', 'GET');
$traces = $matcher->getTracesForRequest($notMatchingRequest);
$this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);

$matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
$traces = $matcher->getTracesForRequest($matchingRequest);
$this->assertEquals('Route matches!', $traces[0]['log']);
}
}