/** * This event handler lists all activated modules with the description * of each module. * * @access public * @param \Zepi\Turbo\Framework $framework * @param \Zepi\Turbo\Request\WebRequest $request * @param \Zepi\Turbo\Response\Response $response */ public function execute(Framework $framework, WebRequest $request, Response $response) { // Display the main css group $this->assetCacheManager->displayAssetType(AssetManager::CSS); // Display the main js group $this->assetCacheManager->displayAssetType(AssetManager::JS); }
/** * Adds the needed headers and prepares the content * * @param \Zepi\Turbo\Response\Response $response * @param string $type * @param string $hash * @param string $version * @param string $content */ protected function deliverContent(Response $response, $type, $hash, $version, $content) { // Define the if modified since timestamp $cachedAssetTimestamp = $this->assetCacheManager->getCachedAssetTimestamp($type, $hash, $version); $ifModifiedSince = -1; if ($this->isHeaderSetAndNotEmpty('HTTP_IF_MODIFIED_SINCE')) { $ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE']; } // Define the etag $eTag = md5($content); $eTagHeader = -1; if ($this->isHeaderSetAndNotEmpty('HTTP_IF_NONE_MATCH')) { $eTagHeader = $_SERVER['HTTP_IF_NONE_MATCH']; } // Set the cache headers $cacheTtl = 86400 * 365; header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cachedAssetTimestamp) . ' GMT'); header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $cacheTtl) . ' GMT'); header('Pragma: cache'); header('Etag: ' . $eTag); header('Cache-Control: max-age=' . $cacheTtl); // Verify the cached timestamp and the eTag if ($cachedAssetTimestamp === $ifModifiedSince || $eTag === $eTagHeader) { header('HTTP/1.1 304 Not Modified'); exit; } // Set the content type $contentType = $this->getContentType($type, $version); if ($contentType !== '') { header('Content-type: ' . $contentType, true); } // Display the content $response->setOutput($content); }
/** * Initializes and return an instance of the given class name. * * @access public * @param string $className * @return mixed */ public function getInstance($className) { switch ($className) { case '\\Zepi\\Web\\General\\Manager\\AssetManager': if ($this->assetManager === null) { $this->assetManager = new $className($this->getDataObjectBackend('assets.data'), $this->framework->getRootDirectory()); $this->assetManager->initializeAssetManager(); } return $this->assetManager; break; case '\\Zepi\\Web\\General\\Manager\\AssetCacheManager': if ($this->assetCacheManager === null) { // Get the cache backends $path = $this->framework->getRootDirectory() . '/cache/'; $fileObjectBackend = new \Zepi\Turbo\Backend\FileObjectBackend($path . 'cachedFiles.data'); $fileBackend = new \Zepi\Turbo\Backend\FileBackend($path); // CSS helper $cssHelper = new \Zepi\Web\General\Helper\CssHelper($fileBackend); // Load the configuration $configurationManager = $this->framework->getInstance('\\Zepi\\Core\\Utils\\Manager\\ConfigurationManager'); $minifyAssets = $configurationManager->getSetting('assets.minifyAssets'); $combineAssetGroups = $configurationManager->getSetting('assets.combineAssetGroups'); $this->assetCacheManager = new $className($this->framework, $this->getInstance('\\Zepi\\Web\\General\\Manager\\AssetManager'), $fileObjectBackend, $fileBackend, $cssHelper, $minifyAssets, $combineAssetGroups); $this->assetCacheManager->initializeAssetCacheManager(); } return $this->assetCacheManager; break; case '\\Zepi\\Web\\General\\Manager\\TemplatesManager': if ($this->templatesManager === null) { $this->templatesManager = new $className($this->framework, $this->getDataObjectBackend('templates.data')); $this->templatesManager->initializeTemplatesManager(); // Execute the register renderer event $runtimeManager = $this->framework->getRuntimeManager(); $runtimeManager->executeEvent('\\Zepi\\Web\\General\\Event\\RegisterRenderers'); } return $this->templatesManager; break; case '\\Zepi\\Web\\General\\Manager\\MenuManager': case '\\Zepi\\Web\\General\\Manager\\MetaInformationManager': return $this->framework->initiateObject($className, array(), true); break; default: return $this->framework->initiateObject($className); break; } }
/** * Optimizes the urls in the css content. * * @access protected * @param string $content * @param string $file * @return string */ protected function optimizeUrls($content, $file) { preg_match_all('/url\\((.[^\\)]*)\\)/is', $content, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $uri = $match[1]; // Remove the apostrophs if (strpos($uri, '\'') === 0 || strpos($uri, '"') === 0) { $uri = substr($uri, 1, -1); } // If the uri is an absolute url we do not change anything if (strpos($uri, 'http') === 0) { continue; } $additionalData = $this->getAdditionalUriData($uri); $uri = $this->removeAdditionalUriData($uri); $path = dirname($file); $fullFilePath = realpath($path . '/' . $uri); $fileInformation = pathinfo($fullFilePath); $imageExtensions = array('png', 'jpg', 'jpeg', 'gif'); if (in_array($fileInformation['extension'], $imageExtensions)) { // Load the file content $fileContent = $this->fileBackend->loadFromFile($fullFilePath); // Encode the file content $encodedContent = base64_encode($fileContent); $urlData = 'data:image/gif;base64,' . $encodedContent; // Replace the reference in the css content $content = str_replace($match[1], '\'' . $urlData . '\'', $content); } else { $type = AssetManager::BINARY; // Cache the file $cachedFile = $this->assetCacheManager->generateCachedFile($type, $fullFilePath); $url = $this->assetCacheManager->getUrlToTheAssetLoader($cachedFile['file']); // Add the additional data if ($additionalData !== '') { $url .= $additionalData; } // Replace the reference in the css content $content = str_replace($match[1], '\'' . $url . '\'', $content); } } return $content; }
/** * This event handler clears the assets cache. * * @access public * @param \Zepi\Turbo\Framework $framework * @param \Zepi\Turbo\Request\CliRequest $request * @param \Zepi\Turbo\Response\Response $response */ public function execute(Framework $framework, CliRequest $request, Response $response) { // Clean the asset cache $this->assetCacheManager->clearAssetCache(); $response->setOutputPart('cacheCleared', 'The asset cache was successfully cleared!'); }