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-09 10:18:43 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-11-09 14:18:36 +00:00
|
|
|
use OCP\IURLGenerator;
|
2016-11-08 12:36:35 +00:00
|
|
|
|
|
|
|
class SCSSCacher {
|
|
|
|
|
|
|
|
protected $root;
|
2016-11-09 10:18:43 +00:00
|
|
|
protected $folder;
|
2016-11-08 12:36:35 +00:00
|
|
|
protected $file;
|
2016-11-09 12:39:08 +00:00
|
|
|
protected $fileNameSCSS;
|
|
|
|
protected $fileNameCSS;
|
2016-11-08 12:36:35 +00:00
|
|
|
protected $fileLoc;
|
|
|
|
protected $rootCssLoc;
|
|
|
|
|
|
|
|
/** @var \OCP\ILogger */
|
|
|
|
protected $logger;
|
2016-11-09 12:39:08 +00:00
|
|
|
/** @var \OCP\Files\IAppData */
|
2016-11-09 10:18:43 +00:00
|
|
|
protected $appData;
|
2016-11-10 15:16:33 +00:00
|
|
|
/** @var \OCP\IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
/** @var \OCP\Files\IConfig */
|
|
|
|
protected $systemConfig;
|
2016-11-08 12:36:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\ILogger $logger
|
|
|
|
* @param string $root
|
|
|
|
* @param string $file
|
2016-11-09 12:39:08 +00:00
|
|
|
* @param \OCP\Files\IAppData $appData
|
2016-11-08 12:36:35 +00:00
|
|
|
*/
|
2016-11-10 15:16:33 +00:00
|
|
|
public function __construct(\OCP\ILogger $logger, $root, $file, \OCP\Files\IAppData $appData, \OCP\IURLGenerator $urlGenerator, $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-08 12:36:35 +00:00
|
|
|
$this->root = $root;
|
|
|
|
$this->file = explode('/', $root.'/'.$file);
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/* filenames */
|
2016-11-09 10:18:43 +00:00
|
|
|
$this->fileNameSCSS = array_pop($this->file);
|
|
|
|
$this->fileNameCSS = str_replace('.scss', '.css', $this->fileNameSCSS);
|
2016-11-09 12:39:08 +00:00
|
|
|
|
2016-11-08 12:36:35 +00:00
|
|
|
$this->fileLoc = implode('/', $this->file);
|
|
|
|
|
|
|
|
// base uri to css file
|
|
|
|
$this->rootCssLoc = explode('/', $file);
|
|
|
|
array_pop($this->rootCssLoc);
|
|
|
|
$this->rootCssLoc = implode('/', $this->rootCssLoc);
|
2016-11-09 10:18:43 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->folder = $this->appData->getFolder('css');
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
// creating css appdata folder
|
|
|
|
$this->folder = $this->appData->newFolder('css');
|
|
|
|
}
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Process the caching process if needed
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-08 12:36:35 +00:00
|
|
|
public function process() {
|
|
|
|
|
2016-11-09 10:18:43 +00:00
|
|
|
if($this->is_cached()) {
|
2016-11-08 12:36:35 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $this->cache();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Check if the file is cached or not
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-09 10:18:43 +00:00
|
|
|
private function is_cached() {
|
|
|
|
try{
|
|
|
|
$cachedfile = $this->folder->getFile($this->fileNameCSS);
|
|
|
|
if( $cachedfile->getMTime() > filemtime($this->fileLoc.'/'.$this->fileNameSCSS)
|
|
|
|
&& $cachedfile->getSize() > 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-08 12:36:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
/**
|
|
|
|
* Cache the file with AppData
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-08 12:36:35 +00:00
|
|
|
private function cache() {
|
|
|
|
$scss = new Compiler();
|
|
|
|
$scss->setImportPaths($this->fileLoc);
|
2016-11-10 15:16:33 +00:00
|
|
|
if($this->systemConfig->getValue('debug')) {
|
2016-11-08 12:36:35 +00:00
|
|
|
// Debug mode
|
|
|
|
$scss->setFormatter('Leafo\ScssPhp\Formatter\Expanded');
|
|
|
|
$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
|
|
|
|
} else {
|
2016-11-09 10:18:43 +00:00
|
|
|
// Compression
|
2016-11-08 12:36:35 +00:00
|
|
|
$scss->setFormatter('Leafo\ScssPhp\Formatter\Crunched');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-11-09 10:18:43 +00:00
|
|
|
$cachedfile = $this->folder->getFile($this->fileNameCSS);
|
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
$cachedfile = $this->folder->newFile($this->fileNameCSS);
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:39:08 +00:00
|
|
|
// Compile
|
2016-11-09 10:18:43 +00:00
|
|
|
try {
|
|
|
|
$compiledScss = $scss->compile('@import "'.$this->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 {
|
|
|
|
$cachedfile->putContent($this->rebaseUrls($compiledScss));
|
2016-11-10 15:16:33 +00:00
|
|
|
$this->logger->debug($this->rootCssLoc.'/'.$this->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
|
|
|
}
|
|
|
|
return false;
|
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
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 12:36:35 +00:00
|
|
|
private function rebaseUrls($css) {
|
|
|
|
$re = '/url\([\'"](.*)[\'"]\)/x';
|
2016-11-09 12:39:08 +00:00
|
|
|
$subst = 'url(\'../../../'.$this->rootCssLoc.'/$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
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-08 12:36:35 +00:00
|
|
|
public function getCachedSCSS() {
|
2016-11-10 15:16:33 +00:00
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $this->fileNameCSS)), 1);
|
2016-11-08 12:36:35 +00:00
|
|
|
}
|
|
|
|
}
|