CSSResourceLocator.php 4.7 KB

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