getCacheFilename() public method

public getCacheFilename ( $name )
 public function addTemplate($path)
 {
     $this->environment->loadTemplate($path);
     $this->templates[] = $this->environment->getCacheFilename($path);
 }
Esempio n. 2
0
 public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
 {
     $options = array('cache' => sys_get_temp_dir() . '/twig', 'auto_reload' => false, 'debug' => false);
     // force compilation
     $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
     $cache = $twig->getCacheFilename('index');
     if (!is_dir(dirname($cache))) {
         mkdir(dirname($cache), 0777, true);
     }
     file_put_contents($cache, $twig->compileSource('{{ foo }}', 'index'));
     // check that extensions won't be initialized when rendering a template that is already in the cache
     $twig = $this->getMockBuilder('Twig_Environment')->setConstructorArgs(array($loader, $options))->setMethods(array('initExtensions'))->getMock();
     $twig->expects($this->never())->method('initExtensions');
     // render template
     $output = $twig->render('index', array('foo' => 'bar'));
     $this->assertEquals('bar', $output);
     unlink($cache);
 }
Esempio n. 3
0
 /**
  * Get the path to the compiled version of a view.
  *
  * @param  string  $path
  * @return string
  */
 public function getCompiledPath($path)
 {
     return $this->twig->getCacheFilename($path);
 }
Esempio n. 4
0
 public static function _handleEvent($event)
 {
     $eventStart = microtime(true);
     Kwf_Cache_Simple::resetZendCache();
     //reset to re-fetch namespace
     if (substr($event->filename, -4) == '.css' || substr($event->filename, -3) == '.js' || substr($event->filename, -9) == '.printcss' || substr($event->filename, -5) == '.scss') {
         echo "asset modified\n";
         if ($event instanceof Event\Modify) {
             //there is no cache for individual files, scss cache is handled in Kwf_Assets_Dependency_File_Scss using mtime
             $assetsType = substr($event->filename, strrpos($event->filename, '.') + 1);
             if ($assetsType == 'scss') {
                 $assetsType = 'css';
             }
             self::_clearAssetsAll($assetsType);
             if ($assetsType == 'js') {
                 self::_clearAssetsAll('defer.js');
             }
         } else {
             if ($event instanceof Event\Create || $event instanceof Event\Delete || $event instanceof Event\Move) {
                 self::_clearAssetsDependencies();
                 $assetsType = substr($event->filename, strrpos($event->filename, '.') + 1);
                 if ($assetsType == 'scss') {
                     $assetsType = 'css';
                 }
                 self::_clearAssetsAll($assetsType);
                 if ($assetsType == 'js') {
                     self::_clearAssetsAll('defer.js');
                 }
             }
         }
     } else {
         if (self::_endsWith($event->filename, '/dependencies.ini')) {
             if ($event instanceof Event\Modify) {
                 self::_clearAssetsDependencies();
                 self::_clearAssetsAll();
             }
         } else {
             if (preg_match('#/config([^/]*)?\\.ini$#', $event->filename)) {
                 //config.ini, configPoi.ini, config.local.ini (in any directory)
                 if ($event instanceof Event\Modify) {
                     $section = call_user_func(array(Kwf_Setup::$configClass, 'getDefaultConfigSection'));
                     $cacheId = 'config_' . str_replace('-', '_', $section);
                     Kwf_Config_Cache::getInstance()->remove($cacheId);
                     echo "removed config cache\n";
                     $apcCacheId = $cacheId . getcwd();
                     $cacheIds = array($apcCacheId, $apcCacheId . 'mtime');
                     $simpleCacheStaticPrefixes = array('config-', 'configAr-');
                     if (Kwf_Util_Apc::isAvailable()) {
                         self::_clearApcCache(array('cacheIds' => $cacheIds, 'clearCacheSimpleStatic' => $simpleCacheStaticPrefixes));
                         echo "cleared apc config cache\n";
                     } else {
                         foreach ($simpleCacheStaticPrefixes as $i) {
                             Kwf_Cache_SimpleStatic::clear($i);
                         }
                     }
                     $cmd = Kwf_Config::getValue('server.phpCli') . " bootstrap.php clear-cache --type=setup";
                     exec($cmd, $out, $ret);
                     echo "cleared setup.php cache";
                     if ($ret) {
                         echo " [FAILED]";
                     }
                     echo "\n";
                 }
             } else {
                 if (preg_match('#(Acl|MenuConfig)\\.php$#', $event->filename)) {
                     if ($event instanceof Event\Modify) {
                         Kwf_Acl::clearCache();
                         echo "cleared acl cache...\n";
                     }
                 } else {
                     if (self::_endsWith($event->filename, '.twig')) {
                         $loader = new Twig_Loader_Filesystem('.');
                         $twig = new Twig_Environment($loader, array('cache' => 'cache/twig'));
                         $cacheFile = $event->filename;
                         $cacheFile = substr($cacheFile, strlen(getcwd()) + 1);
                         $cacheFile = $twig->getCacheFilename($cacheFile);
                         if (file_exists($cacheFile)) {
                             unlink($cacheFile);
                             echo "cleared twig cache file '{$cacheFile}' for template '{$event->filename}'\n";
                         }
                     }
                 }
             }
         }
     }
     if (self::_startsWith($event->filename, getcwd() . '/components') || self::_startsWith($event->filename, getcwd() . '/theme') || self::_startsWith($event->filename, KWF_PATH . '/Kwc') || defined('VKWF_PATH') && self::_startsWith($event->filename, VKWF_PATH . '/Vkwc')) {
         $cls = null;
         foreach (explode(PATH_SEPARATOR, get_include_path()) as $ip) {
             if (substr($ip, 0, 1) == '.') {
                 $ip = getcwd() . substr($ip, 1);
             }
             if (substr($ip, 0, 1) != '/') {
                 $ip = getcwd() . '/' . $ip;
             }
             if (self::_startsWith($event->filename, $ip)) {
                 $cls = str_replace('/', '_', substr($event->filename, strlen($ip) + 1, -4));
             }
         }
         if (!$cls) {
             echo "unknown component class?!\n";
             return;
         }
         if (!self::_endsWith($event->filename, '/Component.php')) {
             //other component file
             $cls = substr($cls, 0, strrpos($cls, '_')) . '_Component';
         }
         echo "component {$cls}\n";
         if (!@class_exists($cls)) {
             echo "parse error: {$cls}\n";
             return;
         }
         $matchingClasses = array();
         try {
             foreach (Kwc_Abstract::getComponentClasses() as $c) {
                 if (is_instance_of($c, $cls)) {
                     $matchingClasses[] = $c;
                 }
             }
         } catch (Exception $e) {
         }
         if (self::_endsWith($event->filename, '/Admin.php') || self::_endsWith($event->filename, '/Master.tpl') || self::_endsWith($event->filename, '/Component.tpl') || self::_endsWith($event->filename, '/Partial.tpl') || self::_endsWith($event->filename, '/Master.twig') || self::_endsWith($event->filename, '/Component.twig') || self::_endsWith($event->filename, '/Partial.twig') || self::_endsWith($event->filename, '/Controller.php') || self::_endsWith($event->filename, '/FrontendForm.php') || self::_endsWith($event->filename, '/Form.php') || self::_endsWith($event->filename, '/Component.css') || self::_endsWith($event->filename, '/Component.scss') || self::_endsWith($event->filename, '/Component.printcss')) {
             if ($event instanceof Event\Create || $event instanceof Event\Delete) {
                 echo "recalculate 'componentFiles' setting because comopnent file got removed/added...\n";
                 self::_clearComponentSettingsCache($matchingClasses, 'componentFiles');
             }
         }
         if (self::_endsWith($event->filename, '/Component.php') || self::_endsWith($event->filename, '/FrontendForm.php')) {
             if ($event instanceof Event\Modify) {
                 self::_clearComponentSettingsCache($matchingClasses);
                 //view cache can depend on settings
                 $s = new Kwf_Model_Select();
                 $s->whereEquals('component_class', $matchingClasses);
                 self::_deleteViewCache($s);
             }
         } else {
             if (self::_endsWith($event->filename, '/Component.css') || self::_endsWith($event->filename, '/Component.scss') || self::_endsWith($event->filename, '/Component.printcss')) {
                 //MODIFY already handled above (assets)
                 //CREATE/DELETE also handled above
             } else {
                 if (self::_endsWith($event->filename, '/Master.tpl') || self::_endsWith($event->filename, '/Master.twig')) {
                     if ($event instanceof Event\Modify) {
                         $s = new Kwf_Model_Select();
                         //all component_classes
                         $s->whereEquals('type', 'master');
                         self::_deleteViewCache($s);
                     }
                 } else {
                     if (self::_endsWith($event->filename, '/Component.tpl') || self::_endsWith($event->filename, '/Component.twig')) {
                         if ($event instanceof Event\Modify) {
                             $s = new Kwf_Model_Select();
                             $s->whereEquals('component_class', $matchingClasses);
                             $s->whereEquals('type', 'component');
                             $s->whereEquals('renderer', 'component');
                             self::_deleteViewCache($s);
                         }
                     } else {
                         if (self::_endsWith($event->filename, '/Partial.tpl') || self::_endsWith($event->filename, '/Partial.twig')) {
                             if ($event instanceof Event\Modify) {
                                 $s = new Kwf_Model_Select();
                                 $s->whereEquals('component_class', $matchingClasses);
                                 $s->whereEquals('type', 'partial');
                                 self::_deleteViewCache($s);
                             }
                         } else {
                             if (self::_endsWith($event->filename, '/Mail.html.tpl') || self::_endsWith($event->filename, '/Mail.html.twig')) {
                                 if ($event instanceof Event\Modify) {
                                     $s = new Kwf_Model_Select();
                                     $s->whereEquals('component_class', $matchingClasses);
                                     $s->whereEquals('type', 'component');
                                     $s->whereEquals('renderer', 'mail_html');
                                     self::_deleteViewCache($s);
                                 }
                             } else {
                                 if (self::_endsWith($event->filename, '/Mail.txt.tpl') || self::_endsWith($event->filename, '/Mail.txt.twig')) {
                                     if ($event instanceof Event\Modify) {
                                         $s = new Kwf_Model_Select();
                                         $s->whereEquals('component_class', $matchingClasses);
                                         $s->whereEquals('type', 'component');
                                         $s->whereEquals('renderer', 'mail_txt');
                                         self::_deleteViewCache($s);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     echo "finished in " . round((microtime(true) - $eventStart) * 1000, 2) . "ms\n\n";
 }
$config->set('HTML.Allowed', 'a[href],p,b,strong,u,i,em,q,blockquote,*[style]');
$_HTMLPurifier = new HTMLPurifier($config);
$twig->addFilter('purify', new Twig_Filter_Function('str_purify'));
function str_purify($dirty_html)
{
    global $_HTMLPurifier;
    return $_HTMLPurifier->purify($dirty_html);
}
function call_hook()
{
    call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args());
}
$twig->addFunction('hook', new Twig_Function_Function('call_hook'));
// loae I18n extension for Twig
$twig->addExtension(new Twig_Extension_I18n());
// iterate over all your templates
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
    if (substr($file, -5) == ".twig") {
        // force compilation
        $tmplPath = str_replace($tplDir . '/', '', $file);
        $twig->loadTemplate($tmplPath);
        $cacheFile = $twig->getCacheFilename($tmplPath);
        $compiled = file_get_contents($cacheFile);
        $outPath = $tmpDir . str_replace("/", "__", $tmplPath) . ".php";
        if (substr($tmplPath, 0, 8) == 'plugins/') {
            $outPath = $tmpDirPlugins . str_replace("/", "__", substr($tmplPath, 8)) . ".php";
        }
        file_put_contents($outPath, $compiled);
        unlink($cacheFile);
    }
}