@@ -34,114 +34,114 @@ |
||
34 | 34 | |
35 | 35 | class IconsCacher { |
36 | 36 | |
37 | - /** @var ILogger */ |
|
38 | - protected $logger; |
|
39 | - |
|
40 | - /** @var IAppData */ |
|
41 | - protected $appData; |
|
42 | - |
|
43 | - /** @var ISimpleFolder */ |
|
44 | - private $folder; |
|
45 | - |
|
46 | - /** @var IURLGenerator */ |
|
47 | - protected $urlGenerator; |
|
48 | - |
|
49 | - /** @var string */ |
|
50 | - private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+): url\(["\']([a-zA-Z0-9-_\~\/\.\?\=]+)[^;]+;/m'; |
|
51 | - |
|
52 | - /** @var string */ |
|
53 | - private $fileName = 'icons-vars.css'; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param ILogger $logger |
|
57 | - * @param Factory $appDataFactory |
|
58 | - * @param IURLGenerator $urlGenerator |
|
59 | - */ |
|
60 | - public function __construct(ILogger $logger, |
|
61 | - Factory $appDataFactory, |
|
62 | - IURLGenerator $urlGenerator) { |
|
63 | - $this->logger = $logger; |
|
64 | - $this->appData = $appDataFactory->get('css'); |
|
65 | - $this->urlGenerator = $urlGenerator; |
|
66 | - |
|
67 | - try { |
|
68 | - $this->folder = $this->appData->getFolder('icons'); |
|
69 | - } catch (NotFoundException $e) { |
|
70 | - $this->folder = $this->appData->newFolder('icons'); |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - private function getIconsFromCss(string $css): array{ |
|
75 | - preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER); |
|
76 | - $icons = []; |
|
77 | - foreach ($matches as $icon) { |
|
78 | - $icons[$icon[1]] = $icon[2]; |
|
79 | - } |
|
80 | - |
|
81 | - return $icons; |
|
82 | - } |
|
83 | - /** |
|
84 | - * Parse and cache css |
|
85 | - * |
|
86 | - * @param string $css |
|
87 | - */ |
|
88 | - public function setIconsCss(string $css) { |
|
89 | - |
|
90 | - $cachedFile = $this->getCachedCSS(); |
|
91 | - if (!$cachedFile) { |
|
92 | - $currentData = ''; |
|
93 | - } else { |
|
94 | - $currentData = $cachedFile->getContent(); |
|
95 | - } |
|
96 | - |
|
97 | - // remove :root |
|
98 | - $currentData = str_replace([':root {', '}'], '', $currentData); |
|
99 | - |
|
100 | - $icons = $this->getIconsFromCss($currentData . $css); |
|
101 | - |
|
102 | - $data = ''; |
|
103 | - foreach ($icons as $icon => $url) { |
|
104 | - $data .= "--$icon: url('$url');"; |
|
105 | - } |
|
106 | - |
|
107 | - if (strlen($data) > 0) { |
|
108 | - if (!$cachedFile) { |
|
109 | - $cachedFile = $this->folder->newFile($this->fileName); |
|
110 | - } |
|
111 | - |
|
112 | - $data = ":root { |
|
37 | + /** @var ILogger */ |
|
38 | + protected $logger; |
|
39 | + |
|
40 | + /** @var IAppData */ |
|
41 | + protected $appData; |
|
42 | + |
|
43 | + /** @var ISimpleFolder */ |
|
44 | + private $folder; |
|
45 | + |
|
46 | + /** @var IURLGenerator */ |
|
47 | + protected $urlGenerator; |
|
48 | + |
|
49 | + /** @var string */ |
|
50 | + private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+): url\(["\']([a-zA-Z0-9-_\~\/\.\?\=]+)[^;]+;/m'; |
|
51 | + |
|
52 | + /** @var string */ |
|
53 | + private $fileName = 'icons-vars.css'; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param ILogger $logger |
|
57 | + * @param Factory $appDataFactory |
|
58 | + * @param IURLGenerator $urlGenerator |
|
59 | + */ |
|
60 | + public function __construct(ILogger $logger, |
|
61 | + Factory $appDataFactory, |
|
62 | + IURLGenerator $urlGenerator) { |
|
63 | + $this->logger = $logger; |
|
64 | + $this->appData = $appDataFactory->get('css'); |
|
65 | + $this->urlGenerator = $urlGenerator; |
|
66 | + |
|
67 | + try { |
|
68 | + $this->folder = $this->appData->getFolder('icons'); |
|
69 | + } catch (NotFoundException $e) { |
|
70 | + $this->folder = $this->appData->newFolder('icons'); |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + private function getIconsFromCss(string $css): array{ |
|
75 | + preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER); |
|
76 | + $icons = []; |
|
77 | + foreach ($matches as $icon) { |
|
78 | + $icons[$icon[1]] = $icon[2]; |
|
79 | + } |
|
80 | + |
|
81 | + return $icons; |
|
82 | + } |
|
83 | + /** |
|
84 | + * Parse and cache css |
|
85 | + * |
|
86 | + * @param string $css |
|
87 | + */ |
|
88 | + public function setIconsCss(string $css) { |
|
89 | + |
|
90 | + $cachedFile = $this->getCachedCSS(); |
|
91 | + if (!$cachedFile) { |
|
92 | + $currentData = ''; |
|
93 | + } else { |
|
94 | + $currentData = $cachedFile->getContent(); |
|
95 | + } |
|
96 | + |
|
97 | + // remove :root |
|
98 | + $currentData = str_replace([':root {', '}'], '', $currentData); |
|
99 | + |
|
100 | + $icons = $this->getIconsFromCss($currentData . $css); |
|
101 | + |
|
102 | + $data = ''; |
|
103 | + foreach ($icons as $icon => $url) { |
|
104 | + $data .= "--$icon: url('$url');"; |
|
105 | + } |
|
106 | + |
|
107 | + if (strlen($data) > 0) { |
|
108 | + if (!$cachedFile) { |
|
109 | + $cachedFile = $this->folder->newFile($this->fileName); |
|
110 | + } |
|
111 | + |
|
112 | + $data = ":root { |
|
113 | 113 | $data |
114 | 114 | }"; |
115 | - $cachedFile->putContent($data); |
|
116 | - } |
|
117 | - |
|
118 | - return preg_replace($this->iconVarRE, '', $css); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get icons css file |
|
123 | - * @return ISimpleFile|boolean |
|
124 | - */ |
|
125 | - public function getCachedCSS() { |
|
126 | - try { |
|
127 | - return $this->folder->getFile($this->fileName); |
|
128 | - } catch (NotFoundException $e) { |
|
129 | - return false; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - public function injectCss() { |
|
134 | - // Only inject once |
|
135 | - foreach (\OC_Util::$headers as $header) { |
|
136 | - if ( |
|
137 | - array_key_exists('attributes', $header) && |
|
138 | - array_key_exists('href', $header['attributes']) && |
|
139 | - strpos($header['attributes']['href'], $this->fileName) !== false) { |
|
140 | - return; |
|
141 | - } |
|
142 | - } |
|
143 | - $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName]); |
|
144 | - \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true); |
|
145 | - } |
|
115 | + $cachedFile->putContent($data); |
|
116 | + } |
|
117 | + |
|
118 | + return preg_replace($this->iconVarRE, '', $css); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get icons css file |
|
123 | + * @return ISimpleFile|boolean |
|
124 | + */ |
|
125 | + public function getCachedCSS() { |
|
126 | + try { |
|
127 | + return $this->folder->getFile($this->fileName); |
|
128 | + } catch (NotFoundException $e) { |
|
129 | + return false; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + public function injectCss() { |
|
134 | + // Only inject once |
|
135 | + foreach (\OC_Util::$headers as $header) { |
|
136 | + if ( |
|
137 | + array_key_exists('attributes', $header) && |
|
138 | + array_key_exists('href', $header['attributes']) && |
|
139 | + strpos($header['attributes']['href'], $this->fileName) !== false) { |
|
140 | + return; |
|
141 | + } |
|
142 | + } |
|
143 | + $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName]); |
|
144 | + \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true); |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
148 | 148 | \ No newline at end of file |