Completed
Pull Request — master (#3530)
by Julius
21:13 queued 04:59
created
lib/private/Template/SCSSCacher.php 2 patches
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -34,182 +34,182 @@
 block discarded – undo
34 34
 
35 35
 class SCSSCacher {
36 36
 
37
-	/** @var ILogger */
38
-	protected $logger;
39
-
40
-	/** @var IAppData */
41
-	protected $appData;
42
-
43
-	/** @var IURLGenerator */
44
-	protected $urlGenerator;
45
-
46
-	/** @var SystemConfig */
47
-	protected $systemConfig;
48
-
49
-	/**
50
-	 * @param ILogger $logger
51
-	 * @param IAppData $appData
52
-	 * @param IURLGenerator $urlGenerator
53
-	 * @param SystemConfig $systemConfig
54
-	 */
55
-	public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
56
-		$this->logger = $logger;
57
-		$this->appData = $appData;
58
-		$this->urlGenerator = $urlGenerator;
59
-		$this->systemConfig = $systemConfig;
60
-	}
61
-
62
-	/**
63
-	 * Process the caching process if needed
64
-	 * @param string $root Root path to the nextcloud installation
65
-	 * @param string $file
66
-	 * @param string $app The app name
67
-	 * @return boolean
68
-	 */
69
-	public function process($root, $file, $app) {
70
-		$path = explode('/', $root . '/' . $file);
71
-
72
-		$fileNameSCSS = array_pop($path);
73
-		$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
74
-
75
-		$path = implode('/', $path);
76
-
77
-		$webDir = explode('/', $file);
78
-		array_pop($webDir);
79
-		$webDir = implode('/', $webDir);
80
-
81
-		try {
82
-			$folder = $this->appData->getFolder($app);
83
-		} catch(NotFoundException $e) {
84
-			// creating css appdata folder
85
-			$folder = $this->appData->newFolder($app);
86
-		}
87
-
88
-		if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path) && !$this->variablesChanged($fileNameCSS, $folder)) {
89
-			return true;
90
-		}
91
-		return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
92
-	}
93
-
94
-	/**
95
-	 * Check if the file is cached or not
96
-	 * @param string $fileNameCSS
97
-	 * @param string $fileNameSCSS
98
-	 * @param ISimpleFolder $folder
99
-	 * @param string $path
100
-	 * @return boolean
101
-	 */
102
-	private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
103
-		try {
104
-			$cachedFile = $folder->getFile($fileNameCSS);
105
-			if( $cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
106
-				&& $cachedFile->getSize() > 0 ) {
107
-				return true;
108
-			}
109
-		} catch(NotFoundException $e) {
110
-			return false;
111
-		}
112
-		return false;
113
-	}
114
-
115
-	/**
116
-	 * Check if the variables file has changed
117
-	 * @param string $fileNameCSS
118
-	 * @param ISimpleFolder $folder
119
-	 * @return bool
120
-	 */
121
-	private function variablesChanged($fileNameCSS, ISimpleFolder $folder) {
122
-		$variablesFile = \OC::$SERVERROOT . '/core/css/variables.scss';
123
-		try {
124
-			$cachedFile = $folder->getFile($fileNameCSS);
125
-			if ($cachedFile->getMTime() < filemtime($variablesFile)
126
-				|| $cachedFile->getSize() === 0
127
-			) {
128
-				return true;
129
-			}
130
-		} catch (NotFoundException $e) {
131
-			return true;
132
-		}
133
-		return false;
134
-	}
135
-
136
-	/**
137
-	 * Cache the file with AppData
138
-	 * @param string $path
139
-	 * @param string $fileNameCSS
140
-	 * @param string $fileNameSCSS
141
-	 * @param ISimpleFolder $folder
142
-	 * @param string $webDir
143
-	 * @return boolean
144
-	 */
145
-	private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
146
-		$scss = new Compiler();
147
-		$scss->setImportPaths([
148
-			$path,
149
-			\OC::$SERVERROOT . '/core/css/',
150
-		]);
151
-		if($this->systemConfig->getValue('debug')) {
152
-			// Debug mode
153
-			$scss->setFormatter(Expanded::class);
154
-			$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
155
-		} else {
156
-			// Compression
157
-			$scss->setFormatter(Crunched::class);
158
-		}
159
-
160
-		try {
161
-			$cachedfile = $folder->getFile($fileNameCSS);
162
-		} catch(NotFoundException $e) {
163
-			$cachedfile = $folder->newFile($fileNameCSS);
164
-		}
165
-
166
-		// Compile
167
-		try {
168
-			$compiledScss = $scss->compile(
169
-				'@import "variables.scss";' .
170
-				'@import "'.$fileNameSCSS.'";');
171
-		} catch(ParserException $e) {
172
-			$this->logger->error($e, ['app' => 'core']);
173
-			return false;
174
-		}
175
-
176
-		try {
177
-			$cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
178
-			$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
179
-			return true;
180
-		} catch(NotFoundException $e) {
181
-			return false;
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * Add the correct uri prefix to make uri valid again
187
-	 * @param string $css
188
-	 * @param string $webDir
189
-	 * @return string
190
-	 */
191
-	private function rebaseUrls($css, $webDir) {
192
-		$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
193
-		// OC\Route\Router:75
194
-		if(($this->systemConfig->getValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
195
-			$subst = 'url(\'../../'.$webDir.'/$1\')';	
196
-		} else {
197
-			$subst = 'url(\'../../../'.$webDir.'/$1\')';
198
-		}
199
-		return preg_replace($re, $subst, $css);
200
-	}
201
-
202
-	/**
203
-	 * Return the cached css file uri
204
-	 * @param string $appName the app name
205
-	 * @param string $fileName
206
-	 * @return string
207
-	 */
208
-	public function getCachedSCSS($appName, $fileName) {
209
-		$tmpfileLoc = explode('/', $fileName);
210
-		$fileName = array_pop($tmpfileLoc);
211
-		$fileName = str_replace('.scss', '.css', $fileName);
212
-
213
-		return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
214
-	}
37
+    /** @var ILogger */
38
+    protected $logger;
39
+
40
+    /** @var IAppData */
41
+    protected $appData;
42
+
43
+    /** @var IURLGenerator */
44
+    protected $urlGenerator;
45
+
46
+    /** @var SystemConfig */
47
+    protected $systemConfig;
48
+
49
+    /**
50
+     * @param ILogger $logger
51
+     * @param IAppData $appData
52
+     * @param IURLGenerator $urlGenerator
53
+     * @param SystemConfig $systemConfig
54
+     */
55
+    public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
56
+        $this->logger = $logger;
57
+        $this->appData = $appData;
58
+        $this->urlGenerator = $urlGenerator;
59
+        $this->systemConfig = $systemConfig;
60
+    }
61
+
62
+    /**
63
+     * Process the caching process if needed
64
+     * @param string $root Root path to the nextcloud installation
65
+     * @param string $file
66
+     * @param string $app The app name
67
+     * @return boolean
68
+     */
69
+    public function process($root, $file, $app) {
70
+        $path = explode('/', $root . '/' . $file);
71
+
72
+        $fileNameSCSS = array_pop($path);
73
+        $fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
74
+
75
+        $path = implode('/', $path);
76
+
77
+        $webDir = explode('/', $file);
78
+        array_pop($webDir);
79
+        $webDir = implode('/', $webDir);
80
+
81
+        try {
82
+            $folder = $this->appData->getFolder($app);
83
+        } catch(NotFoundException $e) {
84
+            // creating css appdata folder
85
+            $folder = $this->appData->newFolder($app);
86
+        }
87
+
88
+        if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path) && !$this->variablesChanged($fileNameCSS, $folder)) {
89
+            return true;
90
+        }
91
+        return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
92
+    }
93
+
94
+    /**
95
+     * Check if the file is cached or not
96
+     * @param string $fileNameCSS
97
+     * @param string $fileNameSCSS
98
+     * @param ISimpleFolder $folder
99
+     * @param string $path
100
+     * @return boolean
101
+     */
102
+    private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
103
+        try {
104
+            $cachedFile = $folder->getFile($fileNameCSS);
105
+            if( $cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
106
+                && $cachedFile->getSize() > 0 ) {
107
+                return true;
108
+            }
109
+        } catch(NotFoundException $e) {
110
+            return false;
111
+        }
112
+        return false;
113
+    }
114
+
115
+    /**
116
+     * Check if the variables file has changed
117
+     * @param string $fileNameCSS
118
+     * @param ISimpleFolder $folder
119
+     * @return bool
120
+     */
121
+    private function variablesChanged($fileNameCSS, ISimpleFolder $folder) {
122
+        $variablesFile = \OC::$SERVERROOT . '/core/css/variables.scss';
123
+        try {
124
+            $cachedFile = $folder->getFile($fileNameCSS);
125
+            if ($cachedFile->getMTime() < filemtime($variablesFile)
126
+                || $cachedFile->getSize() === 0
127
+            ) {
128
+                return true;
129
+            }
130
+        } catch (NotFoundException $e) {
131
+            return true;
132
+        }
133
+        return false;
134
+    }
135
+
136
+    /**
137
+     * Cache the file with AppData
138
+     * @param string $path
139
+     * @param string $fileNameCSS
140
+     * @param string $fileNameSCSS
141
+     * @param ISimpleFolder $folder
142
+     * @param string $webDir
143
+     * @return boolean
144
+     */
145
+    private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
146
+        $scss = new Compiler();
147
+        $scss->setImportPaths([
148
+            $path,
149
+            \OC::$SERVERROOT . '/core/css/',
150
+        ]);
151
+        if($this->systemConfig->getValue('debug')) {
152
+            // Debug mode
153
+            $scss->setFormatter(Expanded::class);
154
+            $scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
155
+        } else {
156
+            // Compression
157
+            $scss->setFormatter(Crunched::class);
158
+        }
159
+
160
+        try {
161
+            $cachedfile = $folder->getFile($fileNameCSS);
162
+        } catch(NotFoundException $e) {
163
+            $cachedfile = $folder->newFile($fileNameCSS);
164
+        }
165
+
166
+        // Compile
167
+        try {
168
+            $compiledScss = $scss->compile(
169
+                '@import "variables.scss";' .
170
+                '@import "'.$fileNameSCSS.'";');
171
+        } catch(ParserException $e) {
172
+            $this->logger->error($e, ['app' => 'core']);
173
+            return false;
174
+        }
175
+
176
+        try {
177
+            $cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
178
+            $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
179
+            return true;
180
+        } catch(NotFoundException $e) {
181
+            return false;
182
+        }
183
+    }
184
+
185
+    /**
186
+     * Add the correct uri prefix to make uri valid again
187
+     * @param string $css
188
+     * @param string $webDir
189
+     * @return string
190
+     */
191
+    private function rebaseUrls($css, $webDir) {
192
+        $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
193
+        // OC\Route\Router:75
194
+        if(($this->systemConfig->getValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
195
+            $subst = 'url(\'../../'.$webDir.'/$1\')';	
196
+        } else {
197
+            $subst = 'url(\'../../../'.$webDir.'/$1\')';
198
+        }
199
+        return preg_replace($re, $subst, $css);
200
+    }
201
+
202
+    /**
203
+     * Return the cached css file uri
204
+     * @param string $appName the app name
205
+     * @param string $fileName
206
+     * @return string
207
+     */
208
+    public function getCachedSCSS($appName, $fileName) {
209
+        $tmpfileLoc = explode('/', $fileName);
210
+        $fileName = array_pop($tmpfileLoc);
211
+        $fileName = str_replace('.scss', '.css', $fileName);
212
+
213
+        return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
214
+    }
215 215
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 	 * @return boolean
68 68
 	 */
69 69
 	public function process($root, $file, $app) {
70
-		$path = explode('/', $root . '/' . $file);
70
+		$path = explode('/', $root.'/'.$file);
71 71
 
72 72
 		$fileNameSCSS = array_pop($path);
73 73
 		$fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
@@ -80,12 +80,12 @@  discard block
 block discarded – undo
80 80
 
81 81
 		try {
82 82
 			$folder = $this->appData->getFolder($app);
83
-		} catch(NotFoundException $e) {
83
+		} catch (NotFoundException $e) {
84 84
 			// creating css appdata folder
85 85
 			$folder = $this->appData->newFolder($app);
86 86
 		}
87 87
 
88
-		if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path) && !$this->variablesChanged($fileNameCSS, $folder)) {
88
+		if ($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path) && !$this->variablesChanged($fileNameCSS, $folder)) {
89 89
 			return true;
90 90
 		}
91 91
 		return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
@@ -102,11 +102,11 @@  discard block
 block discarded – undo
102 102
 	private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
103 103
 		try {
104 104
 			$cachedFile = $folder->getFile($fileNameCSS);
105
-			if( $cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
106
-				&& $cachedFile->getSize() > 0 ) {
105
+			if ($cachedFile->getMTime() > filemtime($path.'/'.$fileNameSCSS)
106
+				&& $cachedFile->getSize() > 0) {
107 107
 				return true;
108 108
 			}
109
-		} catch(NotFoundException $e) {
109
+		} catch (NotFoundException $e) {
110 110
 			return false;
111 111
 		}
112 112
 		return false;
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
 	 * @return bool
120 120
 	 */
121 121
 	private function variablesChanged($fileNameCSS, ISimpleFolder $folder) {
122
-		$variablesFile = \OC::$SERVERROOT . '/core/css/variables.scss';
122
+		$variablesFile = \OC::$SERVERROOT.'/core/css/variables.scss';
123 123
 		try {
124 124
 			$cachedFile = $folder->getFile($fileNameCSS);
125 125
 			if ($cachedFile->getMTime() < filemtime($variablesFile)
@@ -146,9 +146,9 @@  discard block
 block discarded – undo
146 146
 		$scss = new Compiler();
147 147
 		$scss->setImportPaths([
148 148
 			$path,
149
-			\OC::$SERVERROOT . '/core/css/',
149
+			\OC::$SERVERROOT.'/core/css/',
150 150
 		]);
151
-		if($this->systemConfig->getValue('debug')) {
151
+		if ($this->systemConfig->getValue('debug')) {
152 152
 			// Debug mode
153 153
 			$scss->setFormatter(Expanded::class);
154 154
 			$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
@@ -159,16 +159,16 @@  discard block
 block discarded – undo
159 159
 
160 160
 		try {
161 161
 			$cachedfile = $folder->getFile($fileNameCSS);
162
-		} catch(NotFoundException $e) {
162
+		} catch (NotFoundException $e) {
163 163
 			$cachedfile = $folder->newFile($fileNameCSS);
164 164
 		}
165 165
 
166 166
 		// Compile
167 167
 		try {
168 168
 			$compiledScss = $scss->compile(
169
-				'@import "variables.scss";' .
169
+				'@import "variables.scss";'.
170 170
 				'@import "'.$fileNameSCSS.'";');
171
-		} catch(ParserException $e) {
171
+		} catch (ParserException $e) {
172 172
 			$this->logger->error($e, ['app' => 'core']);
173 173
 			return false;
174 174
 		}
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 			$cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
178 178
 			$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
179 179
 			return true;
180
-		} catch(NotFoundException $e) {
180
+		} catch (NotFoundException $e) {
181 181
 			return false;
182 182
 		}
183 183
 	}
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
 	private function rebaseUrls($css, $webDir) {
192 192
 		$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
193 193
 		// OC\Route\Router:75
194
-		if(($this->systemConfig->getValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
194
+		if (($this->systemConfig->getValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
195 195
 			$subst = 'url(\'../../'.$webDir.'/$1\')';	
196 196
 		} else {
197 197
 			$subst = 'url(\'../../../'.$webDir.'/$1\')';
Please login to merge, or discard this patch.