123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace OC\Template;
- use OCP\App\AppPathNotFoundException;
- use OCP\App\IAppManager;
- use Psr\Log\LoggerInterface;
- class JSResourceLocator extends ResourceLocator {
- protected JSCombiner $jsCombiner;
- protected IAppManager $appManager;
- public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) {
- parent::__construct($logger);
- $this->jsCombiner = $JSCombiner;
- $this->appManager = $appManager;
- }
-
- public function doFind($script) {
- $theme_dir = 'themes/'.$this->theme.'/';
-
- $app = substr($script, 0, strpos($script, '/'));
- $scriptName = basename($script);
-
- $appRoot = $this->serverroot . '/apps/';
- $appWebRoot = null;
- try {
-
- $appRoot = dirname($this->appManager->getAppPath($app));
-
- if ($appRoot) {
-
- $appRoot = realpath($appRoot);
- }
-
- $appWebRoot = dirname($this->appManager->getAppWebPath($app));
- } catch (AppPathNotFoundException $e) {
-
- }
- if (str_contains($script, '/l10n/')) {
-
-
- $found = 0;
- $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script);
- $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script);
- $found += $this->appendScriptIfExist($this->serverroot, $script);
- $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script);
- $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot);
- $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script);
- if ($found) {
- return;
- }
- } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script)
- || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script)
- || $this->appendScriptIfExist($this->serverroot, $script)
- || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName")
- || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName")
- || $this->appendScriptIfExist($appRoot, $script, $appWebRoot)
- || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
- || $this->cacheAndAppendCombineJsonIfExist($appRoot, $script.'.json', $appWebRoot)
- || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script)
- || $this->appendScriptIfExist($this->serverroot, 'core/'.$script)
- || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName")
- || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName")))
- || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
- ) {
- return;
- }
-
- if (str_contains($script, '/l10n/')) {
- return;
- }
- $this->logger->error('Could not find resource {resource} to load', [
- 'resource' => $script . '.js',
- 'app' => 'jsresourceloader',
- ]);
- }
-
- public function doFindTheme($script) {
- }
-
- protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null) {
- if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) {
- return $this->appendIfExist($root, $file . '.js', $webRoot);
- }
- return true;
- }
- protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
- if (is_file($root.'/'.$file)) {
- if ($this->jsCombiner->process($root, $file, $app)) {
- $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
- } else {
-
- $files = $this->jsCombiner->getContent($root, $file);
- $app_url = null;
- try {
- $app_url = $this->appManager->getAppWebPath($app);
- } catch (AppPathNotFoundException) {
-
- }
- foreach ($files as $jsFile) {
- $this->append($root, $jsFile, $app_url);
- }
- }
- return true;
- }
- return false;
- }
- }
|