JSResourceLocator.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Template;
  27. class JSResourceLocator extends ResourceLocator {
  28. /** @var JSCombiner */
  29. protected $jsCombiner;
  30. public function __construct(\OCP\ILogger $logger, $theme, array $core_map, array $party_map, JSCombiner $JSCombiner) {
  31. parent::__construct($logger, $theme, $core_map, $party_map);
  32. $this->jsCombiner = $JSCombiner;
  33. }
  34. /**
  35. * @param string $script
  36. */
  37. public function doFind($script) {
  38. $theme_dir = 'themes/'.$this->theme.'/';
  39. if (strpos($script, '3rdparty') === 0
  40. && $this->appendIfExist($this->thirdpartyroot, $script.'.js')) {
  41. return;
  42. }
  43. if (strpos($script, '/l10n/') !== false) {
  44. // For language files we try to load them all, so themes can overwrite
  45. // single l10n strings without having to translate all of them.
  46. $found = 0;
  47. $found += $this->appendIfExist($this->serverroot, 'core/'.$script.'.js');
  48. $found += $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js');
  49. $found += $this->appendIfExist($this->serverroot, $script.'.js');
  50. $found += $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js');
  51. $found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js');
  52. if ($found) {
  53. return;
  54. }
  55. } else if ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
  56. || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
  57. || $this->appendIfExist($this->serverroot, $script.'.js')
  58. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
  59. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
  60. || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js')
  61. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
  62. ) {
  63. return;
  64. }
  65. $app = substr($script, 0, strpos($script, '/'));
  66. $script = substr($script, strpos($script, '/')+1);
  67. $app_path = \OC_App::getAppPath($app);
  68. $app_url = \OC_App::getAppWebPath($app);
  69. if ($app_path !== false) {
  70. // Account for the possibility of having symlinks in app path. Only
  71. // do this if $app_path is set, because an empty argument to realpath
  72. // gets turned into cwd.
  73. $app_path = realpath($app_path);
  74. }
  75. // missing translations files fill be ignored
  76. if (strpos($script, 'l10n/') === 0) {
  77. $this->appendIfExist($app_path, $script . '.js', $app_url);
  78. return;
  79. }
  80. if ($app_path === false && $app_url === false) {
  81. $this->logger->error('Could not find resource {resource} to load', [
  82. 'resource' => $app . '/' . $script . '.js',
  83. 'app' => 'jsresourceloader',
  84. ]);
  85. return;
  86. }
  87. if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
  88. $this->append($app_path, $script . '.js', $app_url);
  89. }
  90. }
  91. /**
  92. * @param string $script
  93. */
  94. public function doFindTheme($script) {
  95. }
  96. protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
  97. if (is_file($root.'/'.$file)) {
  98. if ($this->jsCombiner->process($root, $file, $app)) {
  99. $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
  100. } else {
  101. // Add all the files from the json
  102. $files = $this->jsCombiner->getContent($root, $file);
  103. $app_url = \OC_App::getAppWebPath($app);
  104. foreach ($files as $jsFile) {
  105. $this->append($root, $jsFile, $app_url);
  106. }
  107. }
  108. return true;
  109. }
  110. return false;
  111. }
  112. }