Make SCCCacher injectable

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-11-30 21:51:09 +01:00
parent d9eaed5ffa
commit d64d661ef5
No known key found for this signature in database
GPG Key ID: F941078878347C0C
2 changed files with 59 additions and 72 deletions

View File

@ -91,24 +91,22 @@ class CSSResourceLocator extends ResourceLocator {
* *
* @param string $root path to check * @param string $root path to check
* @param string $file the filename * @param string $file the filename
* @param \OCP\Files\IAppData $appData * @param IAppData $appData
* @param \OCP\IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param \OC\SystemConfig $systemConfig * @param SystemConfig $systemConfig
* @param string|null $webRoot base for path, default map $root to $webRoot * @param string|null $webRoot base for path, default map $root to $webRoot
* @return bool True if the resource was found and cached, false otherwise * @return bool True if the resource was found and cached, false otherwise
*/ */
protected function cacheAndAppendScssIfExist($root, $file, $appData, $urlGenerator, $systemConfig, $webRoot = null) { protected function cacheAndAppendScssIfExist($root, $file, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig, $webRoot = null) {
if (is_file($root.'/'.$file)) { if (is_file($root.'/'.$file)) {
$scssCache = new SCSSCacher( $scssCache = new SCSSCacher(
$this->logger, $this->logger,
$root,
$file,
$appData, $appData,
$urlGenerator, $urlGenerator,
$systemConfig); $systemConfig);
if($scssCache->process()) { if($scssCache->process($root, $file)) {
$this->append($root, $scssCache->getCachedSCSS('core'), $webRoot, false); $this->append($root, $scssCache->getCachedSCSS('core', $file), $webRoot, false);
return true; return true;
} else { } else {
$this->logger->error('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']); $this->logger->error('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']);

View File

@ -25,36 +25,15 @@ use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Exception\ParserException; use Leafo\ScssPhp\Exception\ParserException;
use Leafo\ScssPhp\Formatter\Crunched; use Leafo\ScssPhp\Formatter\Crunched;
use Leafo\ScssPhp\Formatter\Expanded; use Leafo\ScssPhp\Formatter\Expanded;
use OC\Files\SimpleFS\SimpleFolder;
use OC\SystemConfig; use OC\SystemConfig;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
class SCSSCacher { class SCSSCacher {
/** @var string The root path to the nextcloud installation */
protected $root;
/** @var array The exploded absolute path to the file */
protected $file;
/** @var string The scss filename with extension */
protected $fileNameSCSS;
/** @var string The css filename with extension */
protected $fileNameCSS;
/** @var string Absolute path to scss file location folder */
protected $fileLoc;
/** @var string Path to scss file from the root installation */
protected $rootCssLoc;
/** @var SimpleFolder The folder we're putting our compiled css files */
protected $folder;
/** @var ILogger */ /** @var ILogger */
protected $logger; protected $logger;
@ -69,62 +48,62 @@ class SCSSCacher {
/** /**
* @param ILogger $logger * @param ILogger $logger
* @param string $root Root path to the nextcloud installation
* @param string $file
* @param IAppData $appData * @param IAppData $appData
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param SystemConfig $systemConfig * @param SystemConfig $systemConfig
*/ */
public function __construct(ILogger $logger, $root, $file, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) { public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
$this->logger = $logger; $this->logger = $logger;
$this->appData = $appData; $this->appData = $appData;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->systemConfig = $systemConfig; $this->systemConfig = $systemConfig;
$this->root = $root;
$this->file = explode('/', $root.'/'.$file);
/* filenames */
$this->fileNameSCSS = array_pop($this->file);
$this->fileNameCSS = str_replace('.scss', '.css', $this->fileNameSCSS);
$this->fileLoc = implode('/', $this->file);
// base uri to css file
$this->rootCssLoc = explode('/', $file);
array_pop($this->rootCssLoc);
$this->rootCssLoc = implode('/', $this->rootCssLoc);
try {
$this->folder = $this->appData->getFolder('core');
} catch(NotFoundException $e) {
// creating css appdata folder
$this->folder = $this->appData->newFolder('core');
}
} }
/** /**
* Process the caching process if needed * Process the caching process if needed
* @param string $root Root path to the nextcloud installation
* @param string $file
* @return boolean * @return boolean
*/ */
public function process() { public function process($root, $file) {
$path = explode('/', $root . '/' . $file);
if($this->is_cached()) { $fileNameSCSS = array_pop($path);
$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
$path = implode('/', $path);
$webDir = explode('/', $file);
array_pop($webDir);
$webDir = implode('/', $webDir);
try {
$folder = $this->appData->getFolder('core');
} catch(NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder('core');
}
if($this->is_cached($fileNameCSS, $fileNameSCSS, $folder, $path)) {
return true; return true;
} else { } else {
return $this->cache(); return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
} }
} }
/** /**
* Check if the file is cached or not * Check if the file is cached or not
* @param string $fileNameCSS
* @param string $fileNameSCSS
* @param ISimpleFolder $folder
* @param string $path
* @return boolean * @return boolean
*/ */
private function is_cached() { private function is_cached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
try{ try{
$cachedfile = $this->folder->getFile($this->fileNameCSS); $cachedFile = $folder->getFile($fileNameCSS);
if( $cachedfile->getMTime() > filemtime($this->fileLoc.'/'.$this->fileNameSCSS) if( $cachedFile->getMTime() > filemtime($this->fileLoc.'/'.$fileNameSCSS)
&& $cachedfile->getSize() > 0 ) { && $cachedFile->getSize() > 0 ) {
return true; return true;
} }
} catch(NotFoundException $e) { } catch(NotFoundException $e) {
@ -135,11 +114,16 @@ class SCSSCacher {
/** /**
* Cache the file with AppData * Cache the file with AppData
* @param string $path
* @param string $fileNameCSS
* @param string $fileNameSCSS
* @param ISimpleFolder $folder
* @param string $webDir
* @return boolean * @return boolean
*/ */
private function cache() { private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
$scss = new Compiler(); $scss = new Compiler();
$scss->setImportPaths($this->fileLoc); $scss->setImportPaths($path);
if($this->systemConfig->getValue('debug')) { if($this->systemConfig->getValue('debug')) {
// Debug mode // Debug mode
$scss->setFormatter(Expanded::class); $scss->setFormatter(Expanded::class);
@ -150,22 +134,22 @@ class SCSSCacher {
} }
try { try {
$cachedfile = $this->folder->getFile($this->fileNameCSS); $cachedfile = $folder->getFile($fileNameCSS);
} catch(NotFoundException $e) { } catch(NotFoundException $e) {
$cachedfile = $this->folder->newFile($this->fileNameCSS); $cachedfile = $folder->newFile($fileNameCSS);
} }
// Compile // Compile
try { try {
$compiledScss = $scss->compile('@import "'.$this->fileNameSCSS.'";'); $compiledScss = $scss->compile('@import "'.$fileNameSCSS.'";');
} catch(ParserException $e) { } catch(ParserException $e) {
$this->logger->error($e, ['app' => 'core']); $this->logger->error($e, ['app' => 'core']);
return false; return false;
} }
try { try {
$cachedfile->putContent($this->rebaseUrls($compiledScss)); $cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
$this->logger->debug($this->rootCssLoc.'/'.$this->fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
return true; return true;
} catch(NotFoundException $e) { } catch(NotFoundException $e) {
return false; return false;
@ -175,20 +159,25 @@ class SCSSCacher {
/** /**
* Add the correct uri prefix to make uri valid again * Add the correct uri prefix to make uri valid again
* @param string $css * @param string $css
* @param string $webDir
* @return string * @return string
*/ */
private function rebaseUrls($css) { private function rebaseUrls($css, $webDir) {
$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x'; $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
$subst = 'url(\'../../../'.$this->rootCssLoc.'/$1\')'; $subst = 'url(\'../../../'.$webDir.'/$1\')';
return preg_replace($re, $subst, $css); return preg_replace($re, $subst, $css);
} }
/** /**
* Return the cached css file uri * Return the cached css file uri
* @param string $appName the app name * @param string $appName the app name
* @param string $fileName
* @return string * @return string
*/ */
public function getCachedSCSS($appName) { public function getCachedSCSS($appName, $fileName) {
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $this->fileNameCSS, 'appName' => $appName)), 1); $fileName = array_pop(explode('/', $fileName));
$fileName = str_replace('.scss', '.css', $fileName);
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), 1);
} }
} }