diff options
author | Julius Haertl <jus@bitgrid.net> | 2017-02-17 16:42:07 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-04-25 11:39:45 +0200 |
commit | 68a63ad3f33c683d726219a508bb31de7b9ab1d0 (patch) | |
tree | 910932971b761689c8ca843860119a1df6e29573 /lib/private/Template | |
parent | 133f3fdc9aec28383dba323d58569eddd160b0df (diff) | |
download | nextcloud-server-68a63ad3f33c683d726219a508bb31de7b9ab1d0.tar.gz nextcloud-server-68a63ad3f33c683d726219a508bb31de7b9ab1d0.zip |
Implement scss variable injection by OC_Defaults
Signed-off-by: Julius Haertl <jus@bitgrid.net>
Add Scss variables to example theme and theming app
Signed-off-by: Julius Haertl <jus@bitgrid.net>
Use SCSSCacher to build theming css
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Update theming.scss
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Code cleanup
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Fix tests
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Inject SCSSCacher for easier testing
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Fix typehint
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Generate absolute URLs
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Fix tests to always use absolute urls for theming images
Signed-off-by: Julius Härtl <jus@bitgrid.net>
MailheaderColor -> ColorPrimary
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib/private/Template')
-rw-r--r-- | lib/private/Template/SCSSCacher.php | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/lib/private/Template/SCSSCacher.php b/lib/private/Template/SCSSCacher.php index 7021eae8b0e..c31a255e441 100644 --- a/lib/private/Template/SCSSCacher.php +++ b/lib/private/Template/SCSSCacher.php @@ -25,9 +25,11 @@ use Leafo\ScssPhp\Compiler; use Leafo\ScssPhp\Exception\ParserException; use Leafo\ScssPhp\Formatter\Crunched; use Leafo\ScssPhp\Formatter\Expanded; +use OC\Files\AppData\Factory; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\ICache; use OCP\IConfig; @@ -56,22 +58,25 @@ class SCSSCacher { /** * @param ILogger $logger - * @param IAppData $appData + * @param Factory $appDataFactory * @param IURLGenerator $urlGenerator * @param IConfig $config + * @param \OC_Defaults $defaults * @param string $serverRoot * @param ICache $depsCache */ public function __construct(ILogger $logger, - IAppData $appData, + Factory $appDataFactory, IURLGenerator $urlGenerator, IConfig $config, + \OC_Defaults $defaults, $serverRoot, ICache $depsCache) { $this->logger = $logger; - $this->appData = $appData; + $this->appData = $appDataFactory->get('css'); $this->urlGenerator = $urlGenerator; $this->config = $config; + $this->defaults = $defaults; $this->serverRoot = $serverRoot; $this->depsCache = $depsCache; } @@ -100,13 +105,24 @@ class SCSSCacher { $folder = $this->appData->newFolder($app); } - if($this->isCached($fileNameCSS, $folder)) { + + if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { return true; } return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); } /** + * @param $appName + * @param $fileName + * @return ISimpleFile + */ + public function getCachedCSS($appName, $fileName) { + $folder = $this->appData->getFolder($appName); + return $folder->getFile($fileName); + } + + /** * Check if the file is cached or not * @param string $fileNameCSS * @param ISimpleFolder $folder @@ -139,6 +155,20 @@ class SCSSCacher { } /** + * Check if the variables file has changed + * @return bool + */ + private function variablesChanged() { + $injectedVariables = $this->getInjectedVariables(); + if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { + $this->resetCache(); + $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); + return true; + } + return false; + } + + /** * Cache the file with AppData * @param string $path * @param string $fileNameCSS @@ -179,6 +209,7 @@ class SCSSCacher { try { $compiledScss = $scss->compile( '@import "variables.scss";' . + $this->getInjectedVariables() . '@import "'.$fileNameSCSS.'";'); } catch(ParserException $e) { $this->logger->error($e, ['app' => 'core']); @@ -205,6 +236,35 @@ class SCSSCacher { } /** + * Reset scss cache by deleting all generated css files + * We need to regenerate all files when variables change + */ + private function resetCache() { + $appDirectory = $this->appData->getDirectoryListing(); + if(empty($appDirectory)){ + return; + } + foreach ($appDirectory as $folder) { + foreach ($folder->getDirectoryListing() as $file) { + if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") { + $file->delete(); + } + } + } + } + + /** + * @return string SCSS code for variables from OC_Defaults + */ + private function getInjectedVariables() { + $variables = ''; + foreach ($this->defaults->getScssVariables() as $key => $value) { + $variables .= '$' . $key . ': ' . $value . ';'; + } + return $variables; + } + + /** * Add the correct uri prefix to make uri valid again * @param string $css * @param string $webDir |