IconsCacher.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Template;
  25. use OCP\Files\IAppData;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\SimpleFS\ISimpleFolder;
  28. use OCP\Files\SimpleFS\ISimpleFile;
  29. use OCP\ILogger;
  30. use OCP\IURLGenerator;
  31. use OC\Files\AppData\Factory;
  32. class IconsCacher {
  33. /** @var ILogger */
  34. protected $logger;
  35. /** @var IAppData */
  36. protected $appData;
  37. /** @var ISimpleFolder */
  38. private $folder;
  39. /** @var IURLGenerator */
  40. protected $urlGenerator;
  41. /** @var string */
  42. private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+): url\(["\']([a-zA-Z0-9-_\~\/\.\?\=]+)[^;]+;/m';
  43. /** @var string */
  44. private $fileName = 'icons-vars.css';
  45. /**
  46. * @param ILogger $logger
  47. * @param Factory $appDataFactory
  48. * @param IURLGenerator $urlGenerator
  49. */
  50. public function __construct(ILogger $logger,
  51. Factory $appDataFactory,
  52. IURLGenerator $urlGenerator) {
  53. $this->logger = $logger;
  54. $this->appData = $appDataFactory->get('css');
  55. $this->urlGenerator = $urlGenerator;
  56. try {
  57. $this->folder = $this->appData->getFolder('icons');
  58. } catch (NotFoundException $e) {
  59. $this->folder = $this->appData->newFolder('icons');
  60. }
  61. }
  62. private function getIconsFromCss(string $css): array{
  63. preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
  64. $icons = [];
  65. foreach ($matches as $icon) {
  66. $icons[$icon[1]] = $icon[2];
  67. }
  68. return $icons;
  69. }
  70. /**
  71. * Parse and cache css
  72. *
  73. * @param string $css
  74. */
  75. public function setIconsCss(string $css) {
  76. $cachedFile = $this->getCachedCSS();
  77. if (!$cachedFile) {
  78. $currentData = '';
  79. } else {
  80. $currentData = $cachedFile->getContent();
  81. }
  82. // remove :root
  83. $currentData = str_replace([':root {', '}'], '', $currentData);
  84. $icons = $this->getIconsFromCss($currentData . $css);
  85. $data = '';
  86. foreach ($icons as $icon => $url) {
  87. $data .= "--$icon: url('$url');";
  88. }
  89. if (strlen($data) > 0) {
  90. if (!$cachedFile) {
  91. $cachedFile = $this->folder->newFile($this->fileName);
  92. }
  93. $data = ":root {
  94. $data
  95. }";
  96. $cachedFile->putContent($data);
  97. }
  98. return preg_replace($this->iconVarRE, '', $css);
  99. }
  100. /**
  101. * Get icons css file
  102. * @return ISimpleFile|boolean
  103. */
  104. public function getCachedCSS() {
  105. try {
  106. return $this->folder->getFile($this->fileName);
  107. } catch (NotFoundException $e) {
  108. return false;
  109. }
  110. }
  111. public function injectCss() {
  112. // Only inject once
  113. foreach (\OC_Util::$headers as $header) {
  114. if (
  115. array_key_exists('attributes', $header) &&
  116. array_key_exists('href', $header['attributes']) &&
  117. strpos($header['attributes']['href'], $this->fileName) !== false) {
  118. return;
  119. }
  120. }
  121. $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName]);
  122. \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
  123. }
  124. }