getPath() public method

Returns the path to the template - as a path when the template is not part of a bundle - as a resource when the template is part of a bundle
public getPath ( ) : string
return string A path to the template or a resource
 /**
  * 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');
 }
Example #4
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()
        );
    }
 /**
  * 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());
 }
Example #6
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;
 }