Author: Victor Berchet (victor@suumit.com)
Inheritance: extends Symfony\Component\Templating\TemplateReference
 /**
  * {@inheritdoc}
  */
 public function parse($name)
 {
     if ($name instanceof TemplateReferenceInterface) {
         return $name;
     } elseif (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     // normalize name
     $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', str_replace('\\', '/', $name)));
     if (false !== strpos($name, '..')) {
         throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
     }
     if (!preg_match('/^([^:]*):([^:]*):(.+)\\.([^\\.]+)\\.([^\\.]+)$/', $name, $matches)) {
         return parent::parse($name);
     }
     $template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
     if ($template->get('bundle')) {
         try {
             $this->kernel->getBundle($template->get('bundle'));
         } catch (\Exception $e) {
             throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
         }
     }
     return $this->cache[$name] = $template;
 }
 /**
  * {@inheritdoc}
  */
 public function parse($name)
 {
     if ($name instanceof TemplateReferenceInterface) {
         return $name;
     } else {
         if (isset($this->cache[$name])) {
             return $this->cache[$name];
         }
     }
     // normalize name
     $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
     if (false !== strpos($name, '..')) {
         throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
     }
     $parts = explode(':', $name);
     if (3 !== count($parts)) {
         throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
     }
     $elements = explode('.', $parts[2]);
     if (3 !== count($elements)) {
         throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
     }
     $template = new TemplateReference($parts[0], $parts[1], $elements[0], $elements[1], $elements[2]);
     if ($template->get('bundle')) {
         try {
             $this->kernel->getBundle($template->get('bundle'));
         } catch (\Exception $e) {
             throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
         }
     }
     return $this->cache[$name] = $template;
 }
 /**
  * Collects data for the given Request and Response.
  *
  * @param Request    $request   A Request instance
  * @param Response   $response  A Response instance
  * @param \Exception $exception An Exception instance
  *
  * @api
  */
 public function collect(Request $request, Response $response, \Exception $exception = null)
 {
     $messages = array();
     $content = $response->getContent();
     $path = '';
     $fileContent = '';
     $template = $request->attributes->get('_template');
     if (is_null($template)) {
         $templateName = $request->attributes->get('template');
         list($front, $format, $engine) = explode('.', $templateName);
         $templateParts = explode(":", $front);
         $template = new TemplateReference($templateParts[0], $templateParts[1], $templateParts[2], $format, $engine);
     }
     if ($template instanceof TemplateReference) {
         $locale = $request->attributes->get('_locale', $request->getLocale());
         $catalogue = new MessageCatalogue($locale);
         $path = $template->getPath();
         /**
          * Check if $path is a resource
          */
         if (strstr($path, '@')) {
             $kernel = $this->container->get('kernel');
             $path = $kernel->locateResource($path);
             $fileContent = file_get_contents($path);
         }
         $this->twigExtractor->extractTemplate($path, $catalogue);
         $messages = $catalogue->all();
     }
     $this->data = array('content' => $content, 'file_content' => $fileContent, 'messages' => $messages, 'path' => $path);
 }
 public function testLocateATemplate()
 {
     $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
     $fileLocator = $this->getFileLocator();
     $fileLocator->expects($this->once())->method('locate')->with($template->getPath())->will($this->returnValue('/path/to/template'));
     $locator = new TemplateLocator($fileLocator);
     $this->assertEquals('/path/to/template', $locator->locate($template));
 }
 public function testWarmUp()
 {
     $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
     $this->templateFinder->expects($this->once())->method('findAllTemplates')->will($this->returnValue(array($template)));
     $this->fileLocator->expects($this->once())->method('locate')->with($template->getPath())->will($this->returnValue(dirname($this->tmpDir) . '/path/to/template.html.twig'));
     $warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
     $warmer->warmUp($this->tmpDir);
     $this->assertFileEquals(__DIR__ . '/../Fixtures/TemplatePathsCache/templates.php', $this->tmpDir . '/templates.php');
 }
Beispiel #6
0
    public function testGetPathWorksWithNamespacedControllers()
    {
        $reference = new TemplateReference('AcmeBlogBundle', 'Admin\Post', 'index', 'html', 'twig');

        $this->assertSame(
            '@AcmeBlogBundle/Resources/views/Admin/Post/index.html.twig',
            $reference->getPath()
        );
    }
 /**
  * {@inheritdoc}
  */
 public function parse($name)
 {
     if ($name instanceof TemplateReferenceInterface) {
         return $name;
     } elseif (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     if (!preg_match('/^(?:@([^\\/]*)|)(?:\\/(.+))?\\/(.+)\\.([^\\.]+)\\.([^\\.]+)$/', $name, $matches)) {
         return $this->decoratedParser->parse($name);
     }
     $template = new TemplateReference($matches[1] ? $matches[1] . 'Bundle' : '', $matches[2], $matches[3], $matches[4], $matches[5]);
     if ($template->get('bundle')) {
         try {
             $this->kernel->getBundle($template->get('bundle'));
         } catch (\Exception $e) {
             throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
         }
     }
     return $this->cache[$name] = $template;
 }
 /**
  * @param string|TemplateReferenceInterface $template
  * @return string
  */
 protected function findTemplate($template)
 {
     $logicalName = (string) $template;
     if (isset($this->cache[$logicalName])) {
         return $this->cache[$logicalName];
     }
     if ($this->request === null && $this->container->isScopeActive('request')) {
         $this->request = $this->container->get('request');
     }
     $locale = $this->request ? $this->request->attributes->get('_locale') : 'en';
     if (is_string($template)) {
         if (strpos($template, ':')) {
             try {
                 $template = $this->parser->parse($template);
             } catch (\Exception $e) {
             }
         } else {
             return parent::findTemplate($template);
         }
     }
     if ($locale !== 'en') {
         $params = $template->all();
         $localizedTemplate = new TemplateReference($params['bundle'], $params['controller'], $params['name'], $params['format'], $params['engine']);
         if ($params['controller']) {
             $localizedTemplate->set('controller', $locale . '/' . $params['controller']);
         } else {
             $localizedTemplate->set('name', $locale . '/' . $params['name']);
         }
         try {
             return parent::findTemplate($localizedTemplate);
         } catch (\Twig_Error_Loader $e) {
             return parent::findTemplate($template);
         }
     }
     return parent::findTemplate($template);
 }
 /**
  * Locates and appends template to an array
  *
  * @param FileLocatorInterface $locator
  * @param TemplateReference    $template
  * @param array                $templates
  */
 protected function locateTemplate(FileLocatorInterface $locator, TemplateReference $template, array &$templates)
 {
     $templates[$template->getLogicalName()] = $locator->locate($template->getPath());
 }
Beispiel #10
0
 /**
  * @param \TYPO3\Fluid\View\StandaloneView $view
  * @param \Symfony\Bundle\FrameworkBundle\Templating\TemplateReference $templateReference
  * @return void
  */
 protected function addTemplateSource(StandaloneView &$view, TemplateReference $templateReference)
 {
     $translatedPaths = $this->environment->getTranslatedTemplatePaths();
     $templateRootPath = $translatedPaths['templateRootPath'];
     list($bundleName, $controllerName, $file) = explode(':', $templateReference->getLogicalName());
     list($action, $format, $engine) = explode('.', $file);
     $templatePathAndFilename = $templateRootPath . $controllerName . '/' . $action . '.' . $format . '.' . $engine;
     $view->setTemplatePathAndFilename($templatePathAndFilename);
 }
Beispiel #11
0
 /**
  * {@inheritdoc}
  */
 public function getPath()
 {
     $controller = str_replace('\\', '/', $this->get('controller'));
     if (!empty($this->themeOverride)) {
         try {
             $theme = $this->themeHelper->getTheme($this->themeOverride);
             $themeDir = $theme->getThemePath();
         } catch (\Exception $e) {
         }
     } else {
         $theme = $this->themeHelper->getTheme();
         $themeDir = $theme->getThemePath();
     }
     $fileName = $this->get('name') . '.' . $this->get('format') . '.' . $this->get('engine');
     $path = (empty($controller) ? '' : $controller . '/') . $fileName;
     if (!empty($this->parameters['bundle'])) {
         $bundleRoot = $this->pathsHelper->getSystemPath('bundles', true);
         $pluginRoot = $this->pathsHelper->getSystemPath('plugins', true);
         // Check for a system-wide override
         $themePath = $this->pathsHelper->getSystemPath('themes', true);
         $systemTemplate = $themePath . '/system/' . $this->parameters['bundle'] . '/' . $path;
         if (file_exists($systemTemplate)) {
             $template = $systemTemplate;
         } else {
             //check for an override and load it if there is
             if (!empty($themeDir) && file_exists($themeDir . '/html/' . $this->parameters['bundle'] . '/' . $path)) {
                 // Theme override
                 $template = $themeDir . '/html/' . $this->parameters['bundle'] . '/' . $path;
             } else {
                 preg_match('/Mautic(.*?)Bundle/', $this->parameters['bundle'], $match);
                 if (!empty($match[1]) && file_exists($bundleRoot . '/' . $match[1] . 'Bundle/Views/' . $path) || file_exists($pluginRoot . '/' . $this->parameters['bundle'] . '/Views/' . $path)) {
                     // Mautic core template
                     $template = '@' . $this->get('bundle') . '/Views/' . $path;
                 }
             }
         }
     } else {
         $themes = $this->themeHelper->getInstalledThemes();
         if (isset($themes[$controller])) {
             //this is a file in a specific Mautic theme folder
             $theme = $this->themeHelper->getTheme($controller);
             $template = $theme->getThemePath() . '/html/' . $fileName;
         }
     }
     if (empty($template)) {
         //try the parent
         return parent::getPath();
     }
     return $template;
 }
Beispiel #12
0
 /**
  * {@inheritdoc}
  */
 public function getLogicalName()
 {
     $logicalName = parent::getLogicalName();
     if (!empty($this->themeOverride)) {
         $logicalName = $this->themeOverride . '|' . $logicalName;
     }
     return $logicalName;
 }