1
0

ResourceLocator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  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 Psr\Log\LoggerInterface;
  32. abstract class ResourceLocator {
  33. protected $theme;
  34. protected $mapping;
  35. protected $serverroot;
  36. protected $thirdpartyroot;
  37. protected $webroot;
  38. protected $resources = [];
  39. protected LoggerInterface $logger;
  40. /**
  41. * @param string $theme
  42. * @param array $core_map
  43. * @param array $party_map
  44. */
  45. public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) {
  46. $this->logger = $logger;
  47. $this->theme = $theme;
  48. $this->mapping = $core_map + $party_map;
  49. $this->serverroot = key($core_map);
  50. $this->thirdpartyroot = key($party_map);
  51. $this->webroot = $this->mapping[$this->serverroot];
  52. }
  53. /**
  54. * @param string $resource
  55. */
  56. abstract public function doFind($resource);
  57. /**
  58. * @param string $resource
  59. */
  60. abstract public function doFindTheme($resource);
  61. /**
  62. * Finds the resources and adds them to the list
  63. *
  64. * @param array $resources
  65. */
  66. public function find($resources) {
  67. foreach ($resources as $resource) {
  68. try {
  69. $this->doFind($resource);
  70. } catch (ResourceNotFoundException $e) {
  71. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  72. $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  73. }
  74. }
  75. if (!empty($this->theme)) {
  76. foreach ($resources as $resource) {
  77. try {
  78. $this->doFindTheme($resource);
  79. } catch (ResourceNotFoundException $e) {
  80. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  81. $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * append the $file resource if exist at $root
  88. *
  89. * @param string $root path to check
  90. * @param string $file the filename
  91. * @param string|null $webRoot base for path, default map $root to $webRoot
  92. * @return bool True if the resource was found, false otherwise
  93. */
  94. protected function appendIfExist($root, $file, $webRoot = null) {
  95. if ($root !== false && is_file($root.'/'.$file)) {
  96. $this->append($root, $file, $webRoot, false);
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Attempt to find the webRoot
  103. *
  104. * traverse the potential web roots upwards in the path
  105. *
  106. * example:
  107. * - root: /srv/www/apps/myapp
  108. * - available mappings: ['/srv/www']
  109. *
  110. * First we check if a mapping for /srv/www/apps/myapp is available,
  111. * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
  112. * valid web root
  113. *
  114. * @param string $root
  115. * @return string|null The web root or null on failure
  116. */
  117. protected function findWebRoot($root) {
  118. $webRoot = null;
  119. $tmpRoot = $root;
  120. while ($webRoot === null) {
  121. if (isset($this->mapping[$tmpRoot])) {
  122. $webRoot = $this->mapping[$tmpRoot];
  123. break;
  124. }
  125. if ($tmpRoot === '/') {
  126. break;
  127. }
  128. $tmpRoot = dirname($tmpRoot);
  129. }
  130. if ($webRoot === null) {
  131. $realpath = realpath($root);
  132. if ($realpath && ($realpath !== $root)) {
  133. return $this->findWebRoot($realpath);
  134. }
  135. }
  136. return $webRoot;
  137. }
  138. /**
  139. * append the $file resource at $root
  140. *
  141. * @param string $root path to check
  142. * @param string $file the filename
  143. * @param string|null $webRoot base for path, default map $root to $webRoot
  144. * @param bool $throw Throw an exception, when the route does not exist
  145. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  146. */
  147. protected function append($root, $file, $webRoot = null, $throw = true) {
  148. if (!is_string($root)) {
  149. if ($throw) {
  150. throw new ResourceNotFoundException($file, $webRoot);
  151. }
  152. return;
  153. }
  154. if (!$webRoot) {
  155. $webRoot = $this->findWebRoot($root);
  156. if ($webRoot === null) {
  157. $webRoot = '';
  158. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  159. 'app' => 'lib',
  160. 'root' => $root,
  161. 'file' => $file,
  162. 'webRoot' => $webRoot,
  163. 'throw' => $throw ? 'true' : 'false'
  164. ]);
  165. }
  166. }
  167. $this->resources[] = [$root, $webRoot, $file];
  168. if ($throw && !is_file($root . '/' . $file)) {
  169. throw new ResourceNotFoundException($file, $webRoot);
  170. }
  171. }
  172. /**
  173. * Returns the list of all resources that should be loaded
  174. * @return array
  175. */
  176. public function getResources() {
  177. return $this->resources;
  178. }
  179. }