JSResourceLocator.php 4.2 KB

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