ResourceLocator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * @author Kyle Fazzari <kyrofa@ubuntu.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Template;
  28. abstract class ResourceLocator {
  29. protected $theme;
  30. protected $mapping;
  31. protected $serverroot;
  32. protected $thirdpartyroot;
  33. protected $webroot;
  34. protected $resources = array();
  35. /** @var \OCP\ILogger */
  36. protected $logger;
  37. /**
  38. * @param \OCP\ILogger $logger
  39. * @param string $theme
  40. * @param array $core_map
  41. * @param array $party_map
  42. */
  43. public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
  44. $this->logger = $logger;
  45. $this->theme = $theme;
  46. $this->mapping = $core_map + $party_map;
  47. $this->serverroot = key($core_map);
  48. $this->thirdpartyroot = key($party_map);
  49. $this->webroot = $this->mapping[$this->serverroot];
  50. }
  51. /**
  52. * @param string $resource
  53. */
  54. abstract public function doFind($resource);
  55. /**
  56. * @param string $resource
  57. */
  58. abstract public function doFindTheme($resource);
  59. /**
  60. * Finds the resources and adds them to the list
  61. *
  62. * @param array $resources
  63. */
  64. public function find($resources) {
  65. foreach ($resources as $resource) {
  66. try {
  67. $this->doFind($resource);
  68. } catch (ResourceNotFoundException $e) {
  69. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  70. $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  71. }
  72. }
  73. if (!empty($this->theme)) {
  74. foreach ($resources as $resource) {
  75. try {
  76. $this->doFindTheme($resource);
  77. } catch (ResourceNotFoundException $e) {
  78. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  79. $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * append the $file resource if exist at $root
  86. *
  87. * @param string $root path to check
  88. * @param string $file the filename
  89. * @param string|null $webRoot base for path, default map $root to $webRoot
  90. * @return bool True if the resource was found, false otherwise
  91. */
  92. protected function appendIfExist($root, $file, $webRoot = null) {
  93. if (is_file($root.'/'.$file)) {
  94. $this->append($root, $file, $webRoot, false);
  95. return true;
  96. }
  97. return false;
  98. }
  99. /**
  100. * Attempt to find the webRoot
  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. * @param string $root
  113. * @return string|null The web root or null on failure
  114. */
  115. protected function findWebRoot($root) {
  116. $webRoot = null;
  117. $tmpRoot = $root;
  118. while ($webRoot === null) {
  119. if (isset($this->mapping[$tmpRoot])) {
  120. $webRoot = $this->mapping[$tmpRoot];
  121. break;
  122. }
  123. if ($tmpRoot === '/') {
  124. break;
  125. }
  126. $tmpRoot = dirname($tmpRoot);
  127. }
  128. if ($webRoot === null) {
  129. $realpath = realpath($root);
  130. if ($realpath && ($realpath !== $root)) {
  131. return $this->findWebRoot($realpath);
  132. }
  133. }
  134. return $webRoot;
  135. }
  136. /**
  137. * append the $file resource at $root
  138. *
  139. * @param string $root path to check
  140. * @param string $file the filename
  141. * @param string|null $webRoot base for path, default map $root to $webRoot
  142. * @param bool $throw Throw an exception, when the route does not exist
  143. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  144. */
  145. protected function append($root, $file, $webRoot = null, $throw = true) {
  146. if (!is_string($root)) {
  147. if ($throw) {
  148. throw new ResourceNotFoundException($file, $webRoot);
  149. }
  150. return;
  151. }
  152. if (!$webRoot) {
  153. $webRoot = $this->findWebRoot($root);
  154. if ($webRoot === null) {
  155. $webRoot = '';
  156. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  157. 'app' => 'lib',
  158. 'root' => $root,
  159. 'file' => $file,
  160. 'webRoot' => $webRoot,
  161. 'throw' => $throw ? 'true' : 'false'
  162. ]);
  163. }
  164. }
  165. $this->resources[] = array($root, $webRoot, $file);
  166. if ($throw && !is_file($root . '/' . $file)) {
  167. throw new ResourceNotFoundException($file, $webRoot);
  168. }
  169. }
  170. /**
  171. * Returns the list of all resources that should be loaded
  172. * @return array
  173. */
  174. public function getResources() {
  175. return $this->resources;
  176. }
  177. }