CSSResourceLocator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Template;
  26. use OCP\ILogger;
  27. class CSSResourceLocator extends ResourceLocator {
  28. /** @var SCSSCacher */
  29. protected $scssCacher;
  30. /**
  31. * @param ILogger $logger
  32. * @param string $theme
  33. * @param array $core_map
  34. * @param array $party_map
  35. * @param SCSSCacher $scssCacher
  36. */
  37. public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) {
  38. $this->scssCacher = $scssCacher;
  39. parent::__construct($logger, $theme, $core_map, $party_map);
  40. }
  41. /**
  42. * @param string $style
  43. */
  44. public function doFind($style) {
  45. $app = substr($style, 0, strpos($style, '/'));
  46. if (strpos($style, '3rdparty') === 0
  47. && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
  48. || $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app)
  49. || $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss')
  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(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) {
  59. $this->append($app_path, $style.'.css', $app_url);
  60. }
  61. }
  62. /**
  63. * @param string $style
  64. */
  65. public function doFindTheme($style) {
  66. $theme_dir = 'themes/'.$this->theme.'/';
  67. $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
  68. || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
  69. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
  70. }
  71. /**
  72. * cache and append the scss $file if exist at $root
  73. *
  74. * @param string $root path to check
  75. * @param string $file the filename
  76. * @return bool True if the resource was found and cached, false otherwise
  77. */
  78. protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') {
  79. if (is_file($root.'/'.$file)) {
  80. if($this->scssCacher !== null) {
  81. if($this->scssCacher->process($root, $file, $app)) {
  82. $this->append($root, $this->scssCacher->getCachedSCSS($app, $file), false, true, true);
  83. return true;
  84. } else {
  85. $this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']);
  86. return false;
  87. }
  88. } else {
  89. $this->logger->debug('Scss is disabled for '.$root.'/'.$file.', ignoring', ['app' => 'core']);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
  96. if (!$scss) {
  97. parent::append($root, $file, $webRoot, $throw);
  98. } else {
  99. if (!$webRoot) {
  100. $tmpRoot = $root;
  101. /*
  102. * traverse the potential web roots upwards in the path
  103. *
  104. * example:
  105. * - root: /srv/www/apps/myapp
  106. * - available mappings: ['/srv/www']
  107. *
  108. * First we check if a mapping for /srv/www/apps/myapp is available,
  109. * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
  110. * valid web root
  111. */
  112. do {
  113. if (isset($this->mapping[$tmpRoot])) {
  114. $webRoot = $this->mapping[$tmpRoot];
  115. break;
  116. }
  117. if ($tmpRoot === '/') {
  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. break;
  127. }
  128. $tmpRoot = dirname($tmpRoot);
  129. } while(true);
  130. }
  131. if ($throw && $tmpRoot === '/') {
  132. throw new ResourceNotFoundException($file, $webRoot);
  133. }
  134. $this->resources[] = array($tmpRoot, $webRoot, $file);
  135. }
  136. }
  137. }