2016-11-08 12:36:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Template;
|
|
|
|
|
|
|
|
use Leafo\ScssPhp\Compiler;
|
|
|
|
use Leafo\ScssPhp\Exception\ParserException;
|
2016-11-30 15:30:04 +00:00
|
|
|
use Leafo\ScssPhp\Formatter\Crunched;
|
|
|
|
use Leafo\ScssPhp\Formatter\Expanded;
|
|
|
|
use OC\SystemConfig;
|
|
|
|
use OCP\Files\IAppData;
|
2016-11-09 10:18:43 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-11-30 20:51:09 +00:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2016-11-30 15:30:04 +00:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
2016-11-08 12:36:35 +00:00
|
|
|
|
|
|
|
class SCSSCacher {
|
|
|
|
|
2016-11-30 15:30:04 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
|
|
|
/** @var SystemConfig */
|
|
|
|
protected $systemConfig;
|
2016-11-08 12:36:35 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-30 15:30:04 +00:00
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IAppData $appData
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param SystemConfig $systemConfig
|
2016-11-08 12:36:35 +00:00
|
|
|
*/
|
2016-11-30 20:51:09 +00:00
|
|
|
public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
|
2016-11-08 12:36:35 +00:00
|
|
|
$this->logger = $logger;
|
2016-11-09 10:18:43 +00:00
|
|
|
$this->appData = $appData;
|
2016-11-10 15:16:33 +00:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->systemConfig = $systemConfig;
|
2016-11-30 20:51:09 +00:00
|
|
|
}
|
2016-11-10 15:16:33 +00:00
|
|
|
|
2016-11-30 20:51:09 +00:00
|
|
|
/**
|
|
|
|
* Process the caching process if needed
|
|
|
|
* @param string $root Root path to the nextcloud installation
|
|
|
|
* @param string $file
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function process($root, $file) {
|
|
|
|
$path = explode('/', $root . '/' . $file);
|
2016-11-08 12:36:35 +00:00
|
|
|
|
2016-11-30 20:51:09 +00:00
|
|
|
$fileNameSCSS = array_pop($path);
|
|
|
|
$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
|
2016-11-09 12:39:08 +00:00
|
|
|
|
2016-11-30 20:51:09 +00:00
|
|
|
$path = implode('/', $path);
|
2016-11-08 12:36:35 +00:00
|
|
|
|
2016-11-30 20:51:09 +00:00
|
|
|
$webDir = explode('/', $file);
|
|
|
|
array_pop($webDir);
|
|
|
|
$webDir = implode('/', $webDir);
|
2016-11-09 10:18:43 +00:00
|
|
|
|
|
|
|
try {
|
2016-11-30 20:51:09 +00:00
|
|
|
$folder = $this->appData->getFolder('core');
|
2016-11-09 10:18:43 +00:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
// creating css appdata folder
|
2016-11-30 20:51:09 +00:00
|
|
|
$folder = $this->appData->newFolder('core');
|
2016-11-09 10:18:43 +00:00
|
|
|
}
|
2016-11-08 12:36:35 +00:00
|
|
|
|
2016-11-30 20:51:09 +00:00
|
|
|
if($this->is_cached($fileNameCSS, $fileNameSCSS, $folder, $path)) {
|
2016-11-08 12:36:35 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2016-11-30 20:51:09 +00:00
|
|
|
return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Check if the file is cached or not
|
2016-11-30 20:51:09 +00:00
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param string $fileNameSCSS
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $path
|
2016-11-09 12:39:08 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-30 20:51:09 +00:00
|
|
|
private function is_cached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
|
2016-11-09 10:18:43 +00:00
|
|
|
try{
|
2016-11-30 20:51:09 +00:00
|
|
|
$cachedFile = $folder->getFile($fileNameCSS);
|
2016-11-30 21:10:42 +00:00
|
|
|
if( $cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
|
2016-11-30 20:51:09 +00:00
|
|
|
&& $cachedFile->getSize() > 0 ) {
|
2016-11-09 10:18:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-10 15:36:58 +00:00
|
|
|
return false;
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Cache the file with AppData
|
2016-11-30 20:51:09 +00:00
|
|
|
* @param string $path
|
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param string $fileNameSCSS
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $webDir
|
2016-11-09 12:39:08 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-30 20:51:09 +00:00
|
|
|
private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
|
2016-11-08 12:36:35 +00:00
|
|
|
$scss = new Compiler();
|
2016-11-30 20:51:09 +00:00
|
|
|
$scss->setImportPaths($path);
|
2016-11-10 15:16:33 +00:00
|
|
|
if($this->systemConfig->getValue('debug')) {
|
2016-11-08 12:36:35 +00:00
|
|
|
// Debug mode
|
2016-11-30 15:30:04 +00:00
|
|
|
$scss->setFormatter(Expanded::class);
|
2016-11-08 12:36:35 +00:00
|
|
|
$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
|
|
|
|
} else {
|
2016-11-09 10:18:43 +00:00
|
|
|
// Compression
|
2016-11-30 15:30:04 +00:00
|
|
|
$scss->setFormatter(Crunched::class);
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-11-30 20:51:09 +00:00
|
|
|
$cachedfile = $folder->getFile($fileNameCSS);
|
2016-11-09 10:18:43 +00:00
|
|
|
} catch(NotFoundException $e) {
|
2016-11-30 20:51:09 +00:00
|
|
|
$cachedfile = $folder->newFile($fileNameCSS);
|
2016-11-09 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
// Compile
|
2016-11-09 10:18:43 +00:00
|
|
|
try {
|
2016-11-30 20:51:09 +00:00
|
|
|
$compiledScss = $scss->compile('@import "'.$fileNameSCSS.'";');
|
2016-11-08 12:36:35 +00:00
|
|
|
} catch(ParserException $e) {
|
2016-11-10 15:16:33 +00:00
|
|
|
$this->logger->error($e, ['app' => 'core']);
|
2016-11-08 12:36:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-08 20:45:43 +00:00
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
try {
|
2016-11-30 20:51:09 +00:00
|
|
|
$cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
|
|
|
|
$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
|
2016-11-08 20:45:43 +00:00
|
|
|
return true;
|
2016-11-09 12:39:08 +00:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
2016-11-08 20:45:43 +00:00
|
|
|
}
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Add the correct uri prefix to make uri valid again
|
|
|
|
* @param string $css
|
2016-11-30 20:51:09 +00:00
|
|
|
* @param string $webDir
|
2016-11-09 12:39:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 20:51:09 +00:00
|
|
|
private function rebaseUrls($css, $webDir) {
|
2016-11-10 15:20:50 +00:00
|
|
|
$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
|
2016-11-30 20:51:09 +00:00
|
|
|
$subst = 'url(\'../../../'.$webDir.'/$1\')';
|
2016-11-08 12:36:35 +00:00
|
|
|
return preg_replace($re, $subst, $css);
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Return the cached css file uri
|
2016-11-10 15:36:58 +00:00
|
|
|
* @param string $appName the app name
|
2016-11-30 20:51:09 +00:00
|
|
|
* @param string $fileName
|
2016-11-09 12:39:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 20:51:09 +00:00
|
|
|
public function getCachedSCSS($appName, $fileName) {
|
2016-12-22 14:41:13 +00:00
|
|
|
$tmpfileLoc = explode('/', $fileName);
|
|
|
|
$fileName = array_pop($tmpfileLoc);
|
2016-11-30 20:51:09 +00:00
|
|
|
$fileName = str_replace('.scss', '.css', $fileName);
|
|
|
|
|
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), 1);
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
}
|