@@ -13,8 +13,8 @@ discard block |
||
| 13 | 13 | use PSFS\base\types\helpers\GeneratorHelper; |
| 14 | 14 | use PSFS\base\types\helpers\Inspector; |
| 15 | 15 | |
| 16 | -defined('CSS_SRI_FILENAME') or define('CSS_SRI_FILENAME', CACHE_DIR . DIRECTORY_SEPARATOR . 'css.sri.json'); |
|
| 17 | -defined('JS_SRI_FILENAME') or define('JS_SRI_FILENAME', CACHE_DIR . DIRECTORY_SEPARATOR . 'js.sri.json'); |
|
| 16 | +defined('CSS_SRI_FILENAME') or define('CSS_SRI_FILENAME', CACHE_DIR.DIRECTORY_SEPARATOR.'css.sri.json'); |
|
| 17 | +defined('JS_SRI_FILENAME') or define('JS_SRI_FILENAME', CACHE_DIR.DIRECTORY_SEPARATOR.'js.sri.json'); |
|
| 18 | 18 | /** |
| 19 | 19 | * Class AssetsParser |
| 20 | 20 | * @package PSFS\base\extension |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | /** @var Cache $cache */ |
| 67 | 67 | $cache = Cache::getInstance(); |
| 68 | 68 | $this->sri = $cache->getDataFromFile($this->sriFilename, Cache::JSON, true); |
| 69 | - if(empty($this->sri)) { |
|
| 69 | + if (empty($this->sri)) { |
|
| 70 | 70 | $this->sri = []; |
| 71 | 71 | } |
| 72 | 72 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | public function __construct($type = 'js') |
| 80 | 80 | { |
| 81 | 81 | $this->type = $type; |
| 82 | - $this->path = WEB_DIR . DIRECTORY_SEPARATOR; |
|
| 82 | + $this->path = WEB_DIR.DIRECTORY_SEPARATOR; |
|
| 83 | 83 | $this->domains = Template::getDomains(true); |
| 84 | 84 | $this->cdnPath = Config::getParam('resources.cdn.url', Request::getInstance()->getRootUrl()); |
| 85 | 85 | } |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function addFile($filename) |
| 94 | 94 | { |
| 95 | - if (file_exists($this->path . $filename) && preg_match('/\.' . $this->type . '$/i', $filename)) { |
|
| 95 | + if (file_exists($this->path.$filename) && preg_match('/\.'.$this->type.'$/i', $filename)) { |
|
| 96 | 96 | $this->files[] = $filename; |
| 97 | 97 | } elseif (!empty($this->domains)) { |
| 98 | 98 | foreach ($this->domains as $domain => $paths) { |
| 99 | 99 | $domainFilename = str_replace($domain, $paths["public"], $filename); |
| 100 | - if (file_exists($domainFilename) && preg_match('/\.' . $this->type . '$/i', $domainFilename)) { |
|
| 100 | + if (file_exists($domainFilename) && preg_match('/\.'.$this->type.'$/i', $domainFilename)) { |
|
| 101 | 101 | $this->files[] = $domainFilename; |
| 102 | 102 | } |
| 103 | 103 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | public function setHash($hash) |
| 115 | 115 | { |
| 116 | 116 | $cache = Config::getParam('cache.var', ''); |
| 117 | - $this->hash = $hash . (strlen($cache) ? '.' : '') . $cache; |
|
| 117 | + $this->hash = $hash.(strlen($cache) ? '.' : '').$cache; |
|
| 118 | 118 | return $this; |
| 119 | 119 | } |
| 120 | 120 | |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | $sourceFile = explode("?", $sourceFile); |
| 178 | 178 | $sourceFile = $sourceFile[0]; |
| 179 | 179 | } |
| 180 | - $orig = realpath(dirname($filenamePath) . DIRECTORY_SEPARATOR . $sourceFile); |
|
| 180 | + $orig = realpath(dirname($filenamePath).DIRECTORY_SEPARATOR.$sourceFile); |
|
| 181 | 181 | return $orig; |
| 182 | 182 | } |
| 183 | 183 | |
@@ -194,15 +194,15 @@ discard block |
||
| 194 | 194 | if (preg_match_all('#url\((.*?)\)#', $line, $urls, PREG_SET_ORDER)) { |
| 195 | 195 | foreach ($urls as $source) { |
| 196 | 196 | $orig = self::calculateResourcePathname($filenamePath, $source); |
| 197 | - if(!empty($orig)) { |
|
| 197 | + if (!empty($orig)) { |
|
| 198 | 198 | $orig_part = preg_split("/Public/i", $orig); |
| 199 | - $dest = WEB_DIR . $orig_part[1]; |
|
| 199 | + $dest = WEB_DIR.$orig_part[1]; |
|
| 200 | 200 | GeneratorHelper::createDir(dirname($dest)); |
| 201 | 201 | if (@copy($orig, $dest) === false) { |
| 202 | - throw new ConfigException("Can't copy " . $orig . " to " . $dest); |
|
| 202 | + throw new ConfigException("Can't copy ".$orig." to ".$dest); |
|
| 203 | 203 | } |
| 204 | - } else { |
|
| 205 | - Logger::log($filenamePath . ' has an empty origin with the url ' . $source, LOG_WARNING); |
|
| 204 | + }else { |
|
| 205 | + Logger::log($filenamePath.' has an empty origin with the url '.$source, LOG_WARNING); |
|
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 | } |
@@ -215,11 +215,11 @@ discard block |
||
| 215 | 215 | * @throws \PSFS\base\exception\GeneratorException |
| 216 | 216 | */ |
| 217 | 217 | protected function getSriHash($hash, $type = 'js') { |
| 218 | - if(array_key_exists($hash, $this->sri)) { |
|
| 218 | + if (array_key_exists($hash, $this->sri)) { |
|
| 219 | 219 | $sriHash = $this->sri[$hash]; |
| 220 | - } else { |
|
| 221 | - Inspector::stats('[SRITrait] Generating SRI for ' . $hash, Inspector::SCOPE_DEBUG); |
|
| 222 | - $filename = WEB_DIR . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $hash . '.' . $type; |
|
| 220 | + }else { |
|
| 221 | + Inspector::stats('[SRITrait] Generating SRI for '.$hash, Inspector::SCOPE_DEBUG); |
|
| 222 | + $filename = WEB_DIR.DIRECTORY_SEPARATOR.$type.DIRECTORY_SEPARATOR.$hash.'.'.$type; |
|
| 223 | 223 | $sriHash = base64_encode(hash("sha384", file_get_contents($filename), true)); |
| 224 | 224 | $this->sri[$hash] = $sriHash; |
| 225 | 225 | Cache::getInstance()->storeData($this->sriFilename, $this->sri, Cache::JSON, true); |
@@ -30,8 +30,8 @@ discard block |
||
| 30 | 30 | protected function compileCss($basePath, $hash) |
| 31 | 31 | { |
| 32 | 32 | $debug = Config::getParam('debug'); |
| 33 | - $base = $basePath . "css" . DIRECTORY_SEPARATOR; |
|
| 34 | - if ($debug || !file_exists($base . $hash . ".css")) { |
|
| 33 | + $base = $basePath."css".DIRECTORY_SEPARATOR; |
|
| 34 | + if ($debug || !file_exists($base.$hash.".css")) { |
|
| 35 | 35 | $data = ''; |
| 36 | 36 | if (0 < count($this->files)) { |
| 37 | 37 | $minifier = new CSS(); |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | $data = $this->processCssLine($file, $base, $data, $hash); |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | - if($debug) { |
|
| 43 | - AssetsHelper::storeContents($base . $hash . ".css", $data); |
|
| 44 | - } else { |
|
| 42 | + if ($debug) { |
|
| 43 | + AssetsHelper::storeContents($base.$hash.".css", $data); |
|
| 44 | + }else { |
|
| 45 | 45 | $minifier = new CSS(); |
| 46 | 46 | $minifier->add($data); |
| 47 | 47 | ini_set('max_execution_time', -1); |
| 48 | 48 | ini_set('memory_limit', -1); |
| 49 | 49 | GeneratorHelper::createDir($base); |
| 50 | - $minifier->minify($base . $hash . ".css"); |
|
| 50 | + $minifier->minify($base.$hash.".css"); |
|
| 51 | 51 | } |
| 52 | 52 | unset($minifier); |
| 53 | 53 | } |
@@ -67,21 +67,21 @@ discard block |
||
| 67 | 67 | if (file_exists($file)) { |
| 68 | 68 | $debug = Config::getParam('debug'); |
| 69 | 69 | $pathParts = explode("/", $file); |
| 70 | - $filePath = $this->hash . "_" . $pathParts[count($pathParts) - 1]; |
|
| 71 | - if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file) || $debug) { |
|
| 70 | + $filePath = $this->hash."_".$pathParts[count($pathParts) - 1]; |
|
| 71 | + if (!file_exists($base.$filePath) || filemtime($base.$filePath) < filemtime($file) || $debug) { |
|
| 72 | 72 | //Si tenemos modificaciones tenemos que compilar de nuevo todos los ficheros modificados |
| 73 | - if (file_exists($base . $hash . ".css") && @unlink($base . $hash . ".css") === false) { |
|
| 74 | - throw new ConfigException("Can't unlink file " . $base . $hash . ".css"); |
|
| 73 | + if (file_exists($base.$hash.".css") && @unlink($base.$hash.".css") === false) { |
|
| 74 | + throw new ConfigException("Can't unlink file ".$base.$hash.".css"); |
|
| 75 | 75 | } |
| 76 | 76 | $this->loopCssLines($file); |
| 77 | 77 | } |
| 78 | 78 | if ($debug) { |
| 79 | 79 | $data = file_get_contents($file); |
| 80 | - AssetsHelper::storeContents($base . $filePath, $data); |
|
| 81 | - } else { |
|
| 80 | + AssetsHelper::storeContents($base.$filePath, $data); |
|
| 81 | + }else { |
|
| 82 | 82 | $data .= file_get_contents($file); |
| 83 | 83 | } |
| 84 | - $this->compiledFiles[] = "/css/" . $filePath; |
|
| 84 | + $this->compiledFiles[] = "/css/".$filePath; |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | return $data; |
@@ -113,25 +113,25 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | protected function extractCssResources($source, $file) |
| 115 | 115 | { |
| 116 | - Inspector::stats('[CssTrait] Start collecting resources from ' . $file, Inspector::SCOPE_DEBUG); |
|
| 116 | + Inspector::stats('[CssTrait] Start collecting resources from '.$file, Inspector::SCOPE_DEBUG); |
|
| 117 | 117 | $sourceFile = AssetsHelper::extractSourceFilename($source); |
| 118 | - $orig = realpath(dirname($file) . DIRECTORY_SEPARATOR . $sourceFile); |
|
| 118 | + $orig = realpath(dirname($file).DIRECTORY_SEPARATOR.$sourceFile); |
|
| 119 | 119 | $origPart = preg_split('/(\/|\\\)public(\/|\\\)/i', $orig); |
| 120 | 120 | try { |
| 121 | 121 | if (count($source) > 1 && array_key_exists(1, $origPart)) { |
| 122 | - $dest = $this->path . $origPart[1]; |
|
| 122 | + $dest = $this->path.$origPart[1]; |
|
| 123 | 123 | GeneratorHelper::createDir(dirname($dest)); |
| 124 | 124 | if (!file_exists($dest) || filemtime($orig) > filemtime($dest)) { |
| 125 | 125 | if (@copy($orig, $dest) === FALSE) { |
| 126 | - throw new \RuntimeException('Can\' copy ' . $dest . ''); |
|
| 126 | + throw new \RuntimeException('Can\' copy '.$dest.''); |
|
| 127 | 127 | } |
| 128 | 128 | Logger::log("$orig copiado a $dest", LOG_INFO); |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | - } catch (\Exception $e) { |
|
| 131 | + }catch (\Exception $e) { |
|
| 132 | 132 | Logger::log($e->getMessage(), LOG_ERR); |
| 133 | 133 | } |
| 134 | - Inspector::stats('[CssTrait] End collecting resources from ' . $file, Inspector::SCOPE_DEBUG); |
|
| 134 | + Inspector::stats('[CssTrait] End collecting resources from '.$file, Inspector::SCOPE_DEBUG); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /** |
@@ -146,9 +146,9 @@ discard block |
||
| 146 | 146 | foreach ($compiledFiles as $file) { |
| 147 | 147 | echo "\t\t<link href='{$file}' rel='stylesheet' media='screen, print'>"; |
| 148 | 148 | } |
| 149 | - } else { |
|
| 150 | - echo "\t\t<link href='" . $baseUrl . "/css/" . $hash . ".css' rel='stylesheet' " . |
|
| 151 | - "crossorigin='anonymous' integrity='sha384-" . $sri . "'>"; |
|
| 149 | + }else { |
|
| 150 | + echo "\t\t<link href='".$baseUrl."/css/".$hash.".css' rel='stylesheet' ". |
|
| 151 | + "crossorigin='anonymous' integrity='sha384-".$sri."'>"; |
|
| 152 | 152 | } |
| 153 | 153 | } |
| 154 | 154 | } |
@@ -24,9 +24,9 @@ discard block |
||
| 24 | 24 | foreach ($compiledFiles as $file) { |
| 25 | 25 | echo "\t\t<script type='text/javascript' src='{$file}'></script>\n"; |
| 26 | 26 | } |
| 27 | - } else { |
|
| 28 | - echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'" . |
|
| 29 | - " crossorigin='anonymous' integrity='sha384-" . $sri . "'></script>\n"; |
|
| 27 | + }else { |
|
| 28 | + echo "\t\t<script type='text/javascript' src='".$baseUrl."/js/".$hash.".js'". |
|
| 29 | + " crossorigin='anonymous' integrity='sha384-".$sri."'></script>\n"; |
|
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
@@ -41,12 +41,12 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | protected function putDebugJs($pathParts, $base, $file, $hash, array &$compiledFiles) |
| 43 | 43 | { |
| 44 | - $filePath = $hash . "_" . $pathParts[count($pathParts) - 1]; |
|
| 45 | - $compiledFiles[] = "/js/" . $filePath; |
|
| 44 | + $filePath = $hash."_".$pathParts[count($pathParts) - 1]; |
|
| 45 | + $compiledFiles[] = "/js/".$filePath; |
|
| 46 | 46 | $data = ""; |
| 47 | - if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file)) { |
|
| 47 | + if (!file_exists($base.$filePath) || filemtime($base.$filePath) < filemtime($file)) { |
|
| 48 | 48 | $data = file_get_contents($file); |
| 49 | - AssetsHelper::storeContents($base . $filePath, $data); |
|
| 49 | + AssetsHelper::storeContents($base.$filePath, $data); |
|
| 50 | 50 | } |
| 51 | 51 | return $data; |
| 52 | 52 | } |
@@ -62,9 +62,9 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | protected function compileJs(array $files, $basePath, $hash, array &$compiledFiles) |
| 64 | 64 | { |
| 65 | - $base = $basePath . "js" . DIRECTORY_SEPARATOR; |
|
| 65 | + $base = $basePath."js".DIRECTORY_SEPARATOR; |
|
| 66 | 66 | $debug = Config::getParam('debug'); |
| 67 | - if ($debug || !file_exists($base . $hash . ".js")) { |
|
| 67 | + if ($debug || !file_exists($base.$hash.".js")) { |
|
| 68 | 68 | $data = ''; |
| 69 | 69 | if (0 < count($files)) { |
| 70 | 70 | $minifier = new JS(); |
@@ -73,15 +73,15 @@ discard block |
||
| 73 | 73 | if (file_exists($file)) { |
| 74 | 74 | if ($debug) { |
| 75 | 75 | $data = $this->putDebugJs($pathParts, $base, $file, $hash, $compiledFiles); |
| 76 | - } elseif (!file_exists($base . $hash . ".js")) { |
|
| 76 | + } elseif (!file_exists($base.$hash.".js")) { |
|
| 77 | 77 | $minifier->add($file); |
| 78 | 78 | //$data = $this->putProductionJs($base, $file, $data); |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | - if($debug) { |
|
| 83 | - AssetsHelper::storeContents($base . $hash . ".js", $data); |
|
| 84 | - } else { |
|
| 82 | + if ($debug) { |
|
| 83 | + AssetsHelper::storeContents($base.$hash.".js", $data); |
|
| 84 | + }else { |
|
| 85 | 85 | $this->dumpJs($hash, $base, $minifier); |
| 86 | 86 | } |
| 87 | 87 | unset($minifier); |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | ini_set('memory_limit', -1); |
| 103 | 103 | GeneratorHelper::createDir($base); |
| 104 | 104 | if (Config::getParam('assets.obfuscate', false)) { |
| 105 | - $minifier->gzip($base . $hash . ".js"); |
|
| 106 | - } else { |
|
| 107 | - $minifier->minify($base . $hash . ".js"); |
|
| 105 | + $minifier->gzip($base.$hash.".js"); |
|
| 106 | + }else { |
|
| 107 | + $minifier->minify($base.$hash.".js"); |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | } |