@@ -12,122 +12,122 @@ |
||
12 | 12 | use Psr\Log\LoggerInterface; |
13 | 13 | |
14 | 14 | class JSResourceLocator extends ResourceLocator { |
15 | - protected JSCombiner $jsCombiner; |
|
16 | - protected IAppManager $appManager; |
|
17 | - |
|
18 | - public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) { |
|
19 | - parent::__construct($logger); |
|
20 | - |
|
21 | - $this->jsCombiner = $JSCombiner; |
|
22 | - $this->appManager = $appManager; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @param string $script |
|
27 | - */ |
|
28 | - public function doFind($script) { |
|
29 | - $theme_dir = 'themes/' . $this->theme . '/'; |
|
30 | - |
|
31 | - // Extracting the appId and the script file name |
|
32 | - $app = substr($script, 0, strpos($script, '/')); |
|
33 | - $scriptName = basename($script); |
|
34 | - // Get the app root path |
|
35 | - $appRoot = $this->serverroot . '/apps/'; |
|
36 | - $appWebRoot = null; |
|
37 | - try { |
|
38 | - // We need the dir name as getAppPath appends the appid |
|
39 | - $appRoot = dirname($this->appManager->getAppPath($app)); |
|
40 | - // Only do this if $app_path is set, because an empty argument to realpath gets turned into cwd. |
|
41 | - if ($appRoot) { |
|
42 | - // Handle symlinks |
|
43 | - $appRoot = realpath($appRoot); |
|
44 | - } |
|
45 | - // Get the app webroot |
|
46 | - $appWebRoot = dirname($this->appManager->getAppWebPath($app)); |
|
47 | - } catch (AppPathNotFoundException $e) { |
|
48 | - // ignore |
|
49 | - } |
|
50 | - |
|
51 | - if (str_contains($script, '/l10n/')) { |
|
52 | - // For language files we try to load them all, so themes can overwrite |
|
53 | - // single l10n strings without having to translate all of them. |
|
54 | - $found = 0; |
|
55 | - $found += $this->appendScriptIfExist($this->serverroot, 'core/' . $script); |
|
56 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script); |
|
57 | - $found += $this->appendScriptIfExist($this->serverroot, $script); |
|
58 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . $script); |
|
59 | - $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot); |
|
60 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script); |
|
61 | - |
|
62 | - if ($found) { |
|
63 | - return; |
|
64 | - } |
|
65 | - } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script) |
|
66 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir . $script) |
|
67 | - || $this->appendScriptIfExist($this->serverroot, $script) |
|
68 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/$app-$scriptName") |
|
69 | - || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") |
|
70 | - || $this->appendScriptIfExist($appRoot, $script, $appWebRoot) |
|
71 | - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script . '.json') |
|
72 | - || $this->cacheAndAppendCombineJsonIfExist($appRoot, $script . '.json', $app) |
|
73 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script) |
|
74 | - || $this->appendScriptIfExist($this->serverroot, 'core/' . $script) |
|
75 | - || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/core-$scriptName") |
|
76 | - || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) |
|
77 | - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/' . $script . '.json') |
|
78 | - ) { |
|
79 | - return; |
|
80 | - } |
|
81 | - |
|
82 | - // missing translations files will be ignored |
|
83 | - if (str_contains($script, '/l10n/')) { |
|
84 | - return; |
|
85 | - } |
|
86 | - |
|
87 | - $this->logger->error('Could not find resource {resource} to load', [ |
|
88 | - 'resource' => $script . '.js', |
|
89 | - 'app' => 'jsresourceloader', |
|
90 | - ]); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $script |
|
95 | - */ |
|
96 | - public function doFindTheme($script) { |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) |
|
101 | - * @see appendIfExist() |
|
102 | - */ |
|
103 | - protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null) { |
|
104 | - if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { |
|
105 | - return $this->appendIfExist($root, $file . '.js', $webRoot); |
|
106 | - } |
|
107 | - return true; |
|
108 | - } |
|
109 | - |
|
110 | - protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { |
|
111 | - if (is_file($root . '/' . $file)) { |
|
112 | - if ($this->jsCombiner->process($root, $file, $app)) { |
|
113 | - $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); |
|
114 | - } else { |
|
115 | - // Add all the files from the json |
|
116 | - $files = $this->jsCombiner->getContent($root, $file); |
|
117 | - $app_url = null; |
|
118 | - try { |
|
119 | - $app_url = $this->appManager->getAppWebPath($app); |
|
120 | - } catch (AppPathNotFoundException) { |
|
121 | - // pass |
|
122 | - } |
|
123 | - |
|
124 | - foreach ($files as $jsFile) { |
|
125 | - $this->append($root, $jsFile, $app_url); |
|
126 | - } |
|
127 | - } |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - return false; |
|
132 | - } |
|
15 | + protected JSCombiner $jsCombiner; |
|
16 | + protected IAppManager $appManager; |
|
17 | + |
|
18 | + public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) { |
|
19 | + parent::__construct($logger); |
|
20 | + |
|
21 | + $this->jsCombiner = $JSCombiner; |
|
22 | + $this->appManager = $appManager; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @param string $script |
|
27 | + */ |
|
28 | + public function doFind($script) { |
|
29 | + $theme_dir = 'themes/' . $this->theme . '/'; |
|
30 | + |
|
31 | + // Extracting the appId and the script file name |
|
32 | + $app = substr($script, 0, strpos($script, '/')); |
|
33 | + $scriptName = basename($script); |
|
34 | + // Get the app root path |
|
35 | + $appRoot = $this->serverroot . '/apps/'; |
|
36 | + $appWebRoot = null; |
|
37 | + try { |
|
38 | + // We need the dir name as getAppPath appends the appid |
|
39 | + $appRoot = dirname($this->appManager->getAppPath($app)); |
|
40 | + // Only do this if $app_path is set, because an empty argument to realpath gets turned into cwd. |
|
41 | + if ($appRoot) { |
|
42 | + // Handle symlinks |
|
43 | + $appRoot = realpath($appRoot); |
|
44 | + } |
|
45 | + // Get the app webroot |
|
46 | + $appWebRoot = dirname($this->appManager->getAppWebPath($app)); |
|
47 | + } catch (AppPathNotFoundException $e) { |
|
48 | + // ignore |
|
49 | + } |
|
50 | + |
|
51 | + if (str_contains($script, '/l10n/')) { |
|
52 | + // For language files we try to load them all, so themes can overwrite |
|
53 | + // single l10n strings without having to translate all of them. |
|
54 | + $found = 0; |
|
55 | + $found += $this->appendScriptIfExist($this->serverroot, 'core/' . $script); |
|
56 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script); |
|
57 | + $found += $this->appendScriptIfExist($this->serverroot, $script); |
|
58 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . $script); |
|
59 | + $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot); |
|
60 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script); |
|
61 | + |
|
62 | + if ($found) { |
|
63 | + return; |
|
64 | + } |
|
65 | + } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script) |
|
66 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir . $script) |
|
67 | + || $this->appendScriptIfExist($this->serverroot, $script) |
|
68 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/$app-$scriptName") |
|
69 | + || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") |
|
70 | + || $this->appendScriptIfExist($appRoot, $script, $appWebRoot) |
|
71 | + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script . '.json') |
|
72 | + || $this->cacheAndAppendCombineJsonIfExist($appRoot, $script . '.json', $app) |
|
73 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script) |
|
74 | + || $this->appendScriptIfExist($this->serverroot, 'core/' . $script) |
|
75 | + || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/core-$scriptName") |
|
76 | + || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) |
|
77 | + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/' . $script . '.json') |
|
78 | + ) { |
|
79 | + return; |
|
80 | + } |
|
81 | + |
|
82 | + // missing translations files will be ignored |
|
83 | + if (str_contains($script, '/l10n/')) { |
|
84 | + return; |
|
85 | + } |
|
86 | + |
|
87 | + $this->logger->error('Could not find resource {resource} to load', [ |
|
88 | + 'resource' => $script . '.js', |
|
89 | + 'app' => 'jsresourceloader', |
|
90 | + ]); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $script |
|
95 | + */ |
|
96 | + public function doFindTheme($script) { |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) |
|
101 | + * @see appendIfExist() |
|
102 | + */ |
|
103 | + protected function appendScriptIfExist(string $root, string $file, ?string $webRoot = null) { |
|
104 | + if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { |
|
105 | + return $this->appendIfExist($root, $file . '.js', $webRoot); |
|
106 | + } |
|
107 | + return true; |
|
108 | + } |
|
109 | + |
|
110 | + protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { |
|
111 | + if (is_file($root . '/' . $file)) { |
|
112 | + if ($this->jsCombiner->process($root, $file, $app)) { |
|
113 | + $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); |
|
114 | + } else { |
|
115 | + // Add all the files from the json |
|
116 | + $files = $this->jsCombiner->getContent($root, $file); |
|
117 | + $app_url = null; |
|
118 | + try { |
|
119 | + $app_url = $this->appManager->getAppWebPath($app); |
|
120 | + } catch (AppPathNotFoundException) { |
|
121 | + // pass |
|
122 | + } |
|
123 | + |
|
124 | + foreach ($files as $jsFile) { |
|
125 | + $this->append($root, $jsFile, $app_url); |
|
126 | + } |
|
127 | + } |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + return false; |
|
132 | + } |
|
133 | 133 | } |