resourcelocator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. abstract class ResourceLocator {
  27. protected $theme;
  28. protected $mapping;
  29. protected $serverroot;
  30. protected $thirdpartyroot;
  31. protected $webroot;
  32. protected $resources = array();
  33. /** @var \OCP\ILogger */
  34. protected $logger;
  35. /**
  36. * @param \OCP\ILogger $logger
  37. * @param string $theme
  38. * @param array $core_map
  39. * @param array $party_map
  40. */
  41. public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
  42. $this->logger = $logger;
  43. $this->theme = $theme;
  44. $this->mapping = $core_map + $party_map;
  45. $this->serverroot = key($core_map);
  46. $this->thirdpartyroot = key($party_map);
  47. $this->webroot = $this->mapping[$this->serverroot];
  48. }
  49. /**
  50. * @param string $resource
  51. */
  52. abstract public function doFind($resource);
  53. /**
  54. * @param string $resource
  55. */
  56. abstract public function doFindTheme($resource);
  57. /**
  58. * Finds the resources and adds them to the list
  59. *
  60. * @param array $resources
  61. */
  62. public function find($resources) {
  63. foreach ($resources as $resource) {
  64. try {
  65. $this->doFind($resource);
  66. } catch (ResourceNotFoundException $e) {
  67. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  68. $this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  69. }
  70. }
  71. if (!empty($this->theme)) {
  72. foreach ($resources as $resource) {
  73. try {
  74. $this->doFindTheme($resource);
  75. } catch (ResourceNotFoundException $e) {
  76. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  77. $this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * append the $file resource if exist at $root
  84. *
  85. * @param string $root path to check
  86. * @param string $file the filename
  87. * @param string|null $webRoot base for path, default map $root to $webRoot
  88. * @return bool True if the resource was found, false otherwise
  89. */
  90. protected function appendIfExist($root, $file, $webRoot = null) {
  91. if (is_file($root.'/'.$file)) {
  92. $this->append($root, $file, $webRoot, false);
  93. return true;
  94. }
  95. return false;
  96. }
  97. /**
  98. * append the $file resource at $root
  99. *
  100. * @param string $root path to check
  101. * @param string $file the filename
  102. * @param string|null $webRoot base for path, default map $root to $webRoot
  103. * @param bool $throw Throw an exception, when the route does not exist
  104. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  105. */
  106. protected function append($root, $file, $webRoot = null, $throw = true) {
  107. if (!$webRoot) {
  108. $webRoot = $this->mapping[$root];
  109. }
  110. $this->resources[] = array($root, $webRoot, $file);
  111. if ($throw && !is_file($root . '/' . $file)) {
  112. throw new ResourceNotFoundException($file, $webRoot);
  113. }
  114. }
  115. /**
  116. * Returns the list of all resources that should be loaded
  117. * @return array
  118. */
  119. public function getResources() {
  120. return $this->resources;
  121. }
  122. }