2016-11-08 15:36:35 +03: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 18:30:04 +03:00
|
|
|
use Leafo\ScssPhp\Formatter\Crunched;
|
|
|
|
use Leafo\ScssPhp\Formatter\Expanded;
|
|
|
|
use OC\SystemConfig;
|
|
|
|
use OCP\Files\IAppData;
|
2016-11-09 13:18:43 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-11-30 23:51:09 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2016-11-30 18:30:04 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
2016-11-08 15:36:35 +03:00
|
|
|
|
|
|
|
class SCSSCacher {
|
|
|
|
|
2016-11-30 18:30:04 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
|
|
|
/** @var SystemConfig */
|
|
|
|
protected $systemConfig;
|
2016-11-08 15:36:35 +03:00
|
|
|
|
|
|
|
/**
|
2016-11-30 18:30:04 +03:00
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IAppData $appData
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param SystemConfig $systemConfig
|
2016-11-08 15:36:35 +03:00
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
|
2016-11-08 15:36:35 +03:00
|
|
|
$this->logger = $logger;
|
2016-11-09 13:18:43 +03:00
|
|
|
$this->appData = $appData;
|
2016-11-10 18:16:33 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->systemConfig = $systemConfig;
|
2016-11-30 23:51:09 +03:00
|
|
|
}
|
2016-11-10 18:16:33 +03:00
|
|
|
|
2016-11-30 23:51:09 +03: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 15:36:35 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
$fileNameSCSS = array_pop($path);
|
|
|
|
$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
|
2016-11-09 15:39:08 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
$path = implode('/', $path);
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
$webDir = explode('/', $file);
|
|
|
|
array_pop($webDir);
|
|
|
|
$webDir = implode('/', $webDir);
|
2016-11-09 13:18:43 +03:00
|
|
|
|
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$folder = $this->appData->getFolder('core');
|
2016-11-09 13:18:43 +03:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
// creating css appdata folder
|
2016-11-30 23:51:09 +03:00
|
|
|
$folder = $this->appData->newFolder('core');
|
2016-11-09 13:18:43 +03:00
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2017-01-02 19:05:58 +03:00
|
|
|
if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path)) {
|
2016-11-08 15:36:35 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
2016-11-30 23:51:09 +03:00
|
|
|
return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Check if the file is cached or not
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param string $fileNameSCSS
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $path
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-01-02 19:05:58 +03:00
|
|
|
private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
|
2016-11-09 13:18:43 +03:00
|
|
|
try{
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedFile = $folder->getFile($fileNameCSS);
|
2016-12-01 00:10:42 +03:00
|
|
|
if( $cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
|
2016-11-30 23:51:09 +03:00
|
|
|
&& $cachedFile->getSize() > 0 ) {
|
2016-11-09 13:18:43 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-10 18:36:58 +03:00
|
|
|
return false;
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Cache the file with AppData
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $path
|
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param string $fileNameSCSS
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $webDir
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
|
2016-11-08 15:36:35 +03:00
|
|
|
$scss = new Compiler();
|
2016-11-30 23:51:09 +03:00
|
|
|
$scss->setImportPaths($path);
|
2016-11-10 18:16:33 +03:00
|
|
|
if($this->systemConfig->getValue('debug')) {
|
2016-11-08 15:36:35 +03:00
|
|
|
// Debug mode
|
2016-11-30 18:30:04 +03:00
|
|
|
$scss->setFormatter(Expanded::class);
|
2016-11-08 15:36:35 +03:00
|
|
|
$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
|
|
|
|
} else {
|
2016-11-09 13:18:43 +03:00
|
|
|
// Compression
|
2016-11-30 18:30:04 +03:00
|
|
|
$scss->setFormatter(Crunched::class);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedfile = $folder->getFile($fileNameCSS);
|
2016-11-09 13:18:43 +03:00
|
|
|
} catch(NotFoundException $e) {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedfile = $folder->newFile($fileNameCSS);
|
2016-11-09 13:18:43 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
// Compile
|
2016-11-09 13:18:43 +03:00
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$compiledScss = $scss->compile('@import "'.$fileNameSCSS.'";');
|
2016-11-08 15:36:35 +03:00
|
|
|
} catch(ParserException $e) {
|
2016-11-10 18:16:33 +03:00
|
|
|
$this->logger->error($e, ['app' => 'core']);
|
2016-11-08 15:36:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-08 23:45:43 +03:00
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
|
|
|
|
$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
|
2016-11-08 23:45:43 +03:00
|
|
|
return true;
|
2016-11-09 15:39:08 +03:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
2016-11-08 23:45:43 +03:00
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Add the correct uri prefix to make uri valid again
|
|
|
|
* @param string $css
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $webDir
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
private function rebaseUrls($css, $webDir) {
|
2016-11-10 18:20:50 +03:00
|
|
|
$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
|
2017-01-05 22:29:03 +03:00
|
|
|
// OC\Route\Router:75
|
|
|
|
if(($this->systemConfig->getValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
|
|
|
|
$subst = 'url(\'../../'.$webDir.'/$1\')';
|
|
|
|
} else {
|
|
|
|
$subst = 'url(\'../../../'.$webDir.'/$1\')';
|
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
return preg_replace($re, $subst, $css);
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Return the cached css file uri
|
2016-11-10 18:36:58 +03:00
|
|
|
* @param string $appName the app name
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $fileName
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
public function getCachedSCSS($appName, $fileName) {
|
2016-12-22 17:41:13 +03:00
|
|
|
$tmpfileLoc = explode('/', $fileName);
|
|
|
|
$fileName = array_pop($tmpfileLoc);
|
2016-11-30 23:51:09 +03:00
|
|
|
$fileName = str_replace('.scss', '.css', $fileName);
|
|
|
|
|
2017-01-02 18:59:36 +03:00
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
}
|