You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThemeInjectionService.php 2.7KB

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