/** * Replace dynamic script tags. * * @see \Contao\Controller::replaceDynamicScriptTags * * @param string $buffer * * @return string * * @SuppressWarnings(PHPMD.Superglobals) */ public function hookReplaceDynamicScriptTags($buffer) { $page = $this->pageProvider->getPage(); if ($page) { // search for the layout $layout = \LayoutModel::findByPk($page->layout); $renderMode = $this->renderModeDeterminer->determineMode(); // the stylesheets buffer $stylesheets = ''; if (RenderMode::PRE_COMPILE == $renderMode) { // pre-compile stylesheets $this->compileStylesheets($page, $layout); } elseif (!$GLOBALS['TL_CONFIG']['theme_plus_disabled_advanced_asset_caching'] && RenderMode::LIVE == $renderMode) { // load cached stylesheets $this->loadStylesheets($page, $layout, $stylesheets); } else { // dynamically parse stylesheets $this->parseStylesheets($renderMode, $page, $layout, $stylesheets); } $GLOBALS['TL_FRAMEWORK_CSS'] = []; $GLOBALS['TL_CSS'] = []; $GLOBALS['TL_USER_CSS'] = []; // replace dynamic scripts return str_replace('[[TL_CSS]]', $stylesheets, $buffer); } return $buffer; }
/** * Replace dynamic script tags. * * @see \Contao\Controller::replaceDynamicScriptTags * * @param string $buffer * * @return string */ public function hookReplaceDynamicScriptTags($buffer) { $page = $this->pageProvider->getPage(); if ($page) { $headScripts = ''; $bodyScripts = ''; // search for the layout $layout = \LayoutModel::findByPk($page->layout); $renderMode = $this->renderModeDeterminer->determineMode(); if (RenderMode::PRE_COMPILE == $renderMode) { // pre-compile javascripts $this->compileJavaScripts($page, $layout); } elseif (!$GLOBALS['TL_CONFIG']['theme_plus_disabled_advanced_asset_caching'] && RenderMode::LIVE == $renderMode) { // load cached javascripts $this->loadJavaScripts($page, $layout, $headScripts, $bodyScripts); } else { // dynamically parse javascripts $this->parseJavaScripts($renderMode, $page, $layout, $headScripts, $bodyScripts); } $GLOBALS['TL_JAVASCRIPT'] = []; // replace dynamic scripts return str_replace(['[[TL_HEAD]]', '[[TL_SCRIPTS]]'], [$headScripts . '[[TL_HEAD]]', $bodyScripts], $buffer); } return $buffer; }
/** * Get the page. * * @return PageModel|null */ private function getPage() { $page = $this->pageProvider->getPage(); if ($page) { return $this->i18n->getBasePage($page) ?: $page; } return null; }
/** * 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 renderInlineHtml(RenderAssetHtmlEvent $event) { if (!$event->getHtml() && !$event->isDesignerMode()) { $asset = $event->getAsset(); if ($asset instanceof ExtendedAssetInterface && $asset->isInline()) { $page = $this->pageProvider->getPage(); // 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()); $css = $asset->dump($event->getDefaultFilters()); // html mode $xhtml = $page->outputFormat == 'xhtml'; // generate html $styleHtml = '<style'; if ($xhtml) { $styleHtml .= ' type="text/css"'; } if ($asset instanceof ExtendedAssetInterface && $asset->getMediaQuery()) { $styleHtml .= sprintf(' media="%s"', $asset->getMediaQuery()); } $styleHtml .= '>'; $styleHtml .= $css; $styleHtml .= '</style>'; // wrap cc around if ($asset instanceof ExtendedAssetInterface && $asset->getConditionalComment()) { $styleHtml = ThemePlusUtils::wrapCc($styleHtml, $asset->getConditionalComment()); } $styleHtml .= PHP_EOL; $event->setHtml($styleHtml); } } }
/** * @param $strBuffer */ public function inject($strBuffer) { $page = $this->pageProvider->getPage(); if ($page) { // search for the layout $layout = \LayoutModel::findByPk($page->layout); $files = []; $stylesheetsCount = 0; $stylesheetsBuffer = ''; $javascriptsCount = 0; $javascriptsBuffer = ''; foreach ($this->files as $id => $file) { $asset = $file->asset; $files[] = $id; while ($asset instanceof DelegatorAssetInterface) { $asset = $asset->getAsset(); } if ($asset instanceof FileAsset) { $sourcePath = $asset->getSourcePath(); $fileName = basename($asset->getSourcePath()); } else { if ($asset instanceof HttpAsset) { $class = new \ReflectionClass($asset); $property = $class->getProperty('sourceUrl'); $property->setAccessible(true); $sourcePath = $property->getValue($asset); $fileName = parse_url($sourcePath, PHP_URL_PATH); } else { $sourcePath = $asset->getTargetPath(); $fileName = basename($sourcePath); } } switch ($file->type) { case 'css': $icon = '<img src="assets/theme-plus/images/stylesheet.png">'; $type = 'css'; $buffer =& $stylesheetsBuffer; $stylesheetsCount++; break; case 'js': $icon = '<img src="assets/theme-plus/images/javascript.png">'; $type = 'js'; $buffer =& $javascriptsBuffer; $javascriptsCount++; break; } $buffer .= sprintf('<div id="monitor-%s" class="theme-plus-dev-tool-monitor theme-plus-dev-tool-type-%s theme-plus-dev-tool-loading">' . '%s ' . '<a href="%s" target="_blank" class="theme-plus-dev-tool-link">%s</a>' . '</div> ', $id, $type, $icon, $file->url, $sourcePath); } $strBuffer = preg_replace('~<base[^>]+>~', sprintf('$0 <link rel="stylesheet" href="assets/theme-plus/stylesheets/dev.css"> <script src="assets/theme-plus/javascripts/dev.js"></script>'), $strBuffer); $strBuffer = preg_replace('|<body[^>]*>|', sprintf('$0 <!-- indexer::stop --> <div id="theme-plus-dev-tool" class="%s"> <div id="theme-plus-dev-tool-toggler" title="Theme+ developers tool">T+</div> <div id="theme-plus-dev-tool-stylesheets"> <div id="theme-plus-dev-tool-stylesheets-counter">%s <span id="theme-plus-dev-tool-stylesheets-count">0</span> / <span id="theme-plus-dev-tool-stylesheets-total">%d</span></div> <div id="theme-plus-dev-tool-stylesheets-files">%s</div> </div> <div id="theme-plus-dev-tool-javascripts"> <div id="theme-plus-dev-tool-javascripts-counter">%s <span id="theme-plus-dev-tool-javascripts-count">0</span> / <span id="theme-plus-dev-tool-javascripts-total">%d</span></div> <div id="theme-plus-dev-tool-javascripts-files">%s</div> </div> <div id="theme-plus-dev-tool-exception"></div> </div> <script>initThemePlusDevTool(%s, %s);</script> <!-- indexer::continue -->', \Input::cookie('THEME_PLUS_DEV_TOOL_COLLAPES') == 'no' ? '' : 'theme-plus-dev-tool-collapsed', \Image::getHtml('assets/theme-plus/images/stylesheet.png'), $stylesheetsCount, $stylesheetsBuffer, \Image::getHtml('assets/theme-plus/images/javascript.png'), $javascriptsCount, $javascriptsBuffer, json_encode($files), json_encode((bool) $layout->theme_plus_javascript_lazy_load)), $strBuffer); } return $strBuffer; }