/** * Warms up the cache. * * @param string $cacheDir The cache directory */ public function warmUp($cacheDir) { if (empty($this->themeManager)) { return; } $locator = $this->locator->getLocator(); $allTemplates = $this->finder->findAllTemplates(); $curTheme = $this->themeManager->getActiveTheme(); $templates = array(); foreach ($this->themeManager->getThemes() as $theme) { $this->themeManager->setActiveTheme($theme); foreach ($allTemplates as $template) { $templates[$template->getLogicalName() . '|' . $theme] = $locator->locate($template->getPath()); } } $this->themeManager->setActiveTheme($curTheme); $this->writeCacheFile($cacheDir . '/templates.php', sprintf('<?php return %s;', var_export($templates, true))); }
/** * Returns a full path for a given file. * * @param TemplateReferenceInterface $template A template * @param string $currentPath Unused * @param Boolean $first Unused * * @return string The full path for the file * * @throws \InvalidArgumentException When the template is not an instance of TemplateReferenceInterface * @throws \InvalidArgumentException When the template file can not be found */ public function locate($template, $currentPath = null, $first = true) { if (!$template instanceof TemplateReferenceInterface) { throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface."); } $curTheme = $this->themeManager->getActiveTheme(); if ($template instanceof ThemeTemplateReference && $template->get('theme')) { $this->themeManager->setActiveTheme($template->get('theme')); } $key = $this->getCacheKey($template); if (!isset($this->cache[$key])) { try { $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException(sprintf('Unable to find template "%s" in "%s".', $template, $e->getMessage()), 0, $e); } } $this->themeManager->setActiveTheme($curTheme); return $this->cache[$key]; }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { $this->themeManager->resolveTheme($event->getRequest()); } }