Exemple #1
0
 /**
  * Inject the developer tools in designer mode.
  *
  * @see \Contao\Controller::replaceDynamicScriptTags
  *
  * @param string $buffer
  *
  * @return string
  */
 public function injectDeveloperTools($buffer)
 {
     $page = $this->pageProvider->getPage();
     if ($page) {
         $renderMode = $this->renderModeDeterminer->determineMode();
         if (RenderMode::DESIGN == $renderMode) {
             $buffer = $this->developerTool->inject($buffer);
         }
     }
     return $buffer;
 }
 public function renderDesignerModeInlineHtml(RenderAssetHtmlEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     if (!$event->getHtml() && $event->isDesignerMode()) {
         $asset = $event->getAsset();
         if ($asset instanceof ExtendedAssetInterface && $asset->isInline()) {
             $page = $this->pageProvider->getPage();
             $html = '';
             // html mode
             $xhtml = $page->outputFormat == 'xhtml';
             // retrieve page path
             $targetPath = \Environment::get('requestUri');
             // remove query string
             $targetPath = preg_replace('~\\?\\.*~', '', $targetPath);
             // remove leading /
             $targetPath = ltrim($targetPath, '/');
             // overwrite the target path
             $asset->setTargetPath($targetPath);
             // load and dump the collection
             $asset->load($event->getDefaultFilters());
             $js = $asset->dump($event->getDefaultFilters());
             // generate html
             $scriptHtml = '<script';
             if ($xhtml) {
                 $scriptHtml .= ' type="text/javascript"';
             }
             $scriptHtml .= '>';
             $scriptHtml .= $js;
             $scriptHtml .= '</script>';
             // wrap cc around
             if ($asset instanceof ExtendedAssetInterface && $asset->getConditionalComment()) {
                 $scriptHtml = ThemePlusUtils::wrapCc($scriptHtml, $asset->getConditionalComment());
             }
             // add debug information
             $html .= $this->developerTool->getDebugComment($asset);
             $html .= $scriptHtml . PHP_EOL;
             $event->setHtml($html);
         }
     }
 }
 public function renderDesignerModeHtml(RenderAssetHtmlEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
 {
     if (!$event->getHtml() && $event->isDesignerMode()) {
         $asset = $event->getAsset();
         if (!$asset instanceof ExtendedAssetInterface || !$asset->isInline()) {
             $page = $this->pageProvider->getPage();
             $html = '';
             // html mode
             $xhtml = $page->outputFormat == 'xhtml';
             $tagEnding = $xhtml ? ' />' : '>';
             // session id
             $id = substr(md5($asset->getSourceRoot() . '/' . $asset->getSourcePath()), 0, 8);
             // get the session object
             $session = unserialize($_SESSION['THEME_PLUS_ASSETS'][$id]);
             if (!$session || $asset->getLastModified() > $session->asset->getLastModified()) {
                 $session = new \stdClass();
                 $session->page = $page->id;
                 $session->asset = $asset;
                 $_SESSION['THEME_PLUS_ASSETS'][$id] = serialize($session);
             }
             $realAssets = $asset;
             while ($realAssets instanceof DelegatorAssetInterface) {
                 $realAssets = $realAssets->getAsset();
             }
             if ($realAssets instanceof FileAsset) {
                 $name = basename($realAssets->getSourcePath());
             } else {
                 if ($realAssets instanceof HttpAsset) {
                     $class = new \ReflectionClass($realAssets);
                     $property = $class->getProperty('sourceUrl');
                     $property->setAccessible(true);
                     $url = $property->getValue($realAssets);
                     $name = 'url_' . basename(parse_url($url, PHP_URL_PATH));
                 } else {
                     $name = 'asset_' . $id;
                 }
             }
             // generate the proxy url
             $url = sprintf('assets/theme-plus/proxy.php/css/%s/%s', $id, $name);
             // overwrite the target path
             $asset->setTargetPath($url);
             // remember asset for debug tool
             $this->developerTool->registerFile($id, (object) ['asset' => $realAssets, 'type' => 'css', 'url' => $url]);
             // generate html
             $linkHtml = '<link';
             $linkHtml .= sprintf(' id="%s"', $id);
             $linkHtml .= sprintf(' href="%s"', $url);
             if ($xhtml) {
                 $linkHtml .= ' type="text/css"';
             }
             $linkHtml .= ' rel="stylesheet"';
             if ($asset instanceof ExtendedAssetInterface && $asset->getMediaQuery()) {
                 $linkHtml .= sprintf(' media="%s"', $asset->getMediaQuery());
             }
             $linkHtml .= $tagEnding;
             // wrap cc around
             if ($asset instanceof ExtendedAssetInterface && $asset->getConditionalComment()) {
                 $linkHtml = ThemePlusUtils::wrapCc($linkHtml, $asset->getConditionalComment());
             }
             // add debug information
             $html .= $this->developerTool->getDebugComment($asset);
             $html .= $linkHtml . PHP_EOL;
             $event->setHtml($html);
         }
     }
 }
    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);
    }