You are here

class TemplateManager in Devel 4.x

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Profiler/TemplateManager.php \Drupal\webprofiler\Profiler\TemplateManager
  2. 8 webprofiler/src/Profiler/TemplateManager.php \Drupal\webprofiler\Profiler\TemplateManager
  3. 8.2 webprofiler/src/Profiler/TemplateManager.php \Drupal\webprofiler\Profiler\TemplateManager

Profiler Templates Manager.

Hierarchy

Expanded class hierarchy of TemplateManager

2 files declare their use of TemplateManager
DashboardController.php in webprofiler/src/Controller/DashboardController.php
ToolbarController.php in webprofiler/src/Controller/ToolbarController.php
1 string reference to 'TemplateManager'
webprofiler.services.yml in webprofiler/webprofiler.services.yml
webprofiler/webprofiler.services.yml
1 service uses TemplateManager
template_manager in webprofiler/webprofiler.services.yml
Drupal\webprofiler\Profiler\TemplateManager

File

webprofiler/src/Profiler/TemplateManager.php, line 21

Namespace

Drupal\webprofiler\Profiler
View source
class TemplateManager {

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

  /**
   * @var \Twig_Loader_Chain
   */
  protected $twigLoader;

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

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

  /**
   * Constructor.
   *
   * @param \Symfony\Component\HttpKernel\Profiler\Profiler $profiler
   * @param \Twig_Environment $twig
   * @param \Twig_Loader_Chain $twigLoader
   * @param array $templates
   */
  public function __construct(SymfonyProfiler $profiler, \Twig_Environment $twig, \Twig_Loader_Chain $twigLoader, array $templates) {
    $this->profiler = $profiler;
    $this->twig = $twig;
    $this->twigLoader = $twigLoader;
    $this->templates = $templates;
  }

  /**
   * Gets the template name for a given panel.
   *
   * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
   * @param string $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];
  }

  /**
   * Gets the templates for a given profile.
   *
   * @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;
  }

  /**
   * 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 = [];
    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->twigLoader
        ->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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TemplateManager::$profiler protected property
TemplateManager::$templates protected property
TemplateManager::$twig protected property
TemplateManager::$twigLoader protected property
TemplateManager::getName public function Gets the template name for a given panel.
TemplateManager::getNames protected function Gets template names of templates that are present in the viewed profile.
TemplateManager::getTemplates public function Gets the templates for a given profile.
TemplateManager::__construct public function Constructor.