1
0

CSSResourceLocator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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@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 Psr\Log\LoggerInterface;
  34. class CSSResourceLocator extends ResourceLocator {
  35. /**
  36. * @param string $theme
  37. * @param array $core_map
  38. * @param array $party_map
  39. */
  40. public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) {
  41. parent::__construct($logger, $theme, $core_map, $party_map);
  42. }
  43. /**
  44. * @param string $style
  45. */
  46. public function doFind($style) {
  47. $app = substr($style, 0, strpos($style, '/'));
  48. if (strpos($style, '3rdparty') === 0
  49. && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
  50. || $this->appendIfExist($this->serverroot, $style.'.css')
  51. || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
  52. ) {
  53. return;
  54. }
  55. $style = substr($style, strpos($style, '/') + 1);
  56. $app_path = \OC_App::getAppPath($app);
  57. $app_url = \OC_App::getAppWebPath($app);
  58. if ($app_path === false && $app_url === false) {
  59. $this->logger->error('Could not find resource {resource} to load', [
  60. 'resource' => $app . '/' . $style . '.css',
  61. 'app' => 'cssresourceloader',
  62. ]);
  63. return;
  64. }
  65. // Account for the possibility of having symlinks in app path. Doing
  66. // this here instead of above as an empty argument to realpath gets
  67. // turned into cwd.
  68. $app_path = realpath($app_path);
  69. $this->append($app_path, $style.'.css', $app_url);
  70. }
  71. /**
  72. * @param string $style
  73. */
  74. public function doFindTheme($style) {
  75. $theme_dir = 'themes/'.$this->theme.'/';
  76. $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
  77. || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
  78. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
  79. }
  80. public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
  81. if (!$scss) {
  82. parent::append($root, $file, $webRoot, $throw);
  83. } else {
  84. if (!$webRoot) {
  85. $webRoot = $this->findWebRoot($root);
  86. if ($webRoot === null) {
  87. $webRoot = '';
  88. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  89. 'app' => 'lib',
  90. 'root' => $root,
  91. 'file' => $file,
  92. 'webRoot' => $webRoot,
  93. 'throw' => $throw ? 'true' : 'false'
  94. ]);
  95. if ($throw && $root === '/') {
  96. throw new ResourceNotFoundException($file, $webRoot);
  97. }
  98. }
  99. }
  100. $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file];
  101. }
  102. }
  103. }