You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.4 KiB

3 years ago
  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. class JSResourceLocator extends ResourceLocator {
  30. /** @var JSCombiner */
  31. protected $jsCombiner;
  32. public function __construct(\OCP\ILogger $logger, $theme, array $core_map, array $party_map, JSCombiner $JSCombiner) {
  33. parent::__construct($logger, $theme, $core_map, $party_map);
  34. $this->jsCombiner = $JSCombiner;
  35. }
  36. /**
  37. * @param string $script
  38. */
  39. public function doFind($script) {
  40. $theme_dir = 'themes/'.$this->theme.'/';
  41. if (strpos($script, '3rdparty') === 0
  42. && $this->appendIfExist($this->thirdpartyroot, $script.'.js')) {
  43. return;
  44. }
  45. if (strpos($script, '/l10n/') !== false) {
  46. // For language files we try to load them all, so themes can overwrite
  47. // single l10n strings without having to translate all of them.
  48. $found = 0;
  49. $found += $this->appendIfExist($this->serverroot, 'core/'.$script.'.js');
  50. $found += $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js');
  51. $found += $this->appendIfExist($this->serverroot, $script.'.js');
  52. $found += $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js');
  53. $found += $this->appendIfExist($this->serverroot, 'apps/'.$script.'.js');
  54. $found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js');
  55. if ($found) {
  56. return;
  57. }
  58. } elseif ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
  59. || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
  60. || $this->appendIfExist($this->serverroot, $script.'.js')
  61. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
  62. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
  63. || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js')
  64. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
  65. ) {
  66. return;
  67. }
  68. $app = substr($script, 0, strpos($script, '/'));
  69. $script = substr($script, strpos($script, '/')+1);
  70. $app_path = \OC_App::getAppPath($app);
  71. $app_url = \OC_App::getAppWebPath($app);
  72. if ($app_path !== false) {
  73. // Account for the possibility of having symlinks in app path. Only
  74. // do this if $app_path is set, because an empty argument to realpath
  75. // gets turned into cwd.
  76. $app_path = realpath($app_path);
  77. }
  78. // missing translations files fill be ignored
  79. if (strpos($script, 'l10n/') === 0) {
  80. $this->appendIfExist($app_path, $script . '.js', $app_url);
  81. return;
  82. }
  83. if ($app_path === false && $app_url === false) {
  84. $this->logger->error('Could not find resource {resource} to load', [
  85. 'resource' => $app . '/' . $script . '.js',
  86. 'app' => 'jsresourceloader',
  87. ]);
  88. return;
  89. }
  90. if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
  91. $this->append($app_path, $script . '.js', $app_url);
  92. }
  93. }
  94. /**
  95. * @param string $script
  96. */
  97. public function doFindTheme($script) {
  98. }
  99. protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
  100. if (is_file($root.'/'.$file)) {
  101. if ($this->jsCombiner->process($root, $file, $app)) {
  102. $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
  103. } else {
  104. // Add all the files from the json
  105. $files = $this->jsCombiner->getContent($root, $file);
  106. $app_url = \OC_App::getAppWebPath($app);
  107. foreach ($files as $jsFile) {
  108. $this->append($root, $jsFile, $app_url);
  109. }
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. }