ThemeInjectionService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming\Service;
  24. use OCA\Theming\Themes\DefaultTheme;
  25. use OCA\Theming\Util;
  26. use OCP\IConfig;
  27. use OCP\IURLGenerator;
  28. use OCP\IUserSession;
  29. class ThemeInjectionService {
  30. private IURLGenerator $urlGenerator;
  31. private ThemesService $themesService;
  32. private DefaultTheme $defaultTheme;
  33. private Util $util;
  34. private IConfig $config;
  35. private ?string $userId;
  36. public function __construct(IURLGenerator $urlGenerator,
  37. ThemesService $themesService,
  38. DefaultTheme $defaultTheme,
  39. Util $util,
  40. IConfig $config,
  41. IUserSession $userSession) {
  42. $this->urlGenerator = $urlGenerator;
  43. $this->themesService = $themesService;
  44. $this->defaultTheme = $defaultTheme;
  45. $this->util = $util;
  46. $this->config = $config;
  47. if ($userSession->getUser() !== null) {
  48. $this->userId = $userSession->getUser()->getUID();
  49. } else {
  50. $this->userId = null;
  51. }
  52. }
  53. public function injectHeaders() {
  54. $themes = $this->themesService->getThemes();
  55. $defaultTheme = $themes[$this->defaultTheme->getId()];
  56. $mediaThemes = array_filter($themes, function ($theme) {
  57. // Check if the theme provides a media query
  58. return (bool)$theme->getMediaQuery();
  59. });
  60. // Default theme fallback
  61. $this->addThemeHeader($defaultTheme->getId());
  62. // Themes applied by media queries
  63. foreach($mediaThemes as $theme) {
  64. $this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
  65. }
  66. // Themes
  67. foreach($this->themesService->getThemes() as $theme) {
  68. // Ignore default theme as already processed first
  69. if ($theme->getId() === $this->defaultTheme->getId()) {
  70. continue;
  71. }
  72. $this->addThemeHeader($theme->getId(), false);
  73. }
  74. }
  75. /**
  76. * Inject theme header into rendered page
  77. *
  78. * @param string $themeId the theme ID
  79. * @param bool $plain request the :root syntax
  80. * @param string $media media query to use in the <link> element
  81. */
  82. private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
  83. $linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
  84. 'themeId' => $themeId,
  85. 'plain' => $plain,
  86. 'v' => $this->util->getCacheBuster(),
  87. ]);
  88. \OCP\Util::addHeader('link', [
  89. 'rel' => 'stylesheet',
  90. 'media' => $media,
  91. 'href' => $linkToCSS,
  92. 'class' => 'theme'
  93. ]);
  94. }
  95. }