CSSResourceLocator.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Template;
  8. use Psr\Log\LoggerInterface;
  9. class CSSResourceLocator extends ResourceLocator {
  10. public function __construct(LoggerInterface $logger) {
  11. parent::__construct($logger);
  12. }
  13. /**
  14. * @param string $style
  15. */
  16. public function doFind($style) {
  17. $app = substr($style, 0, strpos($style, '/'));
  18. if ($this->appendIfExist($this->serverroot, $style.'.css')
  19. || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
  20. ) {
  21. return;
  22. }
  23. $style = substr($style, strpos($style, '/') + 1);
  24. $app_path = \OC_App::getAppPath($app);
  25. $app_url = \OC_App::getAppWebPath($app);
  26. if ($app_path === false && $app_url === false) {
  27. $this->logger->error('Could not find resource {resource} to load', [
  28. 'resource' => $app . '/' . $style . '.css',
  29. 'app' => 'cssresourceloader',
  30. ]);
  31. return;
  32. }
  33. // Account for the possibility of having symlinks in app path. Doing
  34. // this here instead of above as an empty argument to realpath gets
  35. // turned into cwd.
  36. $app_path = realpath($app_path);
  37. $this->append($app_path, $style.'.css', $app_url);
  38. }
  39. /**
  40. * @param string $style
  41. */
  42. public function doFindTheme($style) {
  43. $theme_dir = 'themes/'.$this->theme.'/';
  44. $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
  45. || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
  46. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
  47. }
  48. public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
  49. if (!$scss) {
  50. parent::append($root, $file, $webRoot, $throw);
  51. } else {
  52. if (!$webRoot) {
  53. $webRoot = $this->findWebRoot($root);
  54. if ($webRoot === null) {
  55. $webRoot = '';
  56. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  57. 'app' => 'lib',
  58. 'root' => $root,
  59. 'file' => $file,
  60. 'webRoot' => $webRoot,
  61. 'throw' => $throw ? 'true' : 'false'
  62. ]);
  63. if ($throw && $root === '/') {
  64. throw new ResourceNotFoundException($file, $webRoot);
  65. }
  66. }
  67. }
  68. $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file];
  69. }
  70. }
  71. }