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 | |
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')
-rw-r--r-- | lib/private/Server.php | 19 | ||||
-rw-r--r-- | lib/private/Template/SCSSCacher.php | 68 | ||||
-rw-r--r-- | lib/private/TemplateLayout.php | 15 | ||||
-rw-r--r-- | lib/private/legacy/defaults.php | 10 |
4 files changed, 96 insertions, 16 deletions
diff --git a/lib/private/Server.php b/lib/private/Server.php index 62c17ced90b..f40a59ad334 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -95,9 +95,12 @@ use OC\Security\TrustedDomainHelper; use OC\Session\CryptoWrapper; use OC\Share20\ShareHelper; use OC\Tagging\TagMapper; +use OC\Template\SCSSCacher; use OCA\Theming\ThemingDefaults; + use OCP\App\IAppManager; use OCP\Defaults; +use OCA\Theming\Util; use OCP\Federation\ICloudIdManager; use OCP\Authentication\LoginCredentials\IStore; use OCP\ICacheFactory; @@ -849,11 +852,25 @@ class Server extends ServerContainer implements IServerContainer { $c->getURLGenerator(), new \OC_Defaults(), $c->getAppDataDir('theming'), - $c->getMemCacheFactory() + $c->getMemCacheFactory(), + new Util($c->getConfig(), $this->getRootFolder(), $this->getAppManager()) ); } return new \OC_Defaults(); }); + $this->registerService(SCSSCacher::class, function(Server $c) { + /** @var Factory $cacheFactory */ + $cacheFactory = $c->query(Factory::class); + return new SCSSCacher( + $c->getLogger(), + $c->query(\OC\Files\AppData\Factory::class), + $c->getURLGenerator(), + $c->getConfig(), + $c->getThemingDefaults(), + \OC::$SERVERROOT, + $cacheFactory->createLocal('SCSS') + ); + }); $this->registerService(EventDispatcher::class, function () { return new EventDispatcher(); }); 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 diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index 7a5984a4924..64d21186f69 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -58,6 +58,7 @@ class TemplateLayout extends \OC_Template { // yes - should be injected .... $this->config = \OC::$server->getConfig(); + // Decide which page we show if($renderAs == 'user') { parent::__construct( 'core', 'layout.user' ); @@ -209,16 +210,7 @@ class TemplateLayout extends \OC_Template { $theme = \OC_Util::getTheme(); if($compileScss) { - /** @var \OC\Memcache\Factory $cache */ - $cache = \OC::$server->query('MemCacheFactory'); - $SCSSCacher = new SCSSCacher( - \OC::$server->getLogger(), - \OC::$server->getAppDataDir('css'), - \OC::$server->getURLGenerator(), - \OC::$server->getConfig(), - \OC::$SERVERROOT, - $cache->createLocal('SCSS') - ); + $SCSSCacher = \OC::$server->query(SCSSCacher::class); } else { $SCSSCacher = null; } @@ -228,7 +220,8 @@ class TemplateLayout extends \OC_Template { $theme, array( \OC::$SERVERROOT => \OC::$WEBROOT ), array( \OC::$SERVERROOT => \OC::$WEBROOT ), - $SCSSCacher); + $SCSSCacher + ); $locator->find($styles); return $locator->getResources(); } diff --git a/lib/private/legacy/defaults.php b/lib/private/legacy/defaults.php index 72e25e5e647..7835707b19d 100644 --- a/lib/private/legacy/defaults.php +++ b/lib/private/legacy/defaults.php @@ -290,6 +290,16 @@ class OC_Defaults { return $this->defaultColorPrimary; } + /** + * @return array scss variables to overwrite + */ + public function getScssVariables() { + if($this->themeExist('getScssVariables')) { + return $this->theme->getScssVariables(); + } + return []; + } + public function shouldReplaceIcons() { return false; } |