/** * 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; }
public function testWithoutParams() { $activeTheme = new ActiveTheme('b', array('a', 'b')); $activeTheme->setName('a'); $listener = new ThemeControllerListener($activeTheme); $listener->onKernelRequest($this->getResponseEventMock()); $this->assertFalse($this->request->attributes->has('_controller')); $this->assertFalse($this->request->attributes->has('_route_params')); }
/** * Set the active theme if there is a portal. * * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $portal = $this->requestAnalyzer->getPortal(); if (null === $portal) { return; } $themeKey = $portal->getWebspace()->getTheme()->getKey(); $this->activeTheme->setName($themeKey); }
/** * @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); } } }
/** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if ($this->router instanceof I18nRouter) { $collection = $this->router->getOriginalRouteCollection(); } else { $collection = $this->router->getRouteCollection(); } $route = $collection->get($request->get('_route')); if (!empty($route) && $route->hasOption('theme_group')) { $group = $route->getOption('theme_group'); $this->theme->setName($group . '_' . $this->container->getParameter('vsymfo_core.theme_' . $group)); } }
/** * @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']); }
/** * {@inheritdoc} */ public function onKernelException(GetResponseForExceptionEvent $event) { $isPanel = false; foreach ($this->accessMap->getPatterns($event->getRequest()) as $pattern) { if (is_array($pattern)) { foreach ($pattern as $role) { if (is_string($role) && $role === 'ROLE_PANEL_ACCESS') { $isPanel = true; break; } } } } if ($isPanel) { $this->activeTheme->setName('backend_' . $this->backendTheme); } $this->loginBlockedException($event); }
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; }
/** * renders content with the real website controller. * * @param PageBridge $content * @param bool $partial * * @return string */ public function render(PageBridge $content, $partial = false) { // set active theme $webspace = $this->webspaceManager->findWebspaceByKey($content->getWebspaceKey()); $this->activeTheme->setName($webspace->getTheme()->getKey()); // get controller and invoke action $request = new Request(); $request->attributes->set('_controller', $content->getController()); $controller = $this->controllerResolver->getController($request); // prepare locale for translator and request $request->setLocale($content->getLanguageCode()); $localeBefore = $this->translator->getLocale(); $this->translator->setLocale($content->getLanguageCode()); $this->requestStack->push($request); /** @var Response $response */ $response = $controller[0]->{$controller[1]}($content, true, $partial); // roll back $this->requestStack->pop(); $this->translator->setLocale($localeBefore); return $response->getContent(); }
/** * Locate Resource Theme aware. Only working for app/Resources. * * @param string $name * @param string $dir * @param bool $first * * @return string|array */ protected function locateAppResource($name, $dir = null, $first = true) { if (false !== strpos($name, '..')) { throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); } $files = array(); $parameters = array('%app_path%' => $this->path, '%current_theme%' => $this->lastTheme, '%current_device%' => $this->activeTheme->getDeviceType(), '%template%' => substr($name, strlen('views/'))); foreach ($this->getPathsForAppResource($parameters) as $checkPaths) { if (file_exists($checkPaths)) { if ($first) { return $checkPaths; } $files[] = $checkPaths; } } return $files; }
/** * @contain Liip\ThemeBundle\Locator\FileLocator::locate */ public function testLocateActiveThemeUpdate() { $kernel = $this->getKernelMock(); $activeTheme = new ActiveTheme('foo', array('foo', 'bar', 'foobar')); $fileLocator = new FileLocatorFake($kernel, $activeTheme, $this->getFixturePath() . '/rootdir/Resources'); $this->assertEquals('foo', $fileLocator->lastTheme); $activeTheme->setName('bar'); $fileLocator->locate('Resources/themes/foo/template', $this->getFixturePath(), true); $this->assertEquals('bar', $fileLocator->lastTheme); }
/** * @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 setName($name) { $this->init(); parent::setName($name); }
/** * {@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; }
/** * @expectedException InvalidArgumentException * @covers Liip\ThemeBundle\ActiveTheme::setName */ public function testSetInvalidName() { $theme = new ActiveTheme('foo', array('foo')); $theme->setName('bar'); }