1
0

JSResourceLocator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Get the app root path
  49. $appRoot = $this->serverroot . '/apps/';
  50. $appWebRoot = null;
  51. try {
  52. // We need the dir name as getAppPath appends the appid
  53. $appRoot = dirname($this->appManager->getAppPath($app));
  54. // Only do this if $app_path is set, because an empty argument to realpath gets turned into cwd.
  55. if ($appRoot) {
  56. // Handle symlinks
  57. $appRoot = realpath($appRoot);
  58. }
  59. // Get the app webroot
  60. $appWebRoot = dirname($this->appManager->getAppWebPath($app));
  61. } catch (AppPathNotFoundException $e) {
  62. // ignore
  63. }
  64. if (str_contains($script, '/l10n/')) {
  65. // For language files we try to load them all, so themes can overwrite
  66. // single l10n strings without having to translate all of them.
  67. $found = 0;
  68. $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script);
  69. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script);
  70. $found += $this->appendScriptIfExist($this->serverroot, $script);
  71. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script);
  72. $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot);
  73. $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script);
  74. if ($found) {
  75. return;
  76. }
  77. } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script)
  78. || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script)
  79. || $this->appendScriptIfExist($this->serverroot, $script)
  80. || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName")
  81. || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName")
  82. || $this->appendScriptIfExist($appRoot, $script, $appWebRoot)
  83. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
  84. || $this->cacheAndAppendCombineJsonIfExist($appRoot, $script.'.json', $appWebRoot)
  85. || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script)
  86. || $this->appendScriptIfExist($this->serverroot, 'core/'.$script)
  87. || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName")
  88. || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName")))
  89. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
  90. ) {
  91. return;
  92. }
  93. // missing translations files will be ignored
  94. if (str_contains($script, '/l10n/')) {
  95. return;
  96. }
  97. $this->logger->error('Could not find resource {resource} to load', [
  98. 'resource' => $script . '.js',
  99. 'app' => 'jsresourceloader',
  100. ]);
  101. }
  102. /**
  103. * @param string $script
  104. */
  105. public function doFindTheme($script) {
  106. }
  107. /**
  108. * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`)
  109. * @see appendIfExist()
  110. */
  111. protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null) {
  112. if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) {
  113. return $this->appendIfExist($root, $file . '.js', $webRoot);
  114. }
  115. return true;
  116. }
  117. protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
  118. if (is_file($root.'/'.$file)) {
  119. if ($this->jsCombiner->process($root, $file, $app)) {
  120. $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
  121. } else {
  122. // Add all the files from the json
  123. $files = $this->jsCombiner->getContent($root, $file);
  124. $app_url = null;
  125. try {
  126. $app_url = $this->appManager->getAppWebPath($app);
  127. } catch (AppPathNotFoundException) {
  128. // pass
  129. }
  130. foreach ($files as $jsFile) {
  131. $this->append($root, $jsFile, $app_url);
  132. }
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. }