Exemplo n.º 1
0
 /**
  * Replace the {{theme_plus_cached_asset::*}} insert tag.
  *
  * @see \Contao\Controller::replaceInsertTags
  *
  * @param $tag
  *
  * @return bool|mixed|string
  * @SuppressWarnings(PHPMD.EvalExpression)
  */
 public function replaceCachedAssetInsertTag($tag)
 {
     if ('theme_plus_cached_asset::' === substr($tag, 0, 25)) {
         /** @var string $cacheKey The cache key. */
         $cacheKey = substr($tag, 25);
         /** @var string $assets */
         $assets = $this->cache->fetch($cacheKey);
         if ($assets) {
             $variables = $this->compiler->getVariables();
             extract($variables);
             return eval($assets);
         }
         return '';
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * Load all stylesheets from the cache.
  *
  * @param \LayoutModel $layout
  * @param array        $stylesheets The search and replace array.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.EvalExpression)
  */
 protected function loadStylesheets(\PageModel $page, \LayoutModel $layout, &$stylesheets)
 {
     $key = 'css:' . $page->id;
     $assets = $this->cache->fetch($key);
     if ($assets) {
         if ($page->cache) {
             $stylesheets .= sprintf('{{theme_plus_cached_asset::%s|uncached}}', $key);
         } else {
             $variables = $this->compiler->getVariables();
             extract($variables);
             $stylesheets .= eval($assets);
         }
     } else {
         $page->cache = false;
         $this->parseStylesheets(RenderMode::LIVE, $page, $layout, $stylesheets);
     }
 }
Exemplo n.º 3
0
 /**
  * Load all javascripts from the cache.
  *
  * @param \LayoutModel $layout
  * @param array        $scripts The search and replace array.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.EvalExpression)
  */
 protected function loadJavaScripts(\PageModel $page, \LayoutModel $layout, &$headScripts, &$bodyScripts)
 {
     $headKey = 'js:head:' . $page->id;
     $headAssets = $this->cache->fetch($headKey);
     $bodyKey = 'js:body:' . $page->id;
     $bodyAssets = $this->cache->fetch($bodyKey);
     if ($headAssets && $bodyAssets) {
         if ($page->cache) {
             $headScripts .= sprintf('{{theme_plus_cached_asset::%s|uncached}}', $headKey);
             $bodyScripts .= sprintf('{{theme_plus_cached_asset::%s|uncached}}', $bodyKey);
         } else {
             $variables = $this->compiler->getVariables();
             extract($variables);
             $headScripts .= eval($headAssets);
             $bodyScripts .= eval($bodyAssets);
         }
     } else {
         $page->cache = false;
         $this->parseJavaScripts(RenderMode::LIVE, $page, $layout, $headScripts, $bodyScripts);
     }
 }
 /**
  * Evaluate filter rules.
  *
  * @param AssetInterface $asset The asset.
  *
  * @return bool
  */
 private function evaluateRules(FilterRules $filterRules)
 {
     return !$this->filterRulesCompiler->evaluate($filterRules);
 }
    private function generateCache(GeneratePreCompiledAssetsCacheEvent $event, EventDispatcherInterface $eventDispatcher, $organizeEventName, $renderEventName)
    {
        if ($event->getCacheCode()) {
            return;
        }
        $date = date('r');
        $page = $event->getPage();
        $layout = $event->getLayout();
        $defaultFilters = $event->getDefaultFilters();
        $collections = $event->getCollections();
        $result = <<<PHP

/**
 * Assets cache
 *
 * created at: {$date}
 * page id: {$page->id}
 * layout id: {$layout->id}
 */

PHP;
        // store all collections
        foreach ($collections as $collection) {
            $organizeEvent = new OrganizeAssetsEvent(RenderMode::LIVE, $page, $layout, $collection, $defaultFilters);
            $eventDispatcher->dispatch($organizeEventName, $organizeEvent);
            $assets = $organizeEvent->getOrganizedAssets()->all();
            $debug = '';
            $html = '';
            foreach ($assets as $asset) {
                $renderEvent = new RenderAssetHtmlEvent(RenderMode::LIVE, $page, $layout, $asset, $defaultFilters);
                $eventDispatcher->dispatch($renderEventName, $renderEvent);
                $debug .= $this->developerTool->getAssetDebugString($asset) . PHP_EOL . PHP_EOL;
                $html .= $renderEvent->getHtml();
            }
            if ($html) {
                $expression = $this->compiler->compile($collection->getFilterRules());
                $debug = rtrim($debug);
                $debug = explode(PHP_EOL, $debug);
                $debug = implode(PHP_EOL . '     * ', $debug);
                $debug = <<<PHP
    /*
     * Debug information:
     *
     * {$debug}
     */
PHP;
                $html = var_export($html, true);
                $result .= <<<PHP

if ({$expression}) {
{$debug}
    return {$html};
}

PHP;
            }
        }
        $result .= <<<PHP

return null;

PHP;
        $event->setCacheCode($result);
    }