Add typehinting an fix some minor cleanup
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
parent
5ec7296f6d
commit
e4c58fc473
|
@ -57,6 +57,9 @@ class SCSSCacher {
|
||||||
/** @var IConfig */
|
/** @var IConfig */
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
/** @var \OC_Defaults */
|
||||||
|
private $defaults;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $serverRoot;
|
protected $serverRoot;
|
||||||
|
|
||||||
|
@ -64,7 +67,7 @@ class SCSSCacher {
|
||||||
protected $depsCache;
|
protected $depsCache;
|
||||||
|
|
||||||
/** @var null|string */
|
/** @var null|string */
|
||||||
protected $injectedVariables = null;
|
private $injectedVariables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ILogger $logger
|
* @param ILogger $logger
|
||||||
|
@ -93,12 +96,14 @@ class SCSSCacher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process the caching process if needed
|
* Process the caching process if needed
|
||||||
|
*
|
||||||
* @param string $root Root path to the nextcloud installation
|
* @param string $root Root path to the nextcloud installation
|
||||||
* @param string $file
|
* @param string $file
|
||||||
* @param string $app The app name
|
* @param string $app The app name
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
* @throws NotPermittedException
|
||||||
*/
|
*/
|
||||||
public function process($root, $file, $app) {
|
public function process(string $root, string $file, string $app): bool {
|
||||||
$path = explode('/', $root . '/' . $file);
|
$path = explode('/', $root . '/' . $file);
|
||||||
|
|
||||||
$fileNameSCSS = array_pop($path);
|
$fileNameSCSS = array_pop($path);
|
||||||
|
@ -126,7 +131,7 @@ class SCSSCacher {
|
||||||
* @param $fileName
|
* @param $fileName
|
||||||
* @return ISimpleFile
|
* @return ISimpleFile
|
||||||
*/
|
*/
|
||||||
public function getCachedCSS($appName, $fileName) {
|
public function getCachedCSS(string $appName, string $fileName): ISimpleFile {
|
||||||
$folder = $this->appData->getFolder($appName);
|
$folder = $this->appData->getFolder($appName);
|
||||||
return $folder->getFile($this->prependBaseurlPrefix($fileName));
|
return $folder->getFile($this->prependBaseurlPrefix($fileName));
|
||||||
}
|
}
|
||||||
|
@ -137,7 +142,7 @@ class SCSSCacher {
|
||||||
* @param ISimpleFolder $folder
|
* @param ISimpleFolder $folder
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
private function isCached($fileNameCSS, ISimpleFolder $folder) {
|
private function isCached(string $fileNameCSS, ISimpleFolder $folder) {
|
||||||
try {
|
try {
|
||||||
$cachedFile = $folder->getFile($fileNameCSS);
|
$cachedFile = $folder->getFile($fileNameCSS);
|
||||||
if ($cachedFile->getSize() > 0) {
|
if ($cachedFile->getSize() > 0) {
|
||||||
|
@ -151,7 +156,7 @@ class SCSSCacher {
|
||||||
}
|
}
|
||||||
$deps = json_decode($deps, true);
|
$deps = json_decode($deps, true);
|
||||||
|
|
||||||
foreach ($deps as $file=>$mtime) {
|
foreach ((array)$deps as $file=>$mtime) {
|
||||||
if (!file_exists($file) || filemtime($file) > $mtime) {
|
if (!file_exists($file) || filemtime($file) > $mtime) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +173,7 @@ class SCSSCacher {
|
||||||
* Check if the variables file has changed
|
* Check if the variables file has changed
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function variablesChanged() {
|
private function variablesChanged(): bool {
|
||||||
$injectedVariables = $this->getInjectedVariables();
|
$injectedVariables = $this->getInjectedVariables();
|
||||||
if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
|
if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
|
||||||
$this->resetCache();
|
$this->resetCache();
|
||||||
|
@ -180,14 +185,16 @@ class SCSSCacher {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache the file with AppData
|
* Cache the file with AppData
|
||||||
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $fileNameCSS
|
* @param string $fileNameCSS
|
||||||
* @param string $fileNameSCSS
|
* @param string $fileNameSCSS
|
||||||
* @param ISimpleFolder $folder
|
* @param ISimpleFolder $folder
|
||||||
* @param string $webDir
|
* @param string $webDir
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
* @throws NotPermittedException
|
||||||
*/
|
*/
|
||||||
private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
|
private function cache(string $path, string $fileNameCSS, string $fileNameSCSS, ISimpleFolder $folder, string $webDir) {
|
||||||
$scss = new Compiler();
|
$scss = new Compiler();
|
||||||
$scss->setImportPaths([
|
$scss->setImportPaths([
|
||||||
$path,
|
$path,
|
||||||
|
@ -261,7 +268,7 @@ class SCSSCacher {
|
||||||
}
|
}
|
||||||
foreach ($appDirectory as $folder) {
|
foreach ($appDirectory as $folder) {
|
||||||
foreach ($folder->getDirectoryListing() as $file) {
|
foreach ($folder->getDirectoryListing() as $file) {
|
||||||
if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") {
|
if (substr($file->getName(), -3) === 'css' || substr($file->getName(), -4) === 'deps') {
|
||||||
$file->delete();
|
$file->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,9 +278,10 @@ class SCSSCacher {
|
||||||
/**
|
/**
|
||||||
* @return string SCSS code for variables from OC_Defaults
|
* @return string SCSS code for variables from OC_Defaults
|
||||||
*/
|
*/
|
||||||
private function getInjectedVariables() {
|
private function getInjectedVariables(): string {
|
||||||
if ($this->injectedVariables !== null)
|
if ($this->injectedVariables !== null) {
|
||||||
return $this->injectedVariables;
|
return $this->injectedVariables;
|
||||||
|
}
|
||||||
$variables = '';
|
$variables = '';
|
||||||
foreach ($this->defaults->getScssVariables() as $key => $value) {
|
foreach ($this->defaults->getScssVariables() as $key => $value) {
|
||||||
$variables .= '$' . $key . ': ' . $value . ';';
|
$variables .= '$' . $key . ': ' . $value . ';';
|
||||||
|
@ -297,7 +305,7 @@ class SCSSCacher {
|
||||||
* @param string $webDir
|
* @param string $webDir
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function rebaseUrls($css, $webDir) {
|
private function rebaseUrls(string $css, string $webDir): string {
|
||||||
$re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
|
$re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
|
||||||
$subst = 'url(\''.$webDir.'/$1\')';
|
$subst = 'url(\''.$webDir.'/$1\')';
|
||||||
return preg_replace($re, $subst, $css);
|
return preg_replace($re, $subst, $css);
|
||||||
|
@ -309,20 +317,20 @@ class SCSSCacher {
|
||||||
* @param string $fileName
|
* @param string $fileName
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCachedSCSS($appName, $fileName) {
|
public function getCachedSCSS(string $appName, string $fileName): string {
|
||||||
$tmpfileLoc = explode('/', $fileName);
|
$tmpfileLoc = explode('/', $fileName);
|
||||||
$fileName = array_pop($tmpfileLoc);
|
$fileName = array_pop($tmpfileLoc);
|
||||||
$fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));
|
$fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));
|
||||||
|
|
||||||
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
|
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepend hashed base url to the css file
|
* Prepend hashed base url to the css file
|
||||||
* @param $cssFile
|
* @param string$cssFile
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function prependBaseurlPrefix($cssFile) {
|
private function prependBaseurlPrefix(string $cssFile): string {
|
||||||
$frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
$frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
||||||
return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile;
|
return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 8) . '-' . $cssFile;
|
||||||
}
|
}
|
||||||
|
@ -335,7 +343,7 @@ class SCSSCacher {
|
||||||
* @param string $webRoot the nextcloud installation root path
|
* @param string $webRoot the nextcloud installation root path
|
||||||
* @return string the webDir
|
* @return string the webDir
|
||||||
*/
|
*/
|
||||||
private function getWebDir($path, $appName, $serverRoot, $webRoot) {
|
private function getWebDir(string $path, string $appName, string $serverRoot, string $webRoot): string {
|
||||||
// Detect if path is within server root AND if path is within an app path
|
// Detect if path is within server root AND if path is within an app path
|
||||||
if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) {
|
if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) {
|
||||||
// Get the file path within the app directory
|
// Get the file path within the app directory
|
||||||
|
|
Loading…
Reference in New Issue