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