setCache() public method

public setCache ( $cache )
Example #1
0
 function __construct()
 {
     parent::__construct('spdx2', AGENT_VERSION, AGENT_REV);
     $this->uploadDao = $this->container->get('dao.upload');
     $this->clearingDao = $this->container->get('dao.clearing');
     $this->dbManager = $this->container->get('db.manager');
     $this->renderer = $this->container->get('twig.environment');
     $this->renderer->setCache(false);
     $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS . ':';
     $this->agentSpecifLongOptions[] = self::OUTPUT_FORMAT_KEY . ':';
 }
Example #2
0
 /**
  * Loads the Twig instance and registers the autoloader.
  *
  * @return void
  */
 public function configure()
 {
     parent::configure();
     $this->configuration = $this->context->getConfiguration();
     //Empty array becuase it changes based on the rendering context
     $this->loader = new Twig_Loader_Filesystem(array());
     $this->twig = new Twig_Environment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false)));
     if ($this->twig->isDebug()) {
         $this->twig->setCache(null);
         $this->twig->setAutoReload(true);
     }
     $this->loadExtensions();
 }
 /**
  * Loads the Twig instance and registers the autoloader.
  */
 public function configure()
 {
     parent::configure();
     $this->configuration = $this->context->getConfiguration();
     require_once sfConfig::get('sf_twig_lib_dir', dirname(__FILE__) . '/../lib/vendor/Twig/lib') . '/Twig/Autoloader.php';
     Twig_Autoloader::register();
     // empty array becuase it changes based on the rendering context
     $this->loader = new Twig_Loader_Filesystem(array());
     $this->twig = new sfTwigEnvironment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false), 'sf_context' => $this->context));
     if ($this->twig->isDebug()) {
         $this->twig->enableAutoReload();
         $this->twig->setCache(null);
     }
     $this->loadExtensions();
 }
Example #4
0
File: Twig.php Project: re-pe/grav
 /**
  * Twig process that renders the site layout. This is the main twig process that renders the overall
  * page and handles all the layout for the site display.
  *
  * @param string $format Output format (defaults to HTML).
  * @return string the rendered output
  * @throws \RuntimeException
  */
 public function processSite($format = null)
 {
     // set the page now its been processed
     $this->grav->fireEvent('onTwigSiteVariables');
     $pages = $this->grav['pages'];
     $page = $this->grav['page'];
     $twig_vars = $this->twig_vars;
     $twig_vars['pages'] = $pages->root();
     $twig_vars['page'] = $page;
     $twig_vars['header'] = $page->header();
     $twig_vars['content'] = $page->content();
     $ext = '.' . ($format ? $format : 'html') . TWIG_EXT;
     // determine if params are set, if so disable twig cache
     $params = $this->grav['uri']->params(null, true);
     if (!empty($params)) {
         $this->twig->setCache(false);
     }
     // Get Twig template layout
     $template = $this->template($page->template() . $ext);
     try {
         $output = $this->twig->render($template, $twig_vars);
     } catch (\Twig_Error_Loader $e) {
         // If loader error, and not .html.twig, try it as fallback
         if ($ext != '.html' . TWIG_EXT) {
             try {
                 $output = $this->twig->render($page->template() . '.html' . TWIG_EXT, $twig_vars);
             } catch (\Twig_Error_Loader $e) {
                 throw new \RuntimeException($e->getRawMessage(), 404, $e);
             }
         } else {
             throw new \RuntimeException($e->getRawMessage(), 404, $e);
         }
     }
     return $output;
 }
Example #5
0
 /**
  * Set Twig cache directory.
  *
  * @param string $cacheDir
  */
 public function setTwigCache($cacheDir)
 {
     if (!is_dir($cacheDir) && (!is_writable(dirname($cacheDir)) || false === @mkdir($cacheDir, 0755))) {
         throw new RendererException(sprintf('Unable to create twig cache "%s"', $cacheDir), RendererException::RENDERING_ERROR);
     }
     if (!is_writable($cacheDir)) {
         throw new RendererException(sprintf('Twig cache "%s" is not writable', $cacheDir), RendererException::RENDERING_ERROR);
     }
     $this->twig->setCache($cacheDir);
 }
Example #6
0
 /**
  * {@inheritdoc}
  *
  * @return $this
  */
 public function setEnvironment(EnvironmentInterface $environment)
 {
     $this->environment = $environment;
     if (!$environment->cachable()) {
         $this->twig->setCache(false);
         return $this;
     }
     $this->twig->setCache(new TwigCache($this->files, $environment));
     return $this;
 }
Example #7
0
function dwInitTwigEnvironment(Twig_Environment $twig)
{
    $twig->setCache(ROOT_PATH . '/tmp/twig');
    $twig->enableAutoReload();
    $twig->addExtension(new Twig_I18n_Extension());
    $twig->addFilter(new Twig_SimpleFilter('purify', function ($dirty) {
        return dwGetHTMLPurifier()->purify($dirty);
    }));
    $twig->addFilter(new Twig_SimpleFilter('json', function ($arr) {
        $mask = 0;
        if (!empty($opts)) {
            if (!empty($opts['pretty'])) {
                $mask = $mask | JSON_PRETTY_PRINT;
            }
        }
        return json_encode($arr, $mask);
    }));
    $twig->addFilter(new Twig_SimpleFilter('css', function ($arr) {
        $css = '';
        foreach ($arr as $prop => $val) {
            $css .= $prop . ':' . $val . ';';
        }
        return $css;
    }));
    $twig->addFunction(new Twig_SimpleFunction('hook', function () {
        call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args());
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_hook', function ($hook) {
        return DatawrapperHooks::getInstance()->hookRegistered($hook);
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_plugin', function ($plugin) {
        return DatawrapperPluginManager::loaded($plugin);
    }));
    $twig->addFilter(new Twig_SimpleFilter('lettering', function ($text) {
        $out = '';
        foreach (str_split($text) as $i => $char) {
            $out .= '<span class="char' . $i . '">' . $char . '</span>';
        }
        return $out;
    }, array('is_safe' => array('html'))));
    $loc = DatawrapperSession::getLanguage();
    if ($loc == 'en') {
        $loc = 'en-US';
    }
    \Moment\Moment::setLocale(str_replace('-', '_', $loc));
    $twig->addFilter(new Twig_SimpleFilter('reltime', function ($time) {
        // return $time;
        return (new \Moment\Moment($time))->fromNow()->getRelative();
    }));
    if (!empty($GLOBALS['dw_config']['debug'])) {
        $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
    }
    return $twig;
}
Example #8
0
 /**
  * Creates a Twig rendering engine
  * @param type $viewRootFolder
  * @param type $cacheFolder
  * @param type $debug
  * @return \Twig_Environment
  */
 protected function createTwig($viewRootFolder, $cacheFolder, $debug)
 {
     $this->loader = new \Twig_Loader_Filesystem($viewRootFolder);
     $twig = new \Twig_Environment($this->loader);
     $twig->enableStrictVariables();
     if ($debug === true) {
         $twig->enableDebug();
     } else {
         $twig->disableDebug();
         $twig->setCache($cacheFolder);
     }
     return $twig;
 }
Example #9
0
 /**
  * Twig process that renders the site layout. This is the main twig process that renders the overall
  * page and handles all the layout for the site display.
  *
  * @param string $format Output format (defaults to HTML).
  * @return string the rendered output
  * @throws \RuntimeException
  */
 public function processSite($format = null)
 {
     // set the page now its been processed
     $this->grav->fireEvent('onTwigSiteVariables');
     $pages = $this->grav['pages'];
     $page = $this->grav['page'];
     $content = $page->content();
     $config = $this->grav['config'];
     $twig_vars = $this->twig_vars;
     $twig_vars['pages'] = $pages->root();
     $twig_vars['page'] = $page;
     $twig_vars['header'] = $page->header();
     $twig_vars['content'] = $content;
     $ext = '.' . ($format ? $format : 'html') . TWIG_EXT;
     // determine if params are set, if so disable twig cache
     $params = $this->grav['uri']->params(null, true);
     if (!empty($params)) {
         $this->twig->setCache(false);
     }
     // Get Twig template layout
     $template = $this->template($page->template() . $ext);
     try {
         $output = $this->twig->render($template, $twig_vars);
     } catch (\Twig_Error_Loader $e) {
         // If loader error, and not .html.twig, try it as fallback
         if (Utils::contains($e->getMessage(), $template)) {
             $inflector = new Inflector();
             $error_msg = 'The template file for this page: "' . $template . '" is not provided by the theme: "' . $inflector->titleize($config->get('system.pages.theme')) . '"';
         } else {
             $error_msg = $e->getMessage();
         }
         // Try html version of this template if initial template was NOT html
         if ($ext != '.html' . TWIG_EXT) {
             try {
                 $output = $this->twig->render($page->template() . '.html' . TWIG_EXT, $twig_vars);
             } catch (\Twig_Error_Loader $e) {
                 throw new \RuntimeException($error_msg, 400, $e);
             }
         } else {
             throw new \RuntimeException($error_msg, 400, $e);
         }
     }
     return $output;
 }
Example #10
0
function dwInitTwigEnvironment(Twig_Environment $twig)
{
    $twig->setCache(ROOT_PATH . '/tmp/twig');
    $twig->enableAutoReload();
    $twig->addExtension(new Twig_I18n_Extension());
    $twig->addFilter(new Twig_SimpleFilter('purify', function ($dirty) {
        return dwGetHTMLPurifier()->purify($dirty);
    }));
    $twig->addFilter(new Twig_SimpleFilter('json', function ($arr) {
        $mask = 0;
        if (!empty($opts)) {
            if (!empty($opts['pretty'])) {
                $mask = $mask | JSON_PRETTY_PRINT;
            }
        }
        return json_encode($arr, $mask);
    }));
    $twig->addFilter(new Twig_SimpleFilter('css', function ($arr) {
        $css = '';
        foreach ($arr as $prop => $val) {
            $css .= $prop . ':' . $val . ';';
        }
        return $css;
    }));
    $twig->addFunction(new Twig_SimpleFunction('hook', function () {
        call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args());
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_hook', function ($hook) {
        return DatawrapperHooks::getInstance()->hookRegistered($hook);
    }));
    $twig->addFunction(new Twig_SimpleFunction('has_plugin', function ($plugin) {
        return DatawrapperPluginManager::loaded($plugin);
    }));
    $twig->addFilter(new Twig_SimpleFilter('lettering', function ($text) {
        $out = '';
        foreach (str_split($text) as $i => $char) {
            $out .= '<span class="char' . $i . '">' . $char . '</span>';
        }
        return $out;
    }, array('is_safe' => array('html'))));
    return $twig;
}
Example #11
0
 /**
  * Twig process that renders the site layout. This is the main twig process that renders the overall
  * page and handles all the layout for the site display.
  *
  * @param string $format Output format (defaults to HTML).
  * @return string the rendered output
  * @throws \RuntimeException
  */
 public function processSite($format = null)
 {
     // set the page now its been processed
     $this->grav->fireEvent('onTwigSiteVariables');
     $pages = $this->grav['pages'];
     $page = $this->grav['page'];
     $content = $page->content();
     $config = $this->grav['config'];
     $twig_vars = $this->twig_vars;
     $twig_vars['pages'] = $pages->root();
     $twig_vars['page'] = $page;
     $twig_vars['header'] = $page->header();
     $twig_vars['media'] = $page->media();
     $twig_vars['content'] = $content;
     $ext = '.' . ($format ? $format : 'html') . TWIG_EXT;
     // determine if params are set, if so disable twig cache
     $params = $this->grav['uri']->params(null, true);
     if (!empty($params)) {
         $this->twig->setCache(false);
     }
     // Get Twig template layout
     $template = $this->template($page->template() . $ext);
     try {
         $output = $this->twig->render($template, $twig_vars);
     } catch (\Twig_Error_Loader $e) {
         $error_msg = $e->getMessage();
         // Try html version of this template if initial template was NOT html
         if ($ext != '.html' . TWIG_EXT) {
             try {
                 $output = $this->twig->render($page->template() . '.html' . TWIG_EXT, $twig_vars);
             } catch (\Twig_Error_Loader $e) {
                 throw new \RuntimeException($error_msg, 400, $e);
             }
         } else {
             throw new \RuntimeException($error_msg, 400, $e);
         }
     }
     return $output;
 }
Example #12
0
<?php

require_once 'includes/Twig/Autoloader.php';
require_once "config.php";
//Register autoloader
Twig_Autoloader::register();
//Loader for template files
$loader = new Twig_Loader_Filesystem('templates');
//Twig instance
$twig = new Twig_Environment($loader, array('cache' => 'cache'));
//Load template file
function fixObject(&$object)
{
    if (!is_object($object) && gettype($object) == 'object') {
        return $object = unserialize(serialize($object));
    }
    return $object;
}
$twig->setCache(false);
$template = $twig->loadTemplate('home.html');
echo $template->render(array('title' => 'Home1'));
?>
    
Example #13
0
<?php

$loader = new Twig_Loader_Filesystem(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
$twig = new Twig_Environment($loader, ['debug' => true]);
if (getenv('TEMPLATE_CACHE_ENABLED') && getenv('TEMPLATE_CACHE_ENABLED') === 'true') {
    $twig->setCache(__DIR__ . DIRECTORY_SEPARATOR . 'cache');
}
if (getenv('GOOGLE_ANALYTICS_ID')) {
    $twig->addGlobal('google_analytics_id', getenv('GOOGLE_ANALYTICS_ID'));
}
$twig->addExtension(new Twig_Extension_Debug());
Example #14
0
 /**
  * @param ContainerInterface $container
  */
 public function setContainer(ContainerInterface $container)
 {
     $this->container = $container;
     $this->twig->getLoader()->setContainer($container);
     $this->twig->setCache($container->getParameter('directories.cache') . DIRECTORY_SEPARATOR . 'twig');
 }
Example #15
0
 /**
  * @return \Twig_Environment
  */
 private function getTwig()
 {
     $twig = new \Twig_Environment();
     $twig->setCache(false);
     return $twig;
 }
Example #16
0
 if (is_dir(const_path . 'pages/' . config_pages)) {
     $loader->addPath(const_path . 'pages/' . config_pages);
 }
 if (dirname($request['page']) != '.') {
     $loader->addPath(const_path . 'pages/' . config_pages . '/' . dirname($request['page']));
 }
 // add dir if is not directly chosen
 if (config_driver == 'smarthome.py' and config_pages != 'smarthome' and is_dir(const_path . "pages/smarthome")) {
     $loader->addPath(const_path . 'pages/smarthome');
 }
 $loader->addPath(const_path . 'pages/base');
 $loader->addPath(const_path . 'widgets');
 // init environment
 $twig = new Twig_Environment($loader);
 if (config_cache) {
     $twig->setCache(dirname(__FILE__) . '/temp');
 }
 foreach ($request as $key => $val) {
     if ($key == "page") {
         $val = basename(str_replace('.', '_', $val));
     }
     $twig->addGlobal($key, $val);
 }
 $twig->addGlobal('pagepath', dirname($request['page']));
 if (config_design == 'ice') {
     $twig->addGlobal('icon1', 'icons/bl/');
     $twig->addGlobal('icon0', 'icons/sw/');
 } elseif (config_design == 'greenhornet') {
     $twig->addGlobal('icon1', 'icons/gn/');
     $twig->addGlobal('icon0', 'icons/ws/');
 } else {
Example #17
0
 /**
  * Twig instance factory
  *
  * @param \Twig_LoaderInterface $loader
  * @return \Twig_Environment
  */
 protected function getTwig(\Twig_LoaderInterface $loader)
 {
     $twig = new \Twig_Environment($loader);
     $twig->setCache(false);
     return $twig;
 }