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); } } }
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'; // 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/js/%s/%s', $id, $name); // overwrite the target path $asset->setTargetPath($url); // remember asset for debug tool $this->developerTool->registerFile($id, (object) ['asset' => $realAssets, 'type' => 'js', 'url' => $url]); // generate html if ($event->getLayout()->theme_plus_javascript_lazy_load) { $scriptHtml = '<script'; $scriptHtml .= sprintf(' id="%s"', $id); if ($xhtml) { $scriptHtml .= ' type="text/javascript"'; } $scriptHtml .= '>'; $scriptHtml .= sprintf('window.loadAsync(%s, %s)', json_encode($url), json_encode($id)); $scriptHtml .= '</script>'; } else { $scriptHtml = '<script'; $scriptHtml .= sprintf(' id="%s"', $id); $scriptHtml .= sprintf(' src="%s"', $url); if ($xhtml) { $scriptHtml .= ' type="text/javascript"'; } $scriptHtml .= sprintf(' onload="window.themePlusDevTool && window.themePlusDevTool.triggerAsyncLoad(this, \'%s\');"', $id); $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); } } }