1
0

ThemeInjectionService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\AppInfo\Application;
  25. use OCA\Theming\Themes\DefaultTheme;
  26. use OCA\Theming\Util;
  27. use OCP\IConfig;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserSession;
  30. class ThemeInjectionService {
  31. private IURLGenerator $urlGenerator;
  32. private ThemesService $themesService;
  33. private DefaultTheme $defaultTheme;
  34. private Util $util;
  35. private IConfig $config;
  36. private ?string $userId;
  37. public function __construct(IURLGenerator $urlGenerator,
  38. ThemesService $themesService,
  39. DefaultTheme $defaultTheme,
  40. Util $util,
  41. IConfig $config,
  42. IUserSession $userSession) {
  43. $this->urlGenerator = $urlGenerator;
  44. $this->themesService = $themesService;
  45. $this->defaultTheme = $defaultTheme;
  46. $this->util = $util;
  47. $this->config = $config;
  48. if ($userSession->getUser() !== null) {
  49. $this->userId = $userSession->getUser()->getUID();
  50. } else {
  51. $this->userId = null;
  52. }
  53. }
  54. public function injectHeaders() {
  55. $themes = $this->themesService->getThemes();
  56. $defaultTheme = $themes[$this->defaultTheme->getId()];
  57. $mediaThemes = array_filter($themes, function($theme) {
  58. // Check if the theme provides a media query
  59. return (bool)$theme->getMediaQuery();
  60. });
  61. // Default theme fallback
  62. $this->addThemeHeader($defaultTheme->getId());
  63. // Themes applied by media queries
  64. foreach($mediaThemes as $theme) {
  65. $this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
  66. }
  67. // Themes
  68. foreach($this->themesService->getThemes() as $theme) {
  69. // Ignore default theme as already processed first
  70. if ($theme->getId() === $this->defaultTheme->getId()) {
  71. continue;
  72. }
  73. $this->addThemeHeader($theme->getId(), false);
  74. }
  75. }
  76. /**
  77. * Inject theme header into rendered page
  78. *
  79. * @param string $themeId the theme ID
  80. * @param bool $plain request the :root syntax
  81. * @param string $media media query to use in the <link> element
  82. */
  83. private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
  84. $linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
  85. 'themeId' => $themeId,
  86. 'plain' => $plain,
  87. 'v' => $this->util->getCacheBuster(),
  88. ]);
  89. \OCP\Util::addHeader('link', [
  90. 'rel' => 'stylesheet',
  91. 'media' => $media,
  92. 'href' => $linkToCSS,
  93. 'class' => 'theme'
  94. ]);
  95. }
  96. }