ResourceLocator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Template;
  27. abstract class ResourceLocator {
  28. protected $theme;
  29. protected $mapping;
  30. protected $serverroot;
  31. protected $thirdpartyroot;
  32. protected $webroot;
  33. protected $resources = array();
  34. /** @var \OCP\ILogger */
  35. protected $logger;
  36. /**
  37. * @param \OCP\ILogger $logger
  38. * @param string $theme
  39. * @param array $core_map
  40. * @param array $party_map
  41. */
  42. public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
  43. $this->logger = $logger;
  44. $this->theme = $theme;
  45. $this->mapping = $core_map + $party_map;
  46. $this->serverroot = key($core_map);
  47. $this->thirdpartyroot = key($party_map);
  48. $this->webroot = $this->mapping[$this->serverroot];
  49. }
  50. /**
  51. * @param string $resource
  52. */
  53. abstract public function doFind($resource);
  54. /**
  55. * @param string $resource
  56. */
  57. abstract public function doFindTheme($resource);
  58. /**
  59. * Finds the resources and adds them to the list
  60. *
  61. * @param array $resources
  62. */
  63. public function find($resources) {
  64. foreach ($resources as $resource) {
  65. try {
  66. $this->doFind($resource);
  67. } catch (ResourceNotFoundException $e) {
  68. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  69. $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  70. }
  71. }
  72. if (!empty($this->theme)) {
  73. foreach ($resources as $resource) {
  74. try {
  75. $this->doFindTheme($resource);
  76. } catch (ResourceNotFoundException $e) {
  77. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  78. $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * append the $file resource if exist at $root
  85. *
  86. * @param string $root path to check
  87. * @param string $file the filename
  88. * @param string|null $webRoot base for path, default map $root to $webRoot
  89. * @return bool True if the resource was found, false otherwise
  90. */
  91. protected function appendIfExist($root, $file, $webRoot = null) {
  92. if (is_file($root.'/'.$file)) {
  93. $this->append($root, $file, $webRoot, false);
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * append the $file resource at $root
  100. *
  101. * @param string $root path to check
  102. * @param string $file the filename
  103. * @param string|null $webRoot base for path, default map $root to $webRoot
  104. * @param bool $throw Throw an exception, when the route does not exist
  105. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  106. */
  107. protected function append($root, $file, $webRoot = null, $throw = true) {
  108. if (!$webRoot) {
  109. $tmpRoot = $root;
  110. /*
  111. * traverse the potential web roots upwards in the path
  112. *
  113. * example:
  114. * - root: /srv/www/apps/myapp
  115. * - available mappings: ['/srv/www']
  116. *
  117. * First we check if a mapping for /srv/www/apps/myapp is available,
  118. * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
  119. * valid web root
  120. */
  121. do {
  122. if (isset($this->mapping[$tmpRoot])) {
  123. $webRoot = $this->mapping[$tmpRoot];
  124. break;
  125. }
  126. if ($tmpRoot === '/') {
  127. $webRoot = '';
  128. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  129. 'app' => 'lib',
  130. 'root' => $root,
  131. 'file' => $file,
  132. 'webRoot' => $webRoot,
  133. 'throw' => $throw ? 'true' : 'false'
  134. ]);
  135. break;
  136. }
  137. $tmpRoot = dirname($tmpRoot);
  138. } while(true);
  139. }
  140. $this->resources[] = array($root, $webRoot, $file);
  141. if ($throw && !is_file($root . '/' . $file)) {
  142. throw new ResourceNotFoundException($file, $webRoot);
  143. }
  144. }
  145. /**
  146. * Returns the list of all resources that should be loaded
  147. * @return array
  148. */
  149. public function getResources() {
  150. return $this->resources;
  151. }
  152. }