diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-04-25 10:56:13 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-25 10:56:13 -0300 |
commit | 5a9224fb4c736fbd07e0dd90058c2e87cbebbe16 (patch) | |
tree | fc9f6e09b6b7924051312d6b369412fc2a9fe85a /lib | |
parent | bcf587542c0777f9b12a96dcecda6c68db647177 (diff) | |
parent | 6448cf5748cfb9a9795b0f0f1be74839160fce48 (diff) | |
download | nextcloud-server-5a9224fb4c736fbd07e0dd90058c2e87cbebbe16.tar.gz nextcloud-server-5a9224fb4c736fbd07e0dd90058c2e87cbebbe16.zip |
Merge pull request #3531 from nextcloud/theming-scss
Theming using SCSS variables
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Server.php | 19 | ||||
-rw-r--r-- | lib/private/Template/SCSSCacher.php | 68 | ||||
-rw-r--r-- | lib/private/TemplateLayout.php | 19 | ||||
-rw-r--r-- | lib/private/legacy/defaults.php | 10 |
4 files changed, 99 insertions, 17 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..d7249a44293 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' ); @@ -196,7 +197,9 @@ class TemplateLayout extends \OC_Template { // allows chrome workspace mapping in debug mode return ""; } - + if ($this->config->getSystemValue('installed', false) && \OC::$server->getAppManager()->isInstalled('theming')) { + return '?v=' . self::$versionHash . '-' . $this->config->getAppValue('theming', 'cachebuster', '0'); + } return '?v=' . self::$versionHash; } @@ -209,16 +212,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 +222,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; } |