@@ -45,274 +45,274 @@ |
||
| 45 | 45 | |
| 46 | 46 | class SCSSCacher { |
| 47 | 47 | |
| 48 | - /** @var ILogger */ |
|
| 49 | - protected $logger; |
|
| 50 | - |
|
| 51 | - /** @var IAppData */ |
|
| 52 | - protected $appData; |
|
| 53 | - |
|
| 54 | - /** @var IURLGenerator */ |
|
| 55 | - protected $urlGenerator; |
|
| 56 | - |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - protected $config; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - protected $serverRoot; |
|
| 62 | - |
|
| 63 | - /** @var ICache */ |
|
| 64 | - protected $depsCache; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param ILogger $logger |
|
| 68 | - * @param Factory $appDataFactory |
|
| 69 | - * @param IURLGenerator $urlGenerator |
|
| 70 | - * @param IConfig $config |
|
| 71 | - * @param \OC_Defaults $defaults |
|
| 72 | - * @param string $serverRoot |
|
| 73 | - * @param ICache $depsCache |
|
| 74 | - */ |
|
| 75 | - public function __construct(ILogger $logger, |
|
| 76 | - Factory $appDataFactory, |
|
| 77 | - IURLGenerator $urlGenerator, |
|
| 78 | - IConfig $config, |
|
| 79 | - \OC_Defaults $defaults, |
|
| 80 | - $serverRoot, |
|
| 81 | - ICache $depsCache) { |
|
| 82 | - $this->logger = $logger; |
|
| 83 | - $this->appData = $appDataFactory->get('css'); |
|
| 84 | - $this->urlGenerator = $urlGenerator; |
|
| 85 | - $this->config = $config; |
|
| 86 | - $this->defaults = $defaults; |
|
| 87 | - $this->serverRoot = $serverRoot; |
|
| 88 | - $this->depsCache = $depsCache; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Process the caching process if needed |
|
| 93 | - * @param string $root Root path to the nextcloud installation |
|
| 94 | - * @param string $file |
|
| 95 | - * @param string $app The app name |
|
| 96 | - * @return boolean |
|
| 97 | - */ |
|
| 98 | - public function process($root, $file, $app) { |
|
| 99 | - $path = explode('/', $root . '/' . $file); |
|
| 100 | - |
|
| 101 | - $fileNameSCSS = array_pop($path); |
|
| 102 | - $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
|
| 103 | - |
|
| 104 | - $path = implode('/', $path); |
|
| 105 | - |
|
| 106 | - $webDir = substr($path, strlen($this->serverRoot)+1); |
|
| 107 | - |
|
| 108 | - try { |
|
| 109 | - $folder = $this->appData->getFolder($app); |
|
| 110 | - } catch(NotFoundException $e) { |
|
| 111 | - // creating css appdata folder |
|
| 112 | - $folder = $this->appData->newFolder($app); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 119 | - return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param $appName |
|
| 124 | - * @param $fileName |
|
| 125 | - * @return ISimpleFile |
|
| 126 | - */ |
|
| 127 | - public function getCachedCSS($appName, $fileName) { |
|
| 128 | - $folder = $this->appData->getFolder($appName); |
|
| 129 | - return $folder->getFile($this->prependBaseurlPrefix($fileName)); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Check if the file is cached or not |
|
| 134 | - * @param string $fileNameCSS |
|
| 135 | - * @param ISimpleFolder $folder |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - private function isCached($fileNameCSS, ISimpleFolder $folder) { |
|
| 139 | - try { |
|
| 140 | - $cachedFile = $folder->getFile($fileNameCSS); |
|
| 141 | - if ($cachedFile->getSize() > 0) { |
|
| 142 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 143 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 144 | - if ($deps === null) { |
|
| 145 | - $depFile = $folder->getFile($depFileName); |
|
| 146 | - $deps = $depFile->getContent(); |
|
| 147 | - //Set to memcache for next run |
|
| 148 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 149 | - } |
|
| 150 | - $deps = json_decode($deps, true); |
|
| 151 | - |
|
| 152 | - foreach ($deps as $file=>$mtime) { |
|
| 153 | - if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return true; |
|
| 159 | - } catch(NotFoundException $e) { |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Check if the variables file has changed |
|
| 166 | - * @return bool |
|
| 167 | - */ |
|
| 168 | - private function variablesChanged() { |
|
| 169 | - $injectedVariables = $this->getInjectedVariables(); |
|
| 170 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 171 | - $this->resetCache(); |
|
| 172 | - $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 173 | - return true; |
|
| 174 | - } |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Cache the file with AppData |
|
| 180 | - * @param string $path |
|
| 181 | - * @param string $fileNameCSS |
|
| 182 | - * @param string $fileNameSCSS |
|
| 183 | - * @param ISimpleFolder $folder |
|
| 184 | - * @param string $webDir |
|
| 185 | - * @return boolean |
|
| 186 | - */ |
|
| 187 | - private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) { |
|
| 188 | - $scss = new Compiler(); |
|
| 189 | - $scss->setImportPaths([ |
|
| 190 | - $path, |
|
| 191 | - \OC::$SERVERROOT . '/core/css/', |
|
| 192 | - ]); |
|
| 193 | - // Continue after throw |
|
| 194 | - $scss->setIgnoreErrors(true); |
|
| 195 | - if($this->config->getSystemValue('debug')) { |
|
| 196 | - // Debug mode |
|
| 197 | - $scss->setFormatter(Expanded::class); |
|
| 198 | - $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 199 | - } else { |
|
| 200 | - // Compression |
|
| 201 | - $scss->setFormatter(Crunched::class); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - try { |
|
| 205 | - $cachedfile = $folder->getFile($fileNameCSS); |
|
| 206 | - } catch(NotFoundException $e) { |
|
| 207 | - $cachedfile = $folder->newFile($fileNameCSS); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 211 | - try { |
|
| 212 | - $depFile = $folder->getFile($depFileName); |
|
| 213 | - } catch (NotFoundException $e) { |
|
| 214 | - $depFile = $folder->newFile($depFileName); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Compile |
|
| 218 | - try { |
|
| 219 | - $compiledScss = $scss->compile( |
|
| 220 | - '@import "variables.scss";' . |
|
| 221 | - $this->getInjectedVariables() . |
|
| 222 | - '@import "'.$fileNameSCSS.'";'); |
|
| 223 | - } catch(ParserException $e) { |
|
| 224 | - $this->logger->error($e, ['app' => 'core']); |
|
| 225 | - return false; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Gzip file |
|
| 229 | - try { |
|
| 230 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 231 | - } catch (NotFoundException $e) { |
|
| 232 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - try { |
|
| 236 | - $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 237 | - $cachedfile->putContent($data); |
|
| 238 | - $deps = json_encode($scss->getParsedFiles()); |
|
| 239 | - $depFile->putContent($deps); |
|
| 240 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 241 | - $gzipFile->putContent(gzencode($data, 9)); |
|
| 242 | - $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 243 | - return true; |
|
| 244 | - } catch(NotPermittedException $e) { |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Reset scss cache by deleting all generated css files |
|
| 251 | - * We need to regenerate all files when variables change |
|
| 252 | - */ |
|
| 253 | - private function resetCache() { |
|
| 254 | - $appDirectory = $this->appData->getDirectoryListing(); |
|
| 255 | - if(empty($appDirectory)){ |
|
| 256 | - return; |
|
| 257 | - } |
|
| 258 | - foreach ($appDirectory as $folder) { |
|
| 259 | - foreach ($folder->getDirectoryListing() as $file) { |
|
| 260 | - if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") { |
|
| 261 | - $file->delete(); |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @return string SCSS code for variables from OC_Defaults |
|
| 269 | - */ |
|
| 270 | - private function getInjectedVariables() { |
|
| 271 | - $variables = ''; |
|
| 272 | - foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 273 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 274 | - } |
|
| 275 | - return $variables; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Add the correct uri prefix to make uri valid again |
|
| 280 | - * @param string $css |
|
| 281 | - * @param string $webDir |
|
| 282 | - * @return string |
|
| 283 | - */ |
|
| 284 | - private function rebaseUrls($css, $webDir) { |
|
| 285 | - $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; |
|
| 286 | - // OC\Route\Router:75 |
|
| 287 | - if(($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 288 | - $subst = 'url(\'../../'.$webDir.'/$1\')'; |
|
| 289 | - } else { |
|
| 290 | - $subst = 'url(\'../../../'.$webDir.'/$1\')'; |
|
| 291 | - } |
|
| 292 | - return preg_replace($re, $subst, $css); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Return the cached css file uri |
|
| 297 | - * @param string $appName the app name |
|
| 298 | - * @param string $fileName |
|
| 299 | - * @return string |
|
| 300 | - */ |
|
| 301 | - public function getCachedSCSS($appName, $fileName) { |
|
| 302 | - $tmpfileLoc = explode('/', $fileName); |
|
| 303 | - $fileName = array_pop($tmpfileLoc); |
|
| 304 | - $fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)); |
|
| 305 | - |
|
| 306 | - return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Prepend hashed base url to the css file |
|
| 311 | - * @param $cssFile |
|
| 312 | - * @return string |
|
| 313 | - */ |
|
| 314 | - private function prependBaseurlPrefix($cssFile) { |
|
| 315 | - $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 316 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 317 | - } |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + protected $logger; |
|
| 50 | + |
|
| 51 | + /** @var IAppData */ |
|
| 52 | + protected $appData; |
|
| 53 | + |
|
| 54 | + /** @var IURLGenerator */ |
|
| 55 | + protected $urlGenerator; |
|
| 56 | + |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + protected $config; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $serverRoot; |
|
| 62 | + |
|
| 63 | + /** @var ICache */ |
|
| 64 | + protected $depsCache; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param ILogger $logger |
|
| 68 | + * @param Factory $appDataFactory |
|
| 69 | + * @param IURLGenerator $urlGenerator |
|
| 70 | + * @param IConfig $config |
|
| 71 | + * @param \OC_Defaults $defaults |
|
| 72 | + * @param string $serverRoot |
|
| 73 | + * @param ICache $depsCache |
|
| 74 | + */ |
|
| 75 | + public function __construct(ILogger $logger, |
|
| 76 | + Factory $appDataFactory, |
|
| 77 | + IURLGenerator $urlGenerator, |
|
| 78 | + IConfig $config, |
|
| 79 | + \OC_Defaults $defaults, |
|
| 80 | + $serverRoot, |
|
| 81 | + ICache $depsCache) { |
|
| 82 | + $this->logger = $logger; |
|
| 83 | + $this->appData = $appDataFactory->get('css'); |
|
| 84 | + $this->urlGenerator = $urlGenerator; |
|
| 85 | + $this->config = $config; |
|
| 86 | + $this->defaults = $defaults; |
|
| 87 | + $this->serverRoot = $serverRoot; |
|
| 88 | + $this->depsCache = $depsCache; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Process the caching process if needed |
|
| 93 | + * @param string $root Root path to the nextcloud installation |
|
| 94 | + * @param string $file |
|
| 95 | + * @param string $app The app name |
|
| 96 | + * @return boolean |
|
| 97 | + */ |
|
| 98 | + public function process($root, $file, $app) { |
|
| 99 | + $path = explode('/', $root . '/' . $file); |
|
| 100 | + |
|
| 101 | + $fileNameSCSS = array_pop($path); |
|
| 102 | + $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
|
| 103 | + |
|
| 104 | + $path = implode('/', $path); |
|
| 105 | + |
|
| 106 | + $webDir = substr($path, strlen($this->serverRoot)+1); |
|
| 107 | + |
|
| 108 | + try { |
|
| 109 | + $folder = $this->appData->getFolder($app); |
|
| 110 | + } catch(NotFoundException $e) { |
|
| 111 | + // creating css appdata folder |
|
| 112 | + $folder = $this->appData->newFolder($app); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | + return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param $appName |
|
| 124 | + * @param $fileName |
|
| 125 | + * @return ISimpleFile |
|
| 126 | + */ |
|
| 127 | + public function getCachedCSS($appName, $fileName) { |
|
| 128 | + $folder = $this->appData->getFolder($appName); |
|
| 129 | + return $folder->getFile($this->prependBaseurlPrefix($fileName)); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Check if the file is cached or not |
|
| 134 | + * @param string $fileNameCSS |
|
| 135 | + * @param ISimpleFolder $folder |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + private function isCached($fileNameCSS, ISimpleFolder $folder) { |
|
| 139 | + try { |
|
| 140 | + $cachedFile = $folder->getFile($fileNameCSS); |
|
| 141 | + if ($cachedFile->getSize() > 0) { |
|
| 142 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 143 | + $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 144 | + if ($deps === null) { |
|
| 145 | + $depFile = $folder->getFile($depFileName); |
|
| 146 | + $deps = $depFile->getContent(); |
|
| 147 | + //Set to memcache for next run |
|
| 148 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 149 | + } |
|
| 150 | + $deps = json_decode($deps, true); |
|
| 151 | + |
|
| 152 | + foreach ($deps as $file=>$mtime) { |
|
| 153 | + if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return true; |
|
| 159 | + } catch(NotFoundException $e) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Check if the variables file has changed |
|
| 166 | + * @return bool |
|
| 167 | + */ |
|
| 168 | + private function variablesChanged() { |
|
| 169 | + $injectedVariables = $this->getInjectedVariables(); |
|
| 170 | + if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 171 | + $this->resetCache(); |
|
| 172 | + $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 173 | + return true; |
|
| 174 | + } |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Cache the file with AppData |
|
| 180 | + * @param string $path |
|
| 181 | + * @param string $fileNameCSS |
|
| 182 | + * @param string $fileNameSCSS |
|
| 183 | + * @param ISimpleFolder $folder |
|
| 184 | + * @param string $webDir |
|
| 185 | + * @return boolean |
|
| 186 | + */ |
|
| 187 | + private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) { |
|
| 188 | + $scss = new Compiler(); |
|
| 189 | + $scss->setImportPaths([ |
|
| 190 | + $path, |
|
| 191 | + \OC::$SERVERROOT . '/core/css/', |
|
| 192 | + ]); |
|
| 193 | + // Continue after throw |
|
| 194 | + $scss->setIgnoreErrors(true); |
|
| 195 | + if($this->config->getSystemValue('debug')) { |
|
| 196 | + // Debug mode |
|
| 197 | + $scss->setFormatter(Expanded::class); |
|
| 198 | + $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 199 | + } else { |
|
| 200 | + // Compression |
|
| 201 | + $scss->setFormatter(Crunched::class); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + try { |
|
| 205 | + $cachedfile = $folder->getFile($fileNameCSS); |
|
| 206 | + } catch(NotFoundException $e) { |
|
| 207 | + $cachedfile = $folder->newFile($fileNameCSS); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 211 | + try { |
|
| 212 | + $depFile = $folder->getFile($depFileName); |
|
| 213 | + } catch (NotFoundException $e) { |
|
| 214 | + $depFile = $folder->newFile($depFileName); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Compile |
|
| 218 | + try { |
|
| 219 | + $compiledScss = $scss->compile( |
|
| 220 | + '@import "variables.scss";' . |
|
| 221 | + $this->getInjectedVariables() . |
|
| 222 | + '@import "'.$fileNameSCSS.'";'); |
|
| 223 | + } catch(ParserException $e) { |
|
| 224 | + $this->logger->error($e, ['app' => 'core']); |
|
| 225 | + return false; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // Gzip file |
|
| 229 | + try { |
|
| 230 | + $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 231 | + } catch (NotFoundException $e) { |
|
| 232 | + $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + try { |
|
| 236 | + $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 237 | + $cachedfile->putContent($data); |
|
| 238 | + $deps = json_encode($scss->getParsedFiles()); |
|
| 239 | + $depFile->putContent($deps); |
|
| 240 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 241 | + $gzipFile->putContent(gzencode($data, 9)); |
|
| 242 | + $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 243 | + return true; |
|
| 244 | + } catch(NotPermittedException $e) { |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Reset scss cache by deleting all generated css files |
|
| 251 | + * We need to regenerate all files when variables change |
|
| 252 | + */ |
|
| 253 | + private function resetCache() { |
|
| 254 | + $appDirectory = $this->appData->getDirectoryListing(); |
|
| 255 | + if(empty($appDirectory)){ |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + foreach ($appDirectory as $folder) { |
|
| 259 | + foreach ($folder->getDirectoryListing() as $file) { |
|
| 260 | + if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") { |
|
| 261 | + $file->delete(); |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @return string SCSS code for variables from OC_Defaults |
|
| 269 | + */ |
|
| 270 | + private function getInjectedVariables() { |
|
| 271 | + $variables = ''; |
|
| 272 | + foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 273 | + $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 274 | + } |
|
| 275 | + return $variables; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Add the correct uri prefix to make uri valid again |
|
| 280 | + * @param string $css |
|
| 281 | + * @param string $webDir |
|
| 282 | + * @return string |
|
| 283 | + */ |
|
| 284 | + private function rebaseUrls($css, $webDir) { |
|
| 285 | + $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; |
|
| 286 | + // OC\Route\Router:75 |
|
| 287 | + if(($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 288 | + $subst = 'url(\'../../'.$webDir.'/$1\')'; |
|
| 289 | + } else { |
|
| 290 | + $subst = 'url(\'../../../'.$webDir.'/$1\')'; |
|
| 291 | + } |
|
| 292 | + return preg_replace($re, $subst, $css); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Return the cached css file uri |
|
| 297 | + * @param string $appName the app name |
|
| 298 | + * @param string $fileName |
|
| 299 | + * @return string |
|
| 300 | + */ |
|
| 301 | + public function getCachedSCSS($appName, $fileName) { |
|
| 302 | + $tmpfileLoc = explode('/', $fileName); |
|
| 303 | + $fileName = array_pop($tmpfileLoc); |
|
| 304 | + $fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)); |
|
| 305 | + |
|
| 306 | + return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Prepend hashed base url to the css file |
|
| 311 | + * @param $cssFile |
|
| 312 | + * @return string |
|
| 313 | + */ |
|
| 314 | + private function prependBaseurlPrefix($cssFile) { |
|
| 315 | + $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 316 | + return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 317 | + } |
|
| 318 | 318 | } |
@@ -96,24 +96,24 @@ discard block |
||
| 96 | 96 | * @return boolean |
| 97 | 97 | */ |
| 98 | 98 | public function process($root, $file, $app) { |
| 99 | - $path = explode('/', $root . '/' . $file); |
|
| 99 | + $path = explode('/', $root.'/'.$file); |
|
| 100 | 100 | |
| 101 | 101 | $fileNameSCSS = array_pop($path); |
| 102 | 102 | $fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)); |
| 103 | 103 | |
| 104 | 104 | $path = implode('/', $path); |
| 105 | 105 | |
| 106 | - $webDir = substr($path, strlen($this->serverRoot)+1); |
|
| 106 | + $webDir = substr($path, strlen($this->serverRoot) + 1); |
|
| 107 | 107 | |
| 108 | 108 | try { |
| 109 | 109 | $folder = $this->appData->getFolder($app); |
| 110 | - } catch(NotFoundException $e) { |
|
| 110 | + } catch (NotFoundException $e) { |
|
| 111 | 111 | // creating css appdata folder |
| 112 | 112 | $folder = $this->appData->newFolder($app); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | |
| 116 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 116 | + if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 117 | 117 | return true; |
| 118 | 118 | } |
| 119 | 119 | return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
@@ -139,13 +139,13 @@ discard block |
||
| 139 | 139 | try { |
| 140 | 140 | $cachedFile = $folder->getFile($fileNameCSS); |
| 141 | 141 | if ($cachedFile->getSize() > 0) { |
| 142 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 143 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 142 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 143 | + $deps = $this->depsCache->get($folder->getName().'-'.$depFileName); |
|
| 144 | 144 | if ($deps === null) { |
| 145 | 145 | $depFile = $folder->getFile($depFileName); |
| 146 | 146 | $deps = $depFile->getContent(); |
| 147 | 147 | //Set to memcache for next run |
| 148 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 148 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 149 | 149 | } |
| 150 | 150 | $deps = json_decode($deps, true); |
| 151 | 151 | |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | return true; |
| 159 | - } catch(NotFoundException $e) { |
|
| 159 | + } catch (NotFoundException $e) { |
|
| 160 | 160 | return false; |
| 161 | 161 | } |
| 162 | 162 | } |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | private function variablesChanged() { |
| 169 | 169 | $injectedVariables = $this->getInjectedVariables(); |
| 170 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 170 | + if ($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 171 | 171 | $this->resetCache(); |
| 172 | 172 | $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
| 173 | 173 | return true; |
@@ -188,11 +188,11 @@ discard block |
||
| 188 | 188 | $scss = new Compiler(); |
| 189 | 189 | $scss->setImportPaths([ |
| 190 | 190 | $path, |
| 191 | - \OC::$SERVERROOT . '/core/css/', |
|
| 191 | + \OC::$SERVERROOT.'/core/css/', |
|
| 192 | 192 | ]); |
| 193 | 193 | // Continue after throw |
| 194 | 194 | $scss->setIgnoreErrors(true); |
| 195 | - if($this->config->getSystemValue('debug')) { |
|
| 195 | + if ($this->config->getSystemValue('debug')) { |
|
| 196 | 196 | // Debug mode |
| 197 | 197 | $scss->setFormatter(Expanded::class); |
| 198 | 198 | $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
@@ -203,11 +203,11 @@ discard block |
||
| 203 | 203 | |
| 204 | 204 | try { |
| 205 | 205 | $cachedfile = $folder->getFile($fileNameCSS); |
| 206 | - } catch(NotFoundException $e) { |
|
| 206 | + } catch (NotFoundException $e) { |
|
| 207 | 207 | $cachedfile = $folder->newFile($fileNameCSS); |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 210 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 211 | 211 | try { |
| 212 | 212 | $depFile = $folder->getFile($depFileName); |
| 213 | 213 | } catch (NotFoundException $e) { |
@@ -217,19 +217,19 @@ discard block |
||
| 217 | 217 | // Compile |
| 218 | 218 | try { |
| 219 | 219 | $compiledScss = $scss->compile( |
| 220 | - '@import "variables.scss";' . |
|
| 221 | - $this->getInjectedVariables() . |
|
| 220 | + '@import "variables.scss";'. |
|
| 221 | + $this->getInjectedVariables(). |
|
| 222 | 222 | '@import "'.$fileNameSCSS.'";'); |
| 223 | - } catch(ParserException $e) { |
|
| 223 | + } catch (ParserException $e) { |
|
| 224 | 224 | $this->logger->error($e, ['app' => 'core']); |
| 225 | 225 | return false; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | // Gzip file |
| 229 | 229 | try { |
| 230 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 230 | + $gzipFile = $folder->getFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 231 | 231 | } catch (NotFoundException $e) { |
| 232 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 232 | + $gzipFile = $folder->newFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | try { |
@@ -237,11 +237,11 @@ discard block |
||
| 237 | 237 | $cachedfile->putContent($data); |
| 238 | 238 | $deps = json_encode($scss->getParsedFiles()); |
| 239 | 239 | $depFile->putContent($deps); |
| 240 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 240 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 241 | 241 | $gzipFile->putContent(gzencode($data, 9)); |
| 242 | 242 | $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
| 243 | 243 | return true; |
| 244 | - } catch(NotPermittedException $e) { |
|
| 244 | + } catch (NotPermittedException $e) { |
|
| 245 | 245 | return false; |
| 246 | 246 | } |
| 247 | 247 | } |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | private function resetCache() { |
| 254 | 254 | $appDirectory = $this->appData->getDirectoryListing(); |
| 255 | - if(empty($appDirectory)){ |
|
| 255 | + if (empty($appDirectory)) { |
|
| 256 | 256 | return; |
| 257 | 257 | } |
| 258 | 258 | foreach ($appDirectory as $folder) { |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | private function getInjectedVariables() { |
| 271 | 271 | $variables = ''; |
| 272 | 272 | foreach ($this->defaults->getScssVariables() as $key => $value) { |
| 273 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 273 | + $variables .= '$'.$key.': '.$value.';'; |
|
| 274 | 274 | } |
| 275 | 275 | return $variables; |
| 276 | 276 | } |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | private function rebaseUrls($css, $webDir) { |
| 285 | 285 | $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; |
| 286 | 286 | // OC\Route\Router:75 |
| 287 | - if(($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 287 | + if (($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) { |
|
| 288 | 288 | $subst = 'url(\'../../'.$webDir.'/$1\')'; |
| 289 | 289 | } else { |
| 290 | 290 | $subst = 'url(\'../../../'.$webDir.'/$1\')'; |
@@ -313,6 +313,6 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | private function prependBaseurlPrefix($cssFile) { |
| 315 | 315 | $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
| 316 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile; |
|
| 316 | + return substr(md5($this->urlGenerator->getBaseUrl().$frontendController), 0, 8).'-'.$cssFile; |
|
| 317 | 317 | } |
| 318 | 318 | } |