diff options
Diffstat (limited to 'app/php/web/ClientScriptManager.php')
-rw-r--r-- | app/php/web/ClientScriptManager.php | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/app/php/web/ClientScriptManager.php b/app/php/web/ClientScriptManager.php index 2dab6e9..1a73b3e 100644 --- a/app/php/web/ClientScriptManager.php +++ b/app/php/web/ClientScriptManager.php @@ -11,7 +11,7 @@ class ClientScriptManager extends TClientScriptManager { } private function _getBasePath() { - return Prado::getPathOfNamespace('Web') . DIRECTORY_SEPARATOR; + return Prado::getPathOfNamespace('Web'); } private function _getBasePaths($urls) { @@ -34,9 +34,10 @@ class ClientScriptManager extends TClientScriptManager { if (!is_dir($cachePath)) { if (file_exists($cachePath)) { throw new TIOException( - 'Client script manager cache path "' - . $cachePath - . ' "exists and is not a directory' + sprintf( + 'Client script manager cache path "%s" exists and is not a directory', + $cachePath + ) ); } else { mkdir($cachePath); @@ -67,7 +68,7 @@ class ClientScriptManager extends TClientScriptManager { 'Compiled assets may only be used within Layout master class controls' ); } - return 'Rendered' . $type . '.' . $template->generateViewID(); + return sprintf('Rendered%s.%s', $type, $template->generateViewID()); } private function _getRenderedScriptsStoreKey() { @@ -88,6 +89,22 @@ class ClientScriptManager extends TClientScriptManager { return $cache; } + private function _isCacheValid($cacheFile, $paths) { + return file_exists($cacheFile) + && (filemtime($cacheFile) >= $this->_getFileCollectionMTime($paths)); + } + + private function _isFileLocal($file) { + $basePath = $this->_getBasePath(); + return file_exists($basePath . DIRECTORY_SEPARATOR . $file); + } + + private function _determineLocalFiles($files) { + return array_filter( + $files, [$this, '_isFileLocal'] + ); + } + // Scripts private $_renderedScriptsInitialized = FALSE; @@ -125,12 +142,12 @@ class ClientScriptManager extends TClientScriptManager { $this->markScriptFileAsRendered($file); } $paths = $this->_getBasePaths($files); - $cacheKey = $this->_getFileCollectionCacheKey($paths); - $cacheFile = $this->_getCacheFilePath($cacheKey . '.js', 'scripts'); + $cacheFile = $this->_getCacheFilePath( + $this->_getFileCollectionCacheKey($paths) . '.js', + 'scripts' + ); $this->_appendRenderedScripts($files, $cacheFile); - if (!file_exists($cacheFile) - || (filemtime($cacheFile) - < $this->_getFileCollectionMTime($paths))) { + if (!$this->_isCacheValid($cacheFile, $paths)) { $scriptContent = implode( PHP_EOL, array_map( @@ -149,16 +166,6 @@ class ClientScriptManager extends TClientScriptManager { return $this->Application->AssetManager->publishFilePath($cacheFile); } - private function _determineLocalScriptFiles($files) { - $basePath = $this->_getBasePath(); - return array_filter( - $files, - function($file) use($basePath) { - return file_exists($basePath . DIRECTORY_SEPARATOR . $file); - } - ); - } - private function _renderLocalScriptFiles($writer, $localFiles) { if ($localFiles) { $assetPath = $this->_compileScriptFiles($localFiles); @@ -179,7 +186,7 @@ class ClientScriptManager extends TClientScriptManager { public function renderScriptFiles($writer, Array $files) { if ($this->getApplication()->getMode() !== TApplicationMode::Debug) { if ($files) { - $localFiles = $this->_determineLocalScriptFiles($files); + $localFiles = $this->_determineLocalFiles($files); $this->_renderLocalScriptFiles($writer, $localFiles); $externalFiles = array_diff($files, $localFiles); $this->_renderExternalScriptFiles($writer, $externalFiles); @@ -224,7 +231,7 @@ class ClientScriptManager extends TClientScriptManager { $newScripts[] = $registeredScript; } } - $newLocalScripts = $this->_determineLocalScriptFiles($newScripts); + $newLocalScripts = $this->_determineLocalFiles($newScripts); $newRemoteScripts = array_diff($newScripts, $newLocalScripts); if ($newLocalScripts) { $scriptUrls[] = $this->_compileScriptFiles($newLocalScripts); @@ -306,9 +313,7 @@ class ClientScriptManager extends TClientScriptManager { $cacheKey = $this->_getFileCollectionCacheKey($paths); $cacheFile = $this->_getCacheFilePath($cacheKey . '.css', 'styles'); $this->_appendRenderedSheets($files, $cacheFile); - if (!file_exists($cacheFile) - || (filemtime($cacheFile) - < $this->_getFileCollectionMTime($paths))) { + if (!$this->_isCacheValid($cacheFile, $paths)) { Prado::using('Lib.cssmin.CssMin'); $styleContent = implode( PHP_EOL, |