CSSResourceLocator.php 3.4 KB

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