JSResourceLocator.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 Kyle Fazzari <kyrofa@ubuntu.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Template;
  29. use OCP\App\AppPathNotFoundException;
  30. use OCP\App\IAppManager;
  31. use Psr\Log\LoggerInterface;
  32. class JSResourceLocator extends ResourceLocator {
  33. protected JSCombiner $jsCombiner;
  34. protected IAppManager $appManager;
  35. public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) {
  36. parent::__construct($logger);
  37. $this->jsCombiner = $JSCombiner;
  38. $this->appManager = $appManager;
  39. }
  40. /**
  41. * @param string $script
  42. */
  43. public function doFind($script) {
  44. $theme_dir = 'themes/'.$this->theme.'/';
  45. // Extracting the appId and the script file name
  46. $app = substr($script, 0, strpos($script, '/'));
  47. $scriptName = basename($script);
  48. if (str_contains($script, '/l10n/')) {
  49. // For language files we try to load them all, so themes can overwrite
  50. // single l10n strings without having to translate all of them.
  51. $found = 0;
  52. $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script);
  53. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script);
  54. $found += $this->appendScriptIfExist($this->serverroot, $script);
  55. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script);
  56. foreach (\OC::$APPSROOTS as $appRoot) {
  57. $dirName = basename($appRoot['path']);
  58. $rootPath = dirname($appRoot['path']);
  59. $found += $this->appendScriptIfExist($rootPath, $dirName.'/'.$script);
  60. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$dirName.'/'.$script);
  61. }
  62. if ($found) {
  63. return;
  64. }
  65. } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script)
  66. || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script)
  67. || $this->appendScriptIfExist($this->serverroot, $script)
  68. || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName")
  69. || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName")
  70. || $this->appendScriptIfExist($this->serverroot, 'apps/'.$script)
  71. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
  72. || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script)
  73. || $this->appendScriptIfExist($this->serverroot, 'core/'.$script)
  74. || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName")
  75. || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName")))
  76. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
  77. ) {
  78. return;
  79. }
  80. $script = substr($script, strpos($script, '/') + 1);
  81. $app_url = null;
  82. try {
  83. $app_url = $this->appManager->getAppWebPath($app);
  84. } catch (AppPathNotFoundException) {
  85. // pass
  86. }
  87. try {
  88. $app_path = $this->appManager->getAppPath($app);
  89. // Account for the possibility of having symlinks in app path. Only
  90. // do this if $app_path is set, because an empty argument to realpath
  91. // gets turned into cwd.
  92. $app_path = realpath($app_path);
  93. // check combined files
  94. if (!str_starts_with($script, 'l10n/') && $this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
  95. return;
  96. }
  97. // fallback to plain file location
  98. if ($this->appendScriptIfExist($app_path, $script, $app_url)) {
  99. return;
  100. }
  101. } catch (AppPathNotFoundException) {
  102. // pass (general error handling happens below)
  103. }
  104. // missing translations files will be ignored
  105. if (str_starts_with($script, 'l10n/')) {
  106. return;
  107. }
  108. $this->logger->error('Could not find resource {resource} to load', [
  109. 'resource' => $app . '/' . $script . '.js',
  110. 'app' => 'jsresourceloader',
  111. ]);
  112. }
  113. /**
  114. * @param string $script
  115. */
  116. public function doFindTheme($script) {
  117. }
  118. /**
  119. * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`)
  120. * @see appendIfExist()
  121. */
  122. protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) {
  123. if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) {
  124. return $this->appendIfExist($root, $file . '.js', $webRoot);
  125. }
  126. return true;
  127. }
  128. protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
  129. if (is_file($root.'/'.$file)) {
  130. if ($this->jsCombiner->process($root, $file, $app)) {
  131. $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
  132. } else {
  133. // Add all the files from the json
  134. $files = $this->jsCombiner->getContent($root, $file);
  135. $app_url = null;
  136. try {
  137. $app_url = $this->appManager->getAppWebPath($app);
  138. } catch (AppPathNotFoundException) {
  139. // pass
  140. }
  141. foreach ($files as $jsFile) {
  142. $this->append($root, $jsFile, $app_url);
  143. }
  144. }
  145. return true;
  146. }
  147. return false;
  148. }
  149. }