JSResourceLocator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, 'apps/'.$script.'.js');
  52. $found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js');
  53. if ($found) {
  54. return;
  55. }
  56. } else if ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
  57. || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
  58. || $this->appendIfExist($this->serverroot, $script.'.js')
  59. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
  60. || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
  61. || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js')
  62. || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
  63. ) {
  64. return;
  65. }
  66. $app = substr($script, 0, strpos($script, '/'));
  67. $script = substr($script, strpos($script, '/')+1);
  68. $app_path = \OC_App::getAppPath($app);
  69. $app_url = \OC_App::getAppWebPath($app);
  70. if ($app_path !== false) {
  71. // Account for the possibility of having symlinks in app path. Only
  72. // do this if $app_path is set, because an empty argument to realpath
  73. // gets turned into cwd.
  74. $app_path = realpath($app_path);
  75. }
  76. // missing translations files fill be ignored
  77. if (strpos($script, 'l10n/') === 0) {
  78. $this->appendIfExist($app_path, $script . '.js', $app_url);
  79. return;
  80. }
  81. if ($app_path === false && $app_url === false) {
  82. $this->logger->error('Could not find resource {resource} to load', [
  83. 'resource' => $app . '/' . $script . '.js',
  84. 'app' => 'jsresourceloader',
  85. ]);
  86. return;
  87. }
  88. if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
  89. $this->append($app_path, $script . '.js', $app_url);
  90. }
  91. }
  92. /**
  93. * @param string $script
  94. */
  95. public function doFindTheme($script) {
  96. }
  97. protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
  98. if (is_file($root.'/'.$file)) {
  99. if ($this->jsCombiner->process($root, $file, $app)) {
  100. $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
  101. } else {
  102. // Add all the files from the json
  103. $files = $this->jsCombiner->getContent($root, $file);
  104. $app_url = \OC_App::getAppWebPath($app);
  105. foreach ($files as $jsFile) {
  106. $this->append($root, $jsFile, $app_url);
  107. }
  108. }
  109. return true;
  110. }
  111. return false;
  112. }
  113. }