/** * Returns the path to the template file. * * The file locator is used to locate the template when the naming convention * is the symfony one (i.e. the name can be parsed). * Otherwise the template is located using the locator from the twig library. * * @param string|TemplateReferenceInterface $template The template * * @return string The path to the template file * * @throws \Twig_Error_Loader if the template could not be found */ protected function findTemplate($template) { $logicalName = (string) $template; if ($this->activeTheme) { $logicalName .= '|' . $this->activeTheme->getName(); } if (isset($this->cache[$logicalName])) { return $this->cache[$logicalName]; } $file = null; $previous = null; try { $file = parent::findTemplate((string) $template); } catch (\Twig_Error_Loader $e) { $previous = $e; // for BC try { $template = $this->parser->parse($template); $file = $this->locator->locate($template); } catch (\Exception $e) { $previous = $e; } } if (false === $file || null === $file) { throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous); } return $this->cache[$logicalName] = $file; }
/** * Returns a full path for a given file. * * @param TemplateReferenceInterface $template A template * * @return string The full path for the file */ protected function getCacheKey($template) { $name = $template->getLogicalName(); if ($this->activeTheme) { $name .= '|' . $this->activeTheme->getName(); } return $name; }
/** * @param FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { // store into the cookie only if the controller did not already change the value if ($this->newTheme == $this->activeTheme->getName()) { $cookie = new Cookie($this->cookieOptions['name'], $this->newTheme, time() + $this->cookieOptions['lifetime'], $this->cookieOptions['path'], $this->cookieOptions['domain'], (bool) $this->cookieOptions['secure'], (bool) $this->cookieOptions['http_only']); $event->getResponse()->headers->setCookie($cookie); } } }
private function loadStructures($webspaceKey) { $before = $this->activeTheme->getName(); $webspace = $this->webspaceManager->findWebspaceByKey($webspaceKey); $this->activeTheme->setName($webspace->getTheme()->getKey()); $structures = []; $keys = []; foreach ($this->structureManager->getStructures() as $page) { /* @var PageBridge $page */ $template = sprintf('%s.html.twig', $page->getView()); if ($this->templateExists($template)) { $keys[] = $page->getKey(); $structures[] = $page; } } $this->activeTheme->setName($before); $this->cache->save($webspaceKey, $keys); return $structures; }
/** * Returns the file path for a given resource for the first directory it * has a match. * * The resource name must follow the following pattern: * * "@BundleName/path/to/a/file.something" * * where BundleName is the name of the bundle * and the remaining part is the relative path in the bundle. * * @param string $name A resource name to locate * @param string $dir A directory where to look for the resource first * @param bool $first Whether to return the first path or paths for all matching bundles * * @return string|array The absolute path of the resource or an array if $first is false * * @throws \InvalidArgumentException if the file cannot be found or the name is not valid * @throws \RuntimeException if the name contains invalid/unsafe characters */ public function locate($name, $dir = null, $first = true) { // update the paths if the theme changed since the last lookup $theme = $this->activeTheme->getName(); if ($this->lastTheme !== $theme) { $this->setCurrentTheme($theme, $this->activeTheme->getDeviceType()); } if ('@' === $name[0]) { return $this->locateBundleResource($name, $this->path, $first); } if (0 === strpos($name, 'views/')) { if ($res = $this->locateAppResource($name, $this->path, $first)) { return $res; } } return parent::locate($name, $dir, $first); }
/** * @param string $fileName * * @return string */ protected function getCacheFileName($fileName) { return $this->appPaths->getCacheDir() . '/vsymfo_webui_config/' . $this->theme->getName() . '/' . str_replace(['/', '\\', '.'], '_', $fileName); }
/** * {@inheritdoc} */ public function createDocument(array $options = []) { $document = new HtmlDocument(); // used by Modernizr $document->element('html')->addClass('no-js'); // meta viewport $viewport = $this->params["meta_viewport"]; if (!empty($viewport)) { $meta = $document->element('viewport'); $meta->attr('content', $viewport); } // favicon $favicon = $document->element('favicon'); $favicon->enable($this->params["favicon_enable"]); $favicon->setBasePath($this->appPaths->url('web_theme') . '/favicons'); $favicon->setTileColor($this->params["favicon_tile_color"]); if (file_exists($this->appPaths->absolute('web_theme') . '/favicons/favicon.html')) { $favicon->setFaviconTemplate(file_get_contents($this->appPaths->absolute('web_theme') . '/favicons/favicon.html')); } // html lang $document->element('html')->attr('lang', $this->requestStack->getCurrentRequest()->getLocale()); // resources $utility = $this->getUtility($this->params); $utility->createResOnAdd($document, "javascript", "default"); $utility->createResOnAdd($document, "stylesheet", "default"); $document->resources("javascript")->chooseOnAdd("default"); $document->resources("stylesheet")->chooseOnAdd("default"); $configDir = $this->appPaths->getRootDir() . '/document'; if (file_exists($configDir . '/' . $this->theme->getName())) { $configDir .= '/' . $this->theme->getName(); } $locator = new FileLocator($configDir); $loader = $utility->createResourcesLoader($document, 'javascript', $locator, '/'); $loader->load('html_resources.yml', 'framework'); $loader->load('html_resources.yml', 'core'); $locator = new FileLocator($this->appPaths->absolute('private_theme')); $loader = $utility->createResourcesLoader($document, 'stylesheet', $locator, $this->appPaths->getThemePath() . '/src'); $loader->load('html_resources.yml', 'theme'); $loader = $utility->createResourcesLoader($document, 'javascript', $locator, $this->appPaths->getThemePath() . '/src'); $loader->load('html_resources.yml', 'theme'); // js initializer $jsloader = $this->getJsLoader(); $script = $document->element('script'); $twig = $this->twig; $env = $this->env; $document->setScriptOutput(function (JavaScriptResourceManager $manager, array $translations) use($jsloader, $script, $twig, $env) { $output = $jsloader->render('html'); if ($env === 'dev') { foreach ($jsloader->resources() as $resource) { if ($resource instanceof CombineResourceInterface && $resource->getCombineObject()->getException() instanceof \Exception) { throw $resource->getCombineObject()->getException(); } } } $output .= $twig->render('::head.html.twig', ["resources" => $manager->render('array'), "translations" => $translations]); if (!$script->isEmpty()) { $output .= '<script type="text/javascript">' . $script->render() . '</script>'; $script->update(function () { return ''; }); } return $output; }); // translations used in theme if (file_exists($this->appPaths->absolute("kernel_root") . '/theme/' . $this->appPaths->getThemeName() . '/translations.yml')) { $trans = $this->translator; $locator = new FileLocator($this->appPaths->absolute("kernel_root") . '/theme/' . $this->appPaths->getThemeName()); $loader = new TranslationLoader($locator, ['cache_dir' => $this->appPaths->absolute("kernel_cache") . "/vsymfo_document/translations", 'cache_refresh' => $this->env == "dev", 'document' => $document, 'trans_closure' => function ($text) use($trans) { return $trans->trans($text); }]); $loader->load('translations.yml'); } return $document; }
/** * @covers Liip\ThemeBundle\ActiveTheme::__construct * @covers Liip\ThemeBundle\ActiveTheme::getName */ public function testGetName() { $theme = new ActiveTheme('foo', array('foo')); $this->assertEquals('foo', $theme->getName()); }
/** * @dataProvider dataProvider */ public function testThemeAction($config, $assertion, $hasAlreadyACookie = true) { $activeTheme = new ActiveTheme($config['active_theme'], $config['themes']); $device = null; if ($config['autodetect_theme']) { $device = $this->getDeviceDetectionMock('autodetect'); } $response = new \Symfony\Component\HttpFoundation\Response(); $request = new \Symfony\Component\HttpFoundation\Request(); if ($hasAlreadyACookie) { $request = $this->getMockRequest('cookie'); } $controller = false; if ($config['load_controllers']) { $controller = new ThemeController($activeTheme, $config['themes'], $config['cookie']); } $listener = new ThemeRequestListener($activeTheme, $config['cookie'], $device); $listener->onKernelRequest($this->getEventMock($request, $response, 'Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')); $this->assertEquals($activeTheme->getName(), $assertion['themeAfterKernelRequest']); if ($controller) { $response = $controller->switchAction($this->getMockRequest($assertion['themeAfterController'])); $this->assertCookieValue($response, $assertion['themeAfterController']); } // onResponse will not set the cookie if the controller modified it $listener->onKernelResponse($this->getEventMock($request, $response, 'Symfony\\Component\\HttpKernel\\Event\\FilterResponseEvent')); $this->assertCookieValue($response, $assertion['themeAfterKernelResponse']); }