JSResourceLocator.php 5.0 KB

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