Example #1
0
 public function register(Application $app, array $options = array())
 {
     $config = $app->getConfig();
     if (!empty($config[$this->name])) {
         $fs_twig_loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../../../../../src/' . $config[$this->name]['path']);
         $loader = new \Twig_Loader_Chain(array($fs_twig_loader));
         $twig = new \Twig_Environment($loader);
         // this extension includes the asset() function
         $twig->addExtension(new TwigCoreExtension($app));
         // the routing extension is required to reference routes in twig templates
         // e.g <a href="{{ path('Some_route')}}">click</a>
         $twig->addExtension(new RoutingExtension($app->shared['url.generator']));
         if ($app->getProvider('translator') !== null) {
             $twig->addExtension(new TranslationExtension($app->getProvider('translator')));
         }
         if ($app->getProvider('form')) {
             $twig_form_templates = array('form_div_layout.html.twig');
             $twig_form_engine = new TwigRendererEngine($twig_form_templates);
             $twig_form_renderer = new TwigRenderer($twig_form_engine, $app->shared['csrf_provider']);
             $twig->addExtension(new FormExtension($twig_form_renderer));
             // add loader for Symfony built-in form templates
             $reflected = new \ReflectionClass('Symfony\\Bridge\\Twig\\Extension\\FormExtension');
             $path = dirname($reflected->getFileName()) . '/../Resources/views/Form';
             $loader->addLoader(new \Twig_Loader_Filesystem($path));
         }
         return $twig;
     }
     return false;
 }
Example #2
0
 public function testExists()
 {
     $loader1 = $this->getMock('Twig_Loader_Array', array('exists', 'getSource'), array(), '', false);
     $loader1->expects($this->once())->method('exists')->will($this->returnValue(false));
     $loader1->expects($this->never())->method('getSource');
     $loader2 = $this->getMock('Twig_LoaderInterface');
     $loader2->expects($this->once())->method('getSource')->will($this->returnValue('content'));
     $loader = new Twig_Loader_Chain();
     $loader->addLoader($loader1);
     $loader->addLoader($loader2);
     $this->assertTrue($loader->exists('foo'));
 }
Example #3
0
 public function testExists()
 {
     $loader1 = $this->getMockBuilder('Twig_Loader_Array')->setMethods(array('exists', 'getSource'))->disableOriginalConstructor()->getMock();
     $loader1->expects($this->once())->method('exists')->will($this->returnValue(false));
     $loader1->expects($this->never())->method('getSource');
     $loader2 = $this->getMockBuilder('Twig_LoaderInterface')->getMock();
     $loader2->expects($this->once())->method('getSource')->will($this->returnValue('content'));
     $loader = new Twig_Loader_Chain();
     $loader->addLoader($loader1);
     $loader->addLoader($loader2);
     $this->assertTrue($loader->exists('foo'));
 }
 /**
  * {@inheritdoc}
  */
 public function transform($text)
 {
     // Here we temporary changing twig environment loader to Chain loader with Twig_Loader_Array as first loader, which contains only one our template reference
     $oldLoader = $this->twig->getLoader();
     $hash = sha1($text);
     $chainLoader = new \Twig_Loader_Chain();
     $chainLoader->addLoader(new \Twig_Loader_Array(array($hash => $text)));
     $chainLoader->addLoader($oldLoader);
     $this->twig->setLoader($chainLoader);
     $result = $this->twig->render($hash);
     $this->twig->setLoader($oldLoader);
     return $result;
 }
Example #5
0
 /**
  * Create the default filesystem loader if enabled.
  *
  * @param ContaoTwigConfig $config The config to use.
  *
  * @return void
  */
 private function enableFilesystemLoader(ContaoTwigConfig $config)
 {
     if ($config->isEnableFilesystemLoader()) {
         $this->loaderFilesystem = new Twig_Loader_Filesystem($this->getTemplatePathes($config));
         $this->loader->addLoader($this->loaderFilesystem);
     }
 }
 /**
  * Create the new twig contao environment
  */
 protected function __construct()
 {
     $arrTemplatePaths = array();
     $blnDebug = $GLOBALS['TL_CONFIG']['debugMode'] || $GLOBALS['TL_CONFIG']['twigDebugMode'];
     // Make sure the cache directory exists
     if (version_compare(VERSION, '2', '<=') && !is_dir(TL_ROOT . '/system/cache')) {
         Files::getInstance()->mkdir('system/cache');
     }
     if (!is_dir(TL_ROOT . '/system/cache/twig')) {
         Files::getInstance()->mkdir('system/cache/twig');
     }
     // Add the layout templates directory
     if (TL_MODE == 'FE') {
         global $objPage;
         $strTemplateGroup = str_replace(array('../', 'templates/'), '', $objPage->templateGroup);
         if ($strTemplateGroup != '') {
             $arrTemplatePaths[] = TL_ROOT . '/templates/' . $strTemplateGroup;
         }
     }
     // Add the global templates directory
     $arrTemplatePaths[] = TL_ROOT . '/templates';
     // Add all modules templates directories
     foreach (Config::getInstance()->getActiveModules() as $strModule) {
         $strPath = TL_ROOT . '/system/modules/' . $strModule . '/templates';
         if (is_dir($strPath)) {
             $arrTemplatePaths[] = $strPath;
         }
     }
     // Create the default array loader
     $this->loaderArray = new Twig_Loader_Array(array());
     // Create the default filesystem loader
     $this->loaderFilesystem = new Twig_Loader_Filesystem($arrTemplatePaths);
     // Create the effective chain loader
     $this->loader = new Twig_Loader_Chain();
     // Register the default filesystem loaders
     $this->loader->addLoader($this->loaderArray);
     $this->loader->addLoader($this->loaderFilesystem);
     // Create the environment
     $this->environment = new Twig_Environment($this->loader, array('cache' => TL_ROOT . '/system/cache/twig', 'debug' => $blnDebug, 'autoescape' => false));
     // set default formats
     $this->environment->getExtension('core')->setNumberFormat(2, $GLOBALS['TL_LANG']['MSC']['decimalSeparator'], $GLOBALS['TL_LANG']['MSC']['thousandsSeparator']);
     // set default date format and timezone
     $this->environment->getExtension('core')->setDateFormat($GLOBALS['TL_CONFIG']['datimFormat']);
     $this->environment->getExtension('core')->setTimezone('Europe/Paris');
     // Add debug extension
     if ($blnDebug || $GLOBALS['TL_CONFIG']['twigDebugExtension']) {
         $this->environment->addExtension(new Twig_Extension_Debug());
     }
     $this->environment->addExtension(new ContaoTwigExtension());
     // HOOK: custom twig initialisation
     if (isset($GLOBALS['TL_HOOKS']['initializeTwig']) && is_array($GLOBALS['TL_HOOKS']['initializeTwig'])) {
         foreach ($GLOBALS['TL_HOOKS']['initializeTwig'] as $callback) {
             $this->import($callback[0]);
             $this->{$callback}[0]->{$callback}[1]($this);
         }
     }
 }
Example #7
0
 /**
  * Initialization for templates
  */
 private function _init()
 {
     $loaderChain = new Twig_Loader_Chain();
     $theme = $this->getTheme();
     // add the current theme as first to the loader chain
     // so Twig will look there first for overridden template files
     try {
         $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $theme));
     } catch (Twig_Error_Loader $e) {
         # @todo isInstalled() should catch this, inject Twig later
         die('The currently selected theme (' . $theme . ') does not seem to be properly installed (' . THEME . '/' . $theme . ' is missing)');
     }
     // add all required themes to the loader chain
     $themeInfo = $this->getThemeInfo($theme);
     if (isset($themeInfo['requirements']) && is_array($themeInfo['requirements'])) {
         foreach ($themeInfo['requirements'] as $requiredTheme) {
             try {
                 $loaderChain->addLoader(new Twig_Loader_Filesystem(THEME . '/' . $requiredTheme));
             } catch (Twig_Error_Loader $e) {
                 # @todo isInstalled() should catch this, inject Twig later
                 die('The required "' . $requiredTheme . '" theme is missing for the current theme (' . $theme . ')');
             }
         }
     }
     if (DEBUG_POCHE) {
         $twigParams = array();
     } else {
         $twigParams = array('cache' => CACHE);
     }
     parent::__construct($loaderChain, $twigParams);
     //$tpl = new Twig_Environment($loaderChain, $twigParams);
     $this->addExtension(new Twig_Extensions_Extension_I18n());
     # filter to display domain name of an url
     $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain');
     $this->addFilter($filter);
     # filter for reading time
     $filter = new Twig_SimpleFilter('getReadingTime', 'Tools::getReadingTime');
     $this->addFilter($filter);
 }
Example #8
0
 /**
  * Gets template names of templates that are present in the viewed profile.
  *
  * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
  *
  * @return array
  *
  * @throws \UnexpectedValueException
  */
 protected function getNames(Profile $profile)
 {
     $templates = [];
     foreach ($this->templates as $arguments) {
         if (NULL === $arguments) {
             continue;
         }
         list($name, $template) = $arguments;
         if (!$this->profiler->has($name) || !$profile->hasCollector($name)) {
             continue;
         }
         if ('.html.twig' === substr($template, -10)) {
             $template = substr($template, 0, -10);
         }
         if (!$this->twigLoader->exists($template . '.html.twig')) {
             throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
         }
         $templates[$name] = $template . '.html.twig';
     }
     return $templates;
 }
 /**
  * Gets the 'twig.loader' service.
  *
  * This service is shared.
  * This method always returns the same instance of the service.
  *
  * @return Twig_Loader_Chain A Twig_Loader_Chain instance.
  */
 protected function getTwig_LoaderService()
 {
     $a = new \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader($this->get('templating.locator'), $this->get('templating.name_parser'));
     $a->addPath('/var/www/default/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/views', 'Framework');
     $a->addPath('/var/www/default/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Resources/views', 'Security');
     $a->addPath('/var/www/default/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Resources/views', 'Twig');
     $a->addPath('/var/www/default/vendor/symfony/swiftmailer-bundle/Symfony/Bundle/SwiftmailerBundle/Resources/views', 'Swiftmailer');
     $a->addPath('/var/www/default/vendor/tedivm/stash-bundle/Tedivm/StashBundle/Resources/views', 'TedivmStash');
     $a->addPath('/var/www/default/vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishCoreBundle/Resources/views', 'EzPublishCore');
     $a->addPath('/var/www/default/vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishLegacyBundle/Resources/views', 'EzPublishLegacy');
     $a->addPath('/var/www/default/vendor/ezsystems/demobundle/EzSystems/DemoBundle/Resources/views', 'eZDemo');
     $a->addPath('/var/www/default/vendor/ezsystems/comments-bundle/EzSystems/CommentsBundle/Resources/views', 'EzSystemsComments');
     $a->addPath('/var/www/default/src/Bluetel/CoreBundle/Resources/views', 'BluetelCore');
     $a->addPath('/var/www/default/vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views', 'WebProfiler');
     $a->addPath('/var/www/default/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/views', 'SensioDistribution');
     $a->addPath('/var/www/default/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form');
     $this->services['twig.loader'] = $instance = new \Twig_Loader_Chain();
     $instance->addLoader($a);
     $instance->addLoader($this->get('twig.loader.string'));
     return $instance;
 }
Example #10
0
 public function testAddLoader()
 {
     $loader = new Twig_Loader_Chain();
     $loader->addLoader(new Twig_Loader_Array(array('foo' => 'bar')));
     $this->assertEquals('bar', $loader->getSource('foo'));
 }
 /**
  * Gets the 'twig' service.
  *
  * This service is shared.
  * This method always returns the same instance of the service.
  *
  * @return \Drupal\Core\Template\TwigEnvironment A Drupal\Core\Template\TwigEnvironment instance.
  */
 protected function getTwigService()
 {
     $a = new \Twig_Loader_Chain();
     $a->addLoader($this->get('twig.loader.filesystem'));
     $a->addLoader($this->get('twig.loader.theme_registry'));
     $a->addLoader($this->get('twig.loader.string'));
     $this->services['twig'] = $instance = new \Drupal\Core\Template\TwigEnvironment($this->get('app.root'), $this->get('cache.default'), $a, array('debug' => false, 'auto_reload' => NULL, 'cache' => true));
     $instance->addExtension($this->get('twig.extension'));
     $instance->addExtension($this->get('twig.extension.debug'));
     $instance->_serviceId = 'twig';
     return $instance;
 }
Example #12
0
 /**
  * Get template loader chain
  *
  * @return \Twig_LoaderInterface
  */
 protected function getLoader()
 {
     $config = $this->getConfig();
     $chain = new \Twig_Loader_Chain();
     // use template's own directory to search for templates
     $paths = array($config['phr_template_dir']);
     // inject common paths
     $projectPath = new ProjectPath($config['phr_template_dir']);
     if ($projectPath = $projectPath->get()) {
         $paths[] = $projectPath . DIRECTORY_SEPARATOR . 'layouts';
         $paths[] = $projectPath;
     }
     $chain->addLoader(new \Twig_Loader_Filesystem($paths));
     // add string template loader, which is responsible for loading templates
     // and removing front-matter
     $chain->addLoader(new \Phrozn\Twig\Loader\String());
     return $chain;
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function isFresh($name, $time)
 {
     return $this->chainLoader->isFresh($name, $time);
 }
Example #14
0
 /**
  * Adds a loader to Twig pointing to the location of the default templates for forms.
  *
  * @param \Twig_Loader_Chain $loader
  *
  * @return void
  */
 private function addFormTemplatesFolderToLoader(\Twig_Loader_Chain $loader)
 {
     $reflected = new \ReflectionClass('Symfony\\Bridge\\Twig\\Extension\\FormExtension');
     $path = dirname($reflected->getFileName()) . '/../Resources/views/Form';
     $loader->addLoader(new \Twig_Loader_Filesystem($path));
 }
Example #15
0
 /**
  * @return bool|Twig|Twig_Environment
  */
 public function getTwig()
 {
     if (!$this->twig) {
         try {
             require_once dirname(dirname(dirname(__FILE__))) . '/vendor/Twig/vendor/autoload.php';
             /** @var Twig_Loader_Chain $twigLoader */
             $twigLoader = new Twig_Loader_Chain(array());
             $loaders = $this->explodeAndClean($this->getOption('loaders', $this->config, '', true));
             if (!empty($loaders)) {
                 $pathLoaders = trim($this->getOption('path_loaders', $this->config, '', true));
                 foreach ($loaders as $loader) {
                     if ($loaderClass = $this->modx->loadClass('TwiggyLoader' . $loader, $pathLoaders, false, true)) {
                         $twigLoader->addLoader(new $loaderClass($this));
                     }
                 }
             }
             /** @var Twig_Environment twig */
             $this->twig = new Twig_Environment($twigLoader, $this->config);
             $this->debug = (bool) $this->getOption('debug', $this->config, false, true);
             if ($this->debug) {
                 $this->twig->addExtension(new Twig_Extension_Debug());
             }
             /** load sandbox */
             $sandbox = (bool) $this->getOption('sandbox', $this->config, false, true);
             if ($sandbox) {
                 /* array cache $options */
                 $options = array('cache_key' => 'config/twiggy_sandbox', 'cacheTime' => 0);
                 if (!($sandboxConfig = $this->getCache($options))) {
                     $sandboxTags = $this->explodeAndClean($this->getOption('sandbox_tags', $this->config, '', true));
                     $sandboxFilters = $this->explodeAndClean($this->getOption('sandbox_filters', $this->config, '', true));
                     $sandboxMethods = $this->modx->fromJSON($this->getOption('sandbox_methods', $this->config, "{}", true));
                     $sandboxProperties = $this->modx->fromJSON($this->getOption('sandbox_properties', $this->config, "{}", true));
                     $sandboxFunctions = $this->explodeAndClean($this->getOption('sandbox_functions', $this->config, '', true));
                     $sandboxConfig = array('tags' => $sandboxTags, 'filters' => $sandboxFilters, 'methods' => $sandboxMethods, 'properties' => $sandboxProperties, 'functions' => $sandboxFunctions);
                     $this->setCache($sandboxConfig, $options);
                 }
                 $sandboxPolicy = new Twig_Sandbox_SecurityPolicy($sandboxConfig['tags'], $sandboxConfig['filters'], $sandboxConfig['methods'], $sandboxConfig['properties'], $sandboxConfig['functions']);
                 $sandboxExtension = new Twig_Extension_Sandbox($sandboxPolicy);
                 $this->twig->addExtension($sandboxExtension);
             }
             /** load external $extensions */
             $extensions = $this->explodeAndClean($this->getOption('extensions', $this->config, '', true));
             if (!empty($extensions)) {
                 $pathExtensions = trim($this->getOption('path_extensions', $this->config, '', true));
                 foreach ($extensions as $extension) {
                     if ($extensionClass = $this->modx->loadClass('TwiggyExtension' . $extension, $pathExtensions, false, true)) {
                         $this->twig->addExtension(new $extensionClass($this));
                     }
                 }
             }
             /** set Globals */
             $this->setGlobals();
             $this->modx->invokeEvent('twiggyOnTwigInit', array('twiggy' => $this, 'config' => $this->config));
         } catch (Exception $e) {
             $this->modx->log(xPDO::LOG_LEVEL_ERROR, $e->getMessage());
             return false;
         }
     }
     return $this->twig;
 }