CSSResourceLocator.php 3.4 KB

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