Passed
Push — master ( ecc37f...c352bb )
by John
14:00 queued 10s
created
lib/private/Template/IconsCacher.php 2 patches
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -35,221 +35,221 @@
 block discarded – undo
35 35
 
36 36
 class IconsCacher {
37 37
 
38
-	/** @var ILogger */
39
-	protected $logger;
40
-
41
-	/** @var IAppData */
42
-	protected $appData;
43
-
44
-	/** @var ISimpleFolder */
45
-	private $folder;
46
-
47
-	/** @var IURLGenerator */
48
-	protected $urlGenerator;
49
-
50
-	/** @var ITimeFactory */
51
-	protected $timeFactory;
52
-
53
-	/** @var string */
54
-	private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m';
55
-
56
-	/** @var string */
57
-	private $fileName = 'icons-vars.css';
58
-
59
-	private $iconList = 'icons-list.template';
60
-
61
-	private $cachedCss;
62
-	private $cachedList;
63
-
64
-	/**
65
-	 * @param ILogger $logger
66
-	 * @param Factory $appDataFactory
67
-	 * @param IURLGenerator $urlGenerator
68
-	 * @param ITimeFactory $timeFactory
69
-	 * @throws \OCP\Files\NotPermittedException
70
-	 */
71
-	public function __construct(ILogger $logger,
72
-								Factory $appDataFactory,
73
-								IURLGenerator $urlGenerator,
74
-								ITimeFactory $timeFactory) {
75
-		$this->logger       = $logger;
76
-		$this->appData      = $appDataFactory->get('css');
77
-		$this->urlGenerator = $urlGenerator;
78
-		$this->timeFactory  = $timeFactory;
79
-
80
-		try {
81
-			$this->folder = $this->appData->getFolder('icons');
82
-		} catch (NotFoundException $e) {
83
-			$this->folder = $this->appData->newFolder('icons');
84
-		}
85
-	}
86
-
87
-	private function getIconsFromCss(string $css): array {
88
-		preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
89
-		$icons = [];
90
-		foreach ($matches as $icon) {
91
-			$icons[$icon[1]] = $icon[2];
92
-		}
93
-
94
-		return $icons;
95
-	}
96
-
97
-	/**
98
-	 * @param string $css
99
-	 * @return string
100
-	 * @throws NotFoundException
101
-	 * @throws \OCP\Files\NotPermittedException
102
-	 */
103
-	public function setIconsCss(string $css): string {
104
-
105
-		$cachedFile = $this->getCachedList();
106
-		if (!$cachedFile) {
107
-			$currentData = '';
108
-			$cachedFile = $this->folder->newFile($this->iconList);
109
-		} else {
110
-			$currentData = $cachedFile->getContent();
111
-		}
112
-
113
-		$cachedVarsCssFile = $this->getCachedCSS();
114
-		if (!$cachedVarsCssFile) {
115
-			$cachedVarsCssFile = $this->folder->newFile($this->fileName);
116
-		}
117
-
118
-		$icons = $this->getIconsFromCss($currentData . $css);
119
-
120
-		$data = '';
121
-		$list = '';
122
-		foreach ($icons as $icon => $url) {
123
-			$list .= "--$icon: url('$url');";
124
-			list($location,$color) = $this->parseUrl($url);
125
-			$svg = false;
126
-			if ($location !== '' && \file_exists($location)) {
127
-				$svg = \file_get_contents($location);
128
-			}
129
-			if ($svg === false) {
130
-				$this->logger->debug('Failed to get icon file ' . $location);
131
-				$data .= "--$icon: url('$url');";
132
-				continue;
133
-			}
134
-			$encode = base64_encode($this->colorizeSvg($svg, $color));
135
-			$data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
136
-		}
137
-
138
-		if (\strlen($data) > 0 && \strlen($list) > 0) {
139
-			$data = ":root {\n$data\n}";
140
-			$cachedVarsCssFile->putContent($data);
141
-			$list = ":root {\n$list\n}";
142
-			$cachedFile->putContent($list);
143
-			$this->cachedList = null;
144
-			$this->cachedCss = null;
145
-		}
146
-
147
-		return preg_replace($this->iconVarRE, '', $css);
148
-	}
149
-
150
-	/**
151
-	 * @param $url
152
-	 * @return array
153
-	 */
154
-	private function parseUrl($url): array {
155
-		$location = '';
156
-		$color = '';
157
-		$base = $this->getRoutePrefix() . '/svg/';
158
-		$cleanUrl = \substr($url, \strlen($base));
159
-		if (\strpos($url, $base . 'core') === 0) {
160
-			$cleanUrl = \substr($cleanUrl, \strlen('core'));
161
-			if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
162
-				list(,$cleanUrl,$color) = $matches;
163
-				$location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
164
-			}
165
-		} elseif (\strpos($url, $base) === 0) {
166
-			if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
167
-				list(,$app,$cleanUrl, $color) = $matches;
168
-				$location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
169
-				if ($app === 'settings') {
170
-					$location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
171
-				}
172
-			}
173
-
174
-		}
175
-		return [
176
-			$location,
177
-			$color
178
-		];
179
-	}
180
-
181
-	/**
182
-	 * @param $svg
183
-	 * @param $color
184
-	 * @return string
185
-	 */
186
-	public function colorizeSvg($svg, $color): string {
187
-		// add fill (fill is not present on black elements)
188
-		$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi';
189
-		$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
190
-
191
-		// replace any fill or stroke colors
192
-		$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
193
-		$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
194
-		return $svg;
195
-	}
196
-
197
-	private function getRoutePrefix() {
198
-		$frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
199
-		$prefix = \OC::$WEBROOT . '/index.php';
200
-		if ($frontControllerActive) {
201
-			$prefix = \OC::$WEBROOT;
202
-		}
203
-		return $prefix;
204
-	}
205
-
206
-	/**
207
-	 * Get icons css file
208
-	 * @return ISimpleFile|boolean
209
-	 */
210
-	public function getCachedCSS() {
211
-		try {
212
-			if (!$this->cachedCss) {
213
-				$this->cachedCss = $this->folder->getFile($this->fileName);
214
-			}
215
-			return $this->cachedCss;
216
-		} catch (NotFoundException $e) {
217
-			return false;
218
-		}
219
-	}
220
-
221
-	/**
222
-	 * Get icon-vars list template
223
-	 * @return ISimpleFile|boolean
224
-	 */
225
-	public function getCachedList() {
226
-		try {
227
-			if (!$this->cachedList) {
228
-				$this->cachedList = $this->folder->getFile($this->iconList);
229
-			}
230
-			return $this->cachedList;
231
-		} catch (NotFoundException $e) {
232
-			return false;
233
-		}
234
-	}
235
-
236
-	public function injectCss() {
237
-		$mtime = $this->timeFactory->getTime();
238
-		$file = $this->getCachedList();
239
-		if ($file) {
240
-			$mtime = $file->getMTime();
241
-		}
242
-		// Only inject once
243
-		foreach (\OC_Util::$headers as $header) {
244
-			if (
245
-				array_key_exists('attributes', $header) &&
246
-				array_key_exists('href', $header['attributes']) &&
247
-				strpos($header['attributes']['href'], $this->fileName) !== false) {
248
-				return;
249
-			}
250
-		}
251
-		$linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]);
252
-		\OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
253
-	}
38
+    /** @var ILogger */
39
+    protected $logger;
40
+
41
+    /** @var IAppData */
42
+    protected $appData;
43
+
44
+    /** @var ISimpleFolder */
45
+    private $folder;
46
+
47
+    /** @var IURLGenerator */
48
+    protected $urlGenerator;
49
+
50
+    /** @var ITimeFactory */
51
+    protected $timeFactory;
52
+
53
+    /** @var string */
54
+    private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m';
55
+
56
+    /** @var string */
57
+    private $fileName = 'icons-vars.css';
58
+
59
+    private $iconList = 'icons-list.template';
60
+
61
+    private $cachedCss;
62
+    private $cachedList;
63
+
64
+    /**
65
+     * @param ILogger $logger
66
+     * @param Factory $appDataFactory
67
+     * @param IURLGenerator $urlGenerator
68
+     * @param ITimeFactory $timeFactory
69
+     * @throws \OCP\Files\NotPermittedException
70
+     */
71
+    public function __construct(ILogger $logger,
72
+                                Factory $appDataFactory,
73
+                                IURLGenerator $urlGenerator,
74
+                                ITimeFactory $timeFactory) {
75
+        $this->logger       = $logger;
76
+        $this->appData      = $appDataFactory->get('css');
77
+        $this->urlGenerator = $urlGenerator;
78
+        $this->timeFactory  = $timeFactory;
79
+
80
+        try {
81
+            $this->folder = $this->appData->getFolder('icons');
82
+        } catch (NotFoundException $e) {
83
+            $this->folder = $this->appData->newFolder('icons');
84
+        }
85
+    }
86
+
87
+    private function getIconsFromCss(string $css): array {
88
+        preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
89
+        $icons = [];
90
+        foreach ($matches as $icon) {
91
+            $icons[$icon[1]] = $icon[2];
92
+        }
93
+
94
+        return $icons;
95
+    }
96
+
97
+    /**
98
+     * @param string $css
99
+     * @return string
100
+     * @throws NotFoundException
101
+     * @throws \OCP\Files\NotPermittedException
102
+     */
103
+    public function setIconsCss(string $css): string {
104
+
105
+        $cachedFile = $this->getCachedList();
106
+        if (!$cachedFile) {
107
+            $currentData = '';
108
+            $cachedFile = $this->folder->newFile($this->iconList);
109
+        } else {
110
+            $currentData = $cachedFile->getContent();
111
+        }
112
+
113
+        $cachedVarsCssFile = $this->getCachedCSS();
114
+        if (!$cachedVarsCssFile) {
115
+            $cachedVarsCssFile = $this->folder->newFile($this->fileName);
116
+        }
117
+
118
+        $icons = $this->getIconsFromCss($currentData . $css);
119
+
120
+        $data = '';
121
+        $list = '';
122
+        foreach ($icons as $icon => $url) {
123
+            $list .= "--$icon: url('$url');";
124
+            list($location,$color) = $this->parseUrl($url);
125
+            $svg = false;
126
+            if ($location !== '' && \file_exists($location)) {
127
+                $svg = \file_get_contents($location);
128
+            }
129
+            if ($svg === false) {
130
+                $this->logger->debug('Failed to get icon file ' . $location);
131
+                $data .= "--$icon: url('$url');";
132
+                continue;
133
+            }
134
+            $encode = base64_encode($this->colorizeSvg($svg, $color));
135
+            $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
136
+        }
137
+
138
+        if (\strlen($data) > 0 && \strlen($list) > 0) {
139
+            $data = ":root {\n$data\n}";
140
+            $cachedVarsCssFile->putContent($data);
141
+            $list = ":root {\n$list\n}";
142
+            $cachedFile->putContent($list);
143
+            $this->cachedList = null;
144
+            $this->cachedCss = null;
145
+        }
146
+
147
+        return preg_replace($this->iconVarRE, '', $css);
148
+    }
149
+
150
+    /**
151
+     * @param $url
152
+     * @return array
153
+     */
154
+    private function parseUrl($url): array {
155
+        $location = '';
156
+        $color = '';
157
+        $base = $this->getRoutePrefix() . '/svg/';
158
+        $cleanUrl = \substr($url, \strlen($base));
159
+        if (\strpos($url, $base . 'core') === 0) {
160
+            $cleanUrl = \substr($cleanUrl, \strlen('core'));
161
+            if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
162
+                list(,$cleanUrl,$color) = $matches;
163
+                $location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
164
+            }
165
+        } elseif (\strpos($url, $base) === 0) {
166
+            if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
167
+                list(,$app,$cleanUrl, $color) = $matches;
168
+                $location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
169
+                if ($app === 'settings') {
170
+                    $location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
171
+                }
172
+            }
173
+
174
+        }
175
+        return [
176
+            $location,
177
+            $color
178
+        ];
179
+    }
180
+
181
+    /**
182
+     * @param $svg
183
+     * @param $color
184
+     * @return string
185
+     */
186
+    public function colorizeSvg($svg, $color): string {
187
+        // add fill (fill is not present on black elements)
188
+        $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi';
189
+        $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
190
+
191
+        // replace any fill or stroke colors
192
+        $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
193
+        $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
194
+        return $svg;
195
+    }
196
+
197
+    private function getRoutePrefix() {
198
+        $frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
199
+        $prefix = \OC::$WEBROOT . '/index.php';
200
+        if ($frontControllerActive) {
201
+            $prefix = \OC::$WEBROOT;
202
+        }
203
+        return $prefix;
204
+    }
205
+
206
+    /**
207
+     * Get icons css file
208
+     * @return ISimpleFile|boolean
209
+     */
210
+    public function getCachedCSS() {
211
+        try {
212
+            if (!$this->cachedCss) {
213
+                $this->cachedCss = $this->folder->getFile($this->fileName);
214
+            }
215
+            return $this->cachedCss;
216
+        } catch (NotFoundException $e) {
217
+            return false;
218
+        }
219
+    }
220
+
221
+    /**
222
+     * Get icon-vars list template
223
+     * @return ISimpleFile|boolean
224
+     */
225
+    public function getCachedList() {
226
+        try {
227
+            if (!$this->cachedList) {
228
+                $this->cachedList = $this->folder->getFile($this->iconList);
229
+            }
230
+            return $this->cachedList;
231
+        } catch (NotFoundException $e) {
232
+            return false;
233
+        }
234
+    }
235
+
236
+    public function injectCss() {
237
+        $mtime = $this->timeFactory->getTime();
238
+        $file = $this->getCachedList();
239
+        if ($file) {
240
+            $mtime = $file->getMTime();
241
+        }
242
+        // Only inject once
243
+        foreach (\OC_Util::$headers as $header) {
244
+            if (
245
+                array_key_exists('attributes', $header) &&
246
+                array_key_exists('href', $header['attributes']) &&
247
+                strpos($header['attributes']['href'], $this->fileName) !== false) {
248
+                return;
249
+            }
250
+        }
251
+        $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]);
252
+        \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
253
+    }
254 254
 
255 255
 }
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare (strict_types = 1);
2
+declare(strict_types=1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2018, John Molakvoæ ([email protected])
5 5
  *
@@ -115,24 +115,24 @@  discard block
 block discarded – undo
115 115
 			$cachedVarsCssFile = $this->folder->newFile($this->fileName);
116 116
 		}
117 117
 
118
-		$icons = $this->getIconsFromCss($currentData . $css);
118
+		$icons = $this->getIconsFromCss($currentData.$css);
119 119
 
120 120
 		$data = '';
121 121
 		$list = '';
122 122
 		foreach ($icons as $icon => $url) {
123 123
 			$list .= "--$icon: url('$url');";
124
-			list($location,$color) = $this->parseUrl($url);
124
+			list($location, $color) = $this->parseUrl($url);
125 125
 			$svg = false;
126 126
 			if ($location !== '' && \file_exists($location)) {
127 127
 				$svg = \file_get_contents($location);
128 128
 			}
129 129
 			if ($svg === false) {
130
-				$this->logger->debug('Failed to get icon file ' . $location);
130
+				$this->logger->debug('Failed to get icon file '.$location);
131 131
 				$data .= "--$icon: url('$url');";
132 132
 				continue;
133 133
 			}
134 134
 			$encode = base64_encode($this->colorizeSvg($svg, $color));
135
-			$data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
135
+			$data .= '--'.$icon.': url(data:image/svg+xml;base64,'.$encode.');';
136 136
 		}
137 137
 
138 138
 		if (\strlen($data) > 0 && \strlen($list) > 0) {
@@ -154,20 +154,20 @@  discard block
 block discarded – undo
154 154
 	private function parseUrl($url): array {
155 155
 		$location = '';
156 156
 		$color = '';
157
-		$base = $this->getRoutePrefix() . '/svg/';
157
+		$base = $this->getRoutePrefix().'/svg/';
158 158
 		$cleanUrl = \substr($url, \strlen($base));
159
-		if (\strpos($url, $base . 'core') === 0) {
159
+		if (\strpos($url, $base.'core') === 0) {
160 160
 			$cleanUrl = \substr($cleanUrl, \strlen('core'));
161 161
 			if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
162
-				list(,$cleanUrl,$color) = $matches;
163
-				$location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
162
+				list(,$cleanUrl, $color) = $matches;
163
+				$location = \OC::$SERVERROOT.'/core/img/'.$cleanUrl.'.svg';
164 164
 			}
165 165
 		} elseif (\strpos($url, $base) === 0) {
166
-			if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
167
-				list(,$app,$cleanUrl, $color) = $matches;
168
-				$location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
166
+			if (\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
167
+				list(,$app, $cleanUrl, $color) = $matches;
168
+				$location = \OC_App::getAppPath($app).'/img/'.$cleanUrl.'.svg';
169 169
 				if ($app === 'settings') {
170
-					$location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
170
+					$location = \OC::$SERVERROOT.'/settings/img/'.$cleanUrl.'.svg';
171 171
 				}
172 172
 			}
173 173
 
@@ -186,17 +186,17 @@  discard block
 block discarded – undo
186 186
 	public function colorizeSvg($svg, $color): string {
187 187
 		// add fill (fill is not present on black elements)
188 188
 		$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi';
189
-		$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
189
+		$svg = preg_replace($fillRe, '<$1 fill="#'.$color.'"/>', $svg);
190 190
 
191 191
 		// replace any fill or stroke colors
192
-		$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
193
-		$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
192
+		$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#'.$color.'"', $svg);
193
+		$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#'.$color.'"', $svg);
194 194
 		return $svg;
195 195
 	}
196 196
 
197 197
 	private function getRoutePrefix() {
198 198
 		$frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
199
-		$prefix = \OC::$WEBROOT . '/index.php';
199
+		$prefix = \OC::$WEBROOT.'/index.php';
200 200
 		if ($frontControllerActive) {
201 201
 			$prefix = \OC::$WEBROOT;
202 202
 		}
Please login to merge, or discard this patch.