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.

151 lines
4.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Axel Helmert <axel.helmert@luka.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Kyle Fazzari <kyrofa@ubuntu.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author tux-rampage <tux-rampage@users.noreply.github.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Template;
  33. use OCP\ILogger;
  34. class CSSResourceLocator extends ResourceLocator {
  35. /** @var SCSSCacher */
  36. protected $scssCacher;
  37. /**
  38. * @param ILogger $logger
  39. * @param string $theme
  40. * @param array $core_map
  41. * @param array $party_map
  42. * @param SCSSCacher $scssCacher
  43. */
  44. public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) {
  45. $this->scssCacher = $scssCacher;
  46. parent::__construct($logger, $theme, $core_map, $party_map);
  47. }
  48. /**
  49. * @param string $style
  50. */
  51. public function doFind($style) {
  52. $app = substr($style, 0, strpos($style, '/'));
  53. if (strpos($style, '3rdparty') === 0
  54. && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
  55. || $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app)
  56. || $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss')
  57. || $this->appendIfExist($this->serverroot, $style.'.css')
  58. || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
  59. ) {
  60. return;
  61. }
  62. $style = substr($style, strpos($style, '/')+1);
  63. $app_path = \OC_App::getAppPath($app);
  64. $app_url = \OC_App::getAppWebPath($app);
  65. if ($app_path === false && $app_url === false) {
  66. $this->logger->error('Could not find resource {resource} to load', [
  67. 'resource' => $app . '/' . $style . '.css',
  68. 'app' => 'cssresourceloader',
  69. ]);
  70. return;
  71. }
  72. // Account for the possibility of having symlinks in app path. Doing
  73. // this here instead of above as an empty argument to realpath gets
  74. // turned into cwd.
  75. $app_path = realpath($app_path);
  76. if (!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) {
  77. $this->append($app_path, $style.'.css', $app_url);
  78. }
  79. }
  80. /**
  81. * @param string $style
  82. */
  83. public function doFindTheme($style) {
  84. $theme_dir = 'themes/'.$this->theme.'/';
  85. $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
  86. || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
  87. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
  88. }
  89. /**
  90. * cache and append the scss $file if exist at $root
  91. *
  92. * @param string $root path to check
  93. * @param string $file the filename
  94. * @return bool True if the resource was found and cached, false otherwise
  95. */
  96. protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') {
  97. if (is_file($root.'/'.$file)) {
  98. if ($this->scssCacher !== null) {
  99. if ($this->scssCacher->process($root, $file, $app)) {
  100. $this->append($this->serverroot, $this->scssCacher->getCachedSCSS($app, $file), \OC::$WEBROOT, true, true);
  101. return true;
  102. } else {
  103. $this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']);
  104. return false;
  105. }
  106. } else {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
  113. if (!$scss) {
  114. parent::append($root, $file, $webRoot, $throw);
  115. } else {
  116. if (!$webRoot) {
  117. $webRoot = $this->findWebRoot($root);
  118. if ($webRoot === null) {
  119. $webRoot = '';
  120. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  121. 'app' => 'lib',
  122. 'root' => $root,
  123. 'file' => $file,
  124. 'webRoot' => $webRoot,
  125. 'throw' => $throw ? 'true' : 'false'
  126. ]);
  127. if ($throw && $root === '/') {
  128. throw new ResourceNotFoundException($file, $webRoot);
  129. }
  130. }
  131. }
  132. $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file];
  133. }
  134. }
  135. }