/** * Retrieve the filesystem path to a view script * * @param string $name * @param null|Renderer $renderer * @throws \Zend\View\Exception\DomainException * @return string */ public function resolve($name, Renderer $renderer = null) { if (!self::$currentLayoutId) { $activeLayouts = LayoutService::getCurrentLayouts(); self::$currentLayoutId = end($activeLayouts)['name']; } // generate a cache name $cacheName = CacheUtility::getCacheName(self::CACHE_TEMPLATE_PATH, [$name, $renderer, self::$currentLayoutId]); // check data in cache if (null === ($templatePath = $this->dynamicCacheInstance->getItem($cacheName))) { if (false !== ($templatePath = parent::resolve($name, $renderer))) { // save data in cache $this->dynamicCacheInstance->setItem($cacheName, $templatePath); } } return $templatePath; }
/** * Select layout */ public function ajaxSelectLayoutAction() { $request = $this->getRequest(); if ($request->isPost()) { if ((int) $this->applicationSetting('layout_select')) { $layoutId = $this->getSlug(-1); $layouts = LayoutService::getLayouts(false); // save selected layout if (array_key_exists($layoutId, $layouts)) { if (!$this->isGuest()) { $user = UserIdentityService::getCurrentUserIdentity(); $this->getModel()->selectLayout($layoutId, $user['user_id']); } LayoutCookieUtility::saveLayout($layoutId); } } } return $this->getResponse(); }
/** * Get resource's url * * @param string $fileName * @param string $type (possible values are: js, css and image) * @param string $module * @return string|false */ public function __invoke($fileName, $type = 'js', $module = self::DEFAULT_MODULE) { if (!self::$currentLayoutId) { $activeLayouts = LayoutService::getCurrentLayouts(); self::$currentLayoutId = end($activeLayouts)['name']; } // generate a dynamicCacheInstance name $dynamicCacheInstanceName = CacheUtility::getCacheName(self::CACHE_RESOURCE_PATH, [$fileName, $type, $module, self::$currentLayoutId]); if (null === ($resourcePath = $this->dynamicCacheInstance->getItem($dynamicCacheInstanceName))) { $baseResourcePath = $this->layoutPath; // get a resource url foreach ($this->layouts as $layout) { $checkResourcePath = $baseResourcePath . $layout['name'] . '/' . $module . '/' . $type . '/' . $fileName; if (file_exists($checkResourcePath)) { $resourcePath = $this->layoutDir . '/' . $layout['name'] . '/' . $module . '/' . $type . '/' . $fileName; // save data in dynamicCacheInstance $this->dynamicCacheInstance->setItem($dynamicCacheInstanceName, $resourcePath); } } } return $resourcePath ? $resourcePath : false; }
/** * Init view helpers * * @return array */ public function getViewHelperConfig() { return ['invokables' => ['layoutHeadLink' => 'Layout\\View\\Helper\\LayoutHeadLink', 'layoutHeadScript' => 'Layout\\View\\Helper\\LayoutHeadScript'], 'factories' => ['layoutAsset' => function () { $cache = $this->serviceLocator->get('Application\\Cache\\Dynamic'); return new \Layout\View\Helper\LayoutAsset($cache, LayoutService::getLayoutPath(), LayoutService::getCurrentLayouts(), LayoutService::getLayoutDir()); }, 'layoutList' => function () { $layouts = $this->serviceLocator->get('Application\\Model\\ModelManager')->getInstance('Layout\\Model\\LayoutBase'); return new \Layout\View\Helper\LayoutList($layouts->getAllInstalledLayouts(), LayoutService::getCurrentLayouts()); }]]; }
/** * Clear css cache * * @return boolean */ public static function clearCssCache() { try { return ApplicationFileSystem::deleteFiles(LayoutService::getLayoutCachePath()); } catch (Exception $e) { ApplicationErrorLogger::log($e); } return false; }
/** * Process css cache items * * @param array $cacheItems * @return array */ protected function processCssCacheItems(array $cacheItems) { $items = []; // process cache items foreach ($cacheItems as $media => $conditions) { foreach ($conditions as $condition => $files) { $filesHash = null; // get files hash foreach ($files as $file) { $filesHash .= $file->href; } // check files hash $cacheFile = md5($filesHash . $condition . $media) . $this->cacheCssFileExtension; $layoutCachePath = LayoutService::getLayoutCachePath(); // generate new cache file if (!file_exists($layoutCachePath . $cacheFile)) { $content = null; // get file content foreach ($files as $file) { // check the file sheme $fileInfo = parse_url($file->href); // add absolute path to file if (empty($fileInfo['scheme'])) { $file->path = APPLICATION_PUBLIC . '/' . $file->href; } $basePath = empty($fileInfo['scheme']) ? $this->view->basePath() . '/' . dirname($file->href) : dirname($file->href); // get file content if (false !== ($result = $this->processCssContent(isset($file->path) ? $file->path : $file->href, $basePath))) { $content .= $result; } } $cacheFilePath = $layoutCachePath . $cacheFile; // write cache $this->genCacheFile($cacheFilePath, $content); // check css cache gzip status if ($this->isCssCacheGzipEnabled()) { $this->gzipContent($cacheFilePath, $content); } } // get new url $file->href = LayoutService::getLayoutCacheDir() . '/' . $cacheFile; $items[] = $this->itemToString($file); } } return $items; }
/** * Process cache items * * @param array $cacheItems * @param string $indent String to add before the item * @param string $escapeStart Starting sequence * @param string $escapeEnd Ending sequence * @return array */ protected function processCacheItems(array $cacheItems, $indent, $escapeStart, $escapeEnd) { $items = []; // process cache items foreach ($cacheItems as $scriptType => $conditions) { foreach ($conditions as $condition => $scripts) { $scriptsHash = null; // get scripts hash foreach ($scripts as $script) { if (!empty($script->source)) { $scriptsHash .= $script->source; } else { // check the file sheme $fileInfo = parse_url($script->attributes['src']); // add absolute path to file if (empty($fileInfo['scheme'])) { $script->attributes['src'] = APPLICATION_PUBLIC . '/' . $script->attributes['src']; } $scriptsHash .= $script->attributes['src']; } } // check scripts hash $cacheFile = md5($scriptsHash . $condition . $scriptType) . $this->cacheFileExtension; $layoutCachePath = LayoutService::getLayoutCachePath('js'); // generate new cache file if (!file_exists($layoutCachePath . $cacheFile)) { $content = null; foreach ($scripts as $script) { if (!empty($script->source)) { $content .= $script->source . PHP_EOL; } else { if (!empty($script->attributes['src'])) { // get file content if (false !== ($result = file_get_contents($script->attributes['src']))) { $content .= $result . PHP_EOL; } } } } $cacheFilePath = $layoutCachePath . $cacheFile; // write cache $this->genCacheFile($cacheFilePath, $content); // check cache gzip status if ($this->isCacheGzipEnabled()) { $this->gzipContent($cacheFilePath, $content); } } $itemInfo = new stdClass(); $itemInfo->type = $scriptType; $itemInfo->attributes['conditional'] = $condition; $itemInfo->attributes['src'] = LayoutService::getLayoutCacheDir('js') . '/' . $cacheFile; $items[] = $this->itemToString($itemInfo, $indent, $escapeStart, $escapeEnd); } } return $items; }