/** * @param \AssetsBundle\AssetFile\AssetFile $oAssetFile * @return string */ public function sanitizeAssetFilePath(\AssetsBundle\AssetFile\AssetFile $oAssetFile) { return $oAssetFile->isAssetFilePathUrl() ? str_replace(array_merge(array('/'), $this->unwantedFilePathChars), '_', implode('/', array_slice(explode('/', preg_replace('/http:\\/\\/|https:\\/\\/|www./', '', $oAssetFile->getAssetFilePath())), 0, 1))) : $oAssetFile->getAssetFilePath(); }
/** * @param \AssetsBundle\AssetFile\AssetFile $oAssetFile * @param scalar $iLastModifiedTime * @return string */ public function getAssetFileBaseUrl(\AssetsBundle\AssetFile\AssetFile $oAssetFile, $iLastModifiedTime = null) { if ($oAssetFile->isAssetFilePathUrl()) { return $oAssetFile->getAssetFilePath(); } $sAssetPath = str_replace(array($this->getCachePath(), DIRECTORY_SEPARATOR), array('', '/'), $oAssetFile->getAssetFilePath()); if ($oAssetFile->getAssetFileType() === \AssetsBundle\AssetFile\AssetFile::ASSET_MEDIA) { return $this->getCacheUrl() . ltrim($sAssetPath, '/'); } if ($iLastModifiedTime === null) { $iLastModifiedTime = $oAssetFile->getAssetFileLastModified(); } return $this->getCacheUrl() . ltrim($sAssetPath, '/') . ($iLastModifiedTime ? (strpos($sAssetPath, '?') === false ? '?' : '&') . $iLastModifiedTime : ''); }
/** * @param \AssetsBundle\AssetFile\AssetFile $oAssetFile * @param \AssetsBundle\AssetFile\AssetFile $oSourceAssetFile * @return \AssetsBundle\AssetFile\AssetFile * @throws \LogicException */ public function cacheAssetFile(\AssetsBundle\AssetFile\AssetFile $oAssetFile, \AssetsBundle\AssetFile\AssetFile $oSourceAssetFile = null) { // Define source asset file if (!$oSourceAssetFile) { $oSourceAssetFile = $oAssetFile; } // Check that file need to be cached if ($this->isAssetFileCached($oSourceAssetFile)) { return $oAssetFile->setAssetFilePath($this->getAssetFileCachePath($oSourceAssetFile)); } // Retrieve asset file cache path $sCacheFilePath = $this->getAssetFileCachePath($oSourceAssetFile); // Retrieve cache file directory path $sCacheFileDirPath = dirname($sCacheFilePath); if ($sCacheFileDirPath === '.') { throw new \LogicException('Asset file cache path "' . $sCacheFilePath . '" does not provide a paretn directory'); } // Create directory if not exists if (!is_dir($sCacheFileDirPath)) { \Zend\Stdlib\ErrorHandler::start(); if (!mkdir($sCacheFileDirPath, 0775, true)) { throw new \RuntimeException('Error occured while creating directory "' . $sCacheFileDirPath . '"'); } if ($oException = \Zend\Stdlib\ErrorHandler::stop()) { throw new \RuntimeException('Error occured while creating directory "' . $sCacheFileDirPath . '"', $oException->getCode(), $oException); } } elseif (!is_writable($sCacheFileDirPath)) { \Zend\Stdlib\ErrorHandler::start(); if (!chmod($sCacheFileDirPath, 0775)) { throw new \RuntimeException('Error occured while changing mode on directory "' . $sCacheFileDirPath . '"'); } \Zend\Stdlib\ErrorHandler::stop(true); } // Cache remote asset file if ($oAssetFile->isAssetFilePathUrl()) { \Zend\Stdlib\ErrorHandler::start(); $oAssetFileFileHandle = fopen($oAssetFile->getAssetFilePath(), 'rb'); \Zend\Stdlib\ErrorHandler::stop(true); if (!$oAssetFileFileHandle) { throw new \LogicException('Unable to open asset file "' . $oAssetFile->getAssetFilePath() . '"'); } \Zend\Stdlib\ErrorHandler::start(); file_put_contents($sCacheFilePath, stream_get_contents($oAssetFileFileHandle)); \Zend\Stdlib\ErrorHandler::stop(true); \Zend\Stdlib\ErrorHandler::start(); fclose($oAssetFileFileHandle); \Zend\Stdlib\ErrorHandler::stop(true); } else { \Zend\Stdlib\ErrorHandler::start(); $sAssetFilePath = $oAssetFile->getAssetFilePath(); if (!is_file($sAssetFilePath)) { throw new \LogicException('Asset file "' . $sAssetFilePath . '" does not exits'); } copy($sAssetFilePath, $sCacheFilePath); \Zend\Stdlib\ErrorHandler::stop(true); } return $oAssetFile->setAssetFilePath($sCacheFilePath); }