Skip to content

Commit

Permalink
[WebProfileBundle] remove dependency on the DIC
Browse files Browse the repository at this point in the history
The controllers are not relying on the DIC anymore and only Twig
is used for rendering (instead of the Templating component).

The Exception controller has not been updated yet as it relies on many
external dependencies (and other bundles).
  • Loading branch information
fabpot committed Oct 13, 2012
1 parent dfc53b0 commit a3b3c28
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 81 deletions.
115 changes: 58 additions & 57 deletions src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php
Expand Up @@ -11,22 +11,37 @@

namespace Symfony\Bundle\WebProfilerBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* ProfilerController.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ProfilerController extends ContainerAware
class ProfilerController
{
protected $templateManager;
private $templateManager;
private $generator;
private $profiler;
private $twig;
private $templates;
private $toolbarPosition;

public function __construct(UrlGeneratorInterface $generator, Profiler $profiler, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
{
$this->generator = $generator;
$this->profiler = $profiler;
$this->twig = $twig;
$this->templates = $templates;
$this->toolbarPosition = $toolbarPosition;
}

/**
* Renders a profiler panel for the given token.
Expand All @@ -38,21 +53,20 @@ class ProfilerController extends ContainerAware
*/
public function panelAction(Request $request, $token)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

$panel = $request->query->get('panel', 'request');
$page = $request->query->get('page', 'home');

if (!$profile = $profiler->loadProfile($token)) {
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:info.html.twig', array('about' => 'no_token', 'token' => $token));
if (!$profile = $this->profiler->loadProfile($token)) {
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array('about' => 'no_token', 'token' => $token)));
}

if (!$profile->hasCollector($panel)) {
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
}

return $this->container->get('templating')->renderResponse($this->getTemplateManager()->getName($profile, $panel), array(
return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
'token' => $token,
'profile' => $profile,
'collector' => $profile->getCollector($panel),
Expand All @@ -61,7 +75,9 @@ public function panelAction(Request $request, $token)
'request' => $request,
'templates' => $this->getTemplateManager()->getTemplates($profile),
'is_ajax' => $request->isXmlHttpRequest(),
));
// for BC compatibility
'app' => array('request' => $request),
)));
}

/**
Expand All @@ -73,14 +89,13 @@ public function panelAction(Request $request, $token)
*/
public function exportAction($token)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

if (!$profile = $profiler->loadProfile($token)) {
if (!$profile = $this->profiler->loadProfile($token)) {
throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
}

return new Response($profiler->export($profile), 200, array(
return new Response($this->profiler->export($profile), 200, array(
'Content-Type' => 'text/plain',
'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
));
Expand All @@ -93,11 +108,10 @@ public function exportAction($token)
*/
public function purgeAction()
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$profiler->purge();
$this->profiler->disable();
$this->profiler->purge();

return new RedirectResponse($this->container->get('router')->generate('_profiler_info', array('about' => 'purge')));
return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'purge')));
}

/**
Expand All @@ -107,22 +121,19 @@ public function purgeAction()
*/
public function importAction(Request $request)
{
$profiler = $this->container->get('profiler');
$profiler->disable();

$router = $this->container->get('router');
$this->profiler->disable();

$file = $request->files->get('file');

if (empty($file) || !$file->isValid()) {
return new RedirectResponse($router->generate('_profiler_info', array('about' => 'upload_error')));
}

if (!$profile = $profiler->import(file_get_contents($file->getPathname()))) {
return new RedirectResponse($router->generate('_profiler_info', array('about' => 'already_exists')));
if (!$profile = $this->profiler->import(file_get_contents($file->getPathname()))) {
return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'already_exists')));
}

return new RedirectResponse($router->generate('_profiler', array('token' => $profile->getToken())));
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $profile->getToken())));
}

/**
Expand All @@ -134,12 +145,11 @@ public function importAction(Request $request)
*/
public function infoAction($about)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:info.html.twig', array(
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
'about' => $about
));
)));
}

/**
Expand All @@ -164,31 +174,30 @@ public function toolbarAction(Request $request, $token, $position = null)
return new Response();
}

$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

if (!$profile = $profiler->loadProfile($token)) {
if (!$profile = $this->profiler->loadProfile($token)) {
return new Response();
}

if (null === $position) {
$position = $this->container->getParameter('web_profiler.debug_toolbar.position');
$position = $this->toolbarPosition;
}

$url = null;
try {
$url = $this->container->get('router')->generate('_profiler', array('token' => $token));
$url = $this->generator->generate('_profiler', array('token' => $token));
} catch (\Exception $e) {
// the profiler is not enabled
}

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
'position' => $position,
'profile' => $profile,
'templates' => $this->getTemplateManager()->getTemplates($profile),
'profiler_url' => $url,
'token' => $token,
));
)));
}

/**
Expand All @@ -200,8 +209,7 @@ public function toolbarAction(Request $request, $token, $position = null)
*/
public function searchBarAction(Request $request)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

if (null === $session = $request->getSession()) {
$ip =
Expand All @@ -217,13 +225,13 @@ public function searchBarAction(Request $request)
$token = $session->get('_profiler_search_token');
}

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
return new Response($this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
'token' => $token,
'ip' => $ip,
'method' => $method,
'url' => $url,
'limit' => $limit,
));
)));
}

/**
Expand All @@ -236,26 +244,25 @@ public function searchBarAction(Request $request)
*/
public function searchResultsAction(Request $request, $token)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

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

$ip = $request->query->get('ip');
$method = $request->query->get('method');
$url = $request->query->get('url');
$limit = $request->query->get('limit');

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
'token' => $token,
'profile' => $profile,
'tokens' => $profiler->find($ip, $url, $limit, $method),
'tokens' => $this->profiler->find($ip, $url, $limit, $method),
'ip' => $ip,
'method' => $method,
'url' => $url,
'limit' => $limit,
'panel' => null,
));
)));
}

/**
Expand All @@ -267,8 +274,7 @@ public function searchResultsAction(Request $request, $token)
*/
public function searchAction(Request $request)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
$method = $request->query->get('method');
Expand All @@ -285,12 +291,12 @@ public function searchAction(Request $request)
}

if (!empty($token)) {
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)));
}

$tokens = $profiler->find($ip, $url, $limit, $method);
$tokens = $this->profiler->find($ip, $url, $limit, $method);

return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array(
return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
'token' => $tokens ? $tokens[0]['token'] : 'empty',
'ip' => $ip,
'method' => $method,
Expand All @@ -306,8 +312,7 @@ public function searchAction(Request $request)
*/
public function phpinfoAction()
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

ob_start();
phpinfo();
Expand All @@ -319,11 +324,7 @@ public function phpinfoAction()
protected function getTemplateManager()
{
if (null === $this->templateManager) {
$this->templateManager = new TemplateManager(
$this->container->get('profiler'),
$this->container->get('twig'),
$this->container->getParameter('data_collector.templates')
);
$this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
}

return $this->templateManager;
Expand Down
Expand Up @@ -11,17 +11,36 @@

namespace Symfony\Bundle\WebProfilerBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Profiler\Profiler;

/**
* RouterController.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouterController extends ContainerAware
class RouterController
{
private $profiler;
private $twig;
private $matcher;
private $routes;

public function __construct(Profiler $profiler, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, $routes = null)
{
$this->profiler = $profiler;
$this->twig = $twig;
$this->matcher = $matcher;
$this->routes = $routes;

if (null === $this->routes && null !== $this->matcher && $this->matcher instanceof RouterInterface) {
$this->routes = $matcher->getRouteCollection();
}
}

/**
* Renders the profiler panel for the given token.
*
Expand All @@ -31,26 +50,24 @@ class RouterController extends ContainerAware
*/
public function panelAction($token)
{
$profiler = $this->container->get('profiler');
$profiler->disable();
$this->profiler->disable();

if (!$this->container->has('router')) {
if (null === $this->matcher || null === $this->routes) {
return new Response('The Router is not enabled.');
}
$router = $this->container->get('router');

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

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

$request = $profile->getCollector('request');

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Router:panel.html.twig', array(
return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
'request' => $request,
'router' => $profile->getCollector('router'),
'traces' => $matcher->getTraces($request->getPathInfo()),
));
)));
}
}
Expand Up @@ -43,6 +43,7 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('profiler.xml');
$loader->load('toolbar.xml');

$container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']);
Expand Down

0 comments on commit a3b3c28

Please sign in to comment.