Skip to content

Commit

Permalink
generating template names moved out from controller to another class
Browse files Browse the repository at this point in the history
  • Loading branch information
wodor committed Feb 16, 2012
1 parent 6138e80 commit abd0eb7
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function panelAction(Request $request, $token)
$panel = $this->container->get('request')->query->get('panel', 'request');
$page = $this->container->get('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));
}
Expand All @@ -49,13 +50,17 @@ public function panelAction(Request $request, $token)
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
}

return $this->container->get('templating')->renderResponse($this->getTemplateName($profile, $panel), array(
/** @var $templateManager \Symfony\Bundle\WebProfilerBundle\Profiler\Template */
$templateManager = $this->container->get('web_profiler.profiler_template');
$templateManager->setProfiler($profiler);

return $this->container->get('templating')->renderResponse($templateManager->getName($profile, $panel), array(
'token' => $token,
'profile' => $profile,
'collector' => $profile->getCollector($panel),
'panel' => $panel,
'page' => $page,
'templates' => $this->getTemplates($profile),
'templates' => $templateManager->getTemplates($profile),
'is_ajax' => $request->isXmlHttpRequest(),
));
}
Expand Down Expand Up @@ -178,10 +183,14 @@ public function toolbarAction($token, $position = null)
// the profiler is not enabled
}

/** @var $templateManager \Symfony\Bundle\WebProfilerBundle\Profiler\Template */
$templateManager = $this->container->get('web_profiler.profiler_template');
$templateManager->setProfiler($profiler);

return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
'position' => $position,
'profile' => $profile,
'templates' => $this->getTemplates($profile),
'templates' => $templateManager->getTemplates($profile),
'profiler_url' => $url,
'verbose' => $this->container->get('web_profiler.debug_toolbar')->isVerbose()
));
Expand Down Expand Up @@ -292,60 +301,4 @@ public function searchAction()
)));
}

/**
* Gets template names of templates that are
* present in the viewed profile
*
* @param $profile
* @return array
* @throws \UnexpectedValueException
*/
protected function getTemplateNames($profile)
{
$templates = array();

foreach ($this->container->getParameter('data_collector.templates') as $arguments) {
if (null === $arguments) {
continue;
}

list($name, $template) = $arguments;
if (!$profile->hasCollector($name)) {
continue;
}

if ('.html.twig' === substr($template, -10)) {
$template = substr($template, 0, -10);
}

if (!$this->container->get('templating')->exists($template.'.html.twig')) {
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
}

$templates[$name] = $template.'.html.twig';
}

return $templates;
}

protected function getTemplateName($profile, $panel)
{
$templates = $this->getTemplateNames($profile);

if (!isset($templates[$panel])) {
throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
}

return $templates[$panel];
}

protected function getTemplates($profile)
{
$templates = $this->getTemplateNames($profile);
foreach ($templates as $name => $template) {
$templates[$name] = $this->container->get('twig')->loadTemplate($template);
}

return $templates;
}
}
133 changes: 133 additions & 0 deletions src/Symfony/Bundle/WebProfilerBundle/Profiler/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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\Profiler;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Bundle\TwigBundle\TwigEngine;

/**
* ProfilerController.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Artur Wielogórski <wodor@wodor.net>
*/
class Template {

/**
* @var \Symfony\Bundle\TwigBundle\TwigEngine
*/
public $templating;

/**
* @var \Twig_Environment
*/
public $twig;

/**
* @var array
*/
public $templates;

/**
* @var \Symfony\Component\HttpKernel\Profiler\Profiler
*/
protected $profiler;

/**
* @param \Symfony\Bundle\TwigBundle\TwigEngine $templating
* @param \Twig_Environment $twig
* @param array $templates
*/
public function __construct(TwigEngine $templating, \Twig_Environment $twig, array $templates)
{
$this->templating = $templating;
$this->twig = $twig;
$this->templates = $templates;
}

/**
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @param $panel
* @return mixed
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function getName(Profile $profile, $panel)
{
$templates = $this->getNames($profile);

if (!isset($templates[$panel])) {
throw new NotFoundHttpException(sprintf('Panel "%s" is not registered in profiler or is not present in viewed profile.', $panel));
}

return $templates[$panel];
}

/**
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @return array
*/
public function getTemplates(Profile $profile)
{
$templates = $this->getNames($profile);
foreach ($templates as $name => $template) {
$templates[$name] = $this->twig->loadTemplate($template);
}

return $templates;
}

/**
* @param \Symfony\Component\HttpKernel\Profiler\Profiler $profiler
*/
public function setProfiler(Profiler $profiler)
{
$this->profiler = $profiler;
}

/**
* Gets template names of templates that are
* present in the viewed profile
* @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
* @return array
* @throws \UnexpectedValueException
*/
protected function getNames(Profile $profile)
{
$templates = array();

foreach ($this->templates as $arguments) {
if (null === $arguments) {
continue;
}

list($name, $template) = $arguments;

if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
continue;
}

if ('.html.twig' === substr($template, -10)) {
$template = substr($template, 0, -10);
}

if (!$this->templating->exists($template.'.html.twig')) {
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
}

$templates[$name] = $template.'.html.twig';
}

return $templates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="web_profiler.debug_toolbar.class">Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener</parameter>
<parameter key="web_profiler.profiler_template">Symfony\Bundle\WebProfilerBundle\Profiler\Template</parameter>
</parameters>

<services>
Expand All @@ -16,5 +17,11 @@
<argument>%web_profiler.debug_toolbar.mode%</argument>
<argument>%web_profiler.debug_toolbar.position%</argument>
</service>

<service id="web_profiler.profiler_template" class="%web_profiler.profiler_template%">
<argument type="service" id="templating.engine.twig" />
<argument type="service" id="twig" />
<argument>%data_collector.templates%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function setUp()
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
$this->container->setParameter('kernel.root_dir', __DIR__);
$this->container->setParameter('data_collector.templates', array());
$this->container->set('kernel', $this->kernel);
}

Expand Down
Loading

1 comment on commit abd0eb7

@wodor
Copy link
Owner Author

@wodor wodor commented on abd0eb7 Feb 16, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought passing over two arguments (profile and profiler) through 3 methods isn't nice, also I wanted to be able to write tests, so I've pulled out that methods. Although I'm not happy with the name of this class (Profiler\Template).

If this change is too invasive i can revert to passing profile and profiler.

How to move these changes to 2.0 easily ?

Please sign in to comment.