@@ -32,135 +32,135 @@ |
||
32 | 32 | use Psr\Log\LoggerInterface; |
33 | 33 | |
34 | 34 | class JSResourceLocator extends ResourceLocator { |
35 | - protected JSCombiner $jsCombiner; |
|
36 | - protected IAppManager $appManager; |
|
37 | - |
|
38 | - public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) { |
|
39 | - parent::__construct($logger); |
|
40 | - |
|
41 | - $this->jsCombiner = $JSCombiner; |
|
42 | - $this->appManager = $appManager; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param string $script |
|
47 | - */ |
|
48 | - public function doFind($script) { |
|
49 | - $theme_dir = 'themes/'.$this->theme.'/'; |
|
50 | - |
|
51 | - // Extracting the appId and the script file name |
|
52 | - $app = substr($script, 0, strpos($script, '/')); |
|
53 | - $scriptName = basename($script); |
|
54 | - |
|
55 | - if (strpos($script, '/l10n/') !== false) { |
|
56 | - // For language files we try to load them all, so themes can overwrite |
|
57 | - // single l10n strings without having to translate all of them. |
|
58 | - $found = 0; |
|
59 | - $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script); |
|
60 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script); |
|
61 | - $found += $this->appendScriptIfExist($this->serverroot, $script); |
|
62 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script); |
|
63 | - $found += $this->appendScriptIfExist($this->serverroot, 'apps/'.$script); |
|
64 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script); |
|
65 | - |
|
66 | - if ($found) { |
|
67 | - return; |
|
68 | - } |
|
69 | - } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script) |
|
70 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script) |
|
71 | - || $this->appendScriptIfExist($this->serverroot, $script) |
|
72 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName") |
|
73 | - || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") |
|
74 | - || $this->appendScriptIfExist($this->serverroot, 'apps/'.$script) |
|
75 | - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json') |
|
76 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script) |
|
77 | - || $this->appendScriptIfExist($this->serverroot, 'core/'.$script) |
|
78 | - || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName") |
|
79 | - || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) |
|
80 | - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json') |
|
81 | - ) { |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - $script = substr($script, strpos($script, '/') + 1); |
|
86 | - $app_url = null; |
|
87 | - |
|
88 | - try { |
|
89 | - $app_url = $this->appManager->getAppWebPath($app); |
|
90 | - } catch (AppPathNotFoundException) { |
|
91 | - // pass |
|
92 | - } |
|
93 | - |
|
94 | - try { |
|
95 | - $app_path = $this->appManager->getAppPath($app); |
|
96 | - |
|
97 | - // Account for the possibility of having symlinks in app path. Only |
|
98 | - // do this if $app_path is set, because an empty argument to realpath |
|
99 | - // gets turned into cwd. |
|
100 | - $app_path = realpath($app_path); |
|
101 | - |
|
102 | - // check combined files |
|
103 | - if ($this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) { |
|
104 | - return; |
|
105 | - } |
|
106 | - |
|
107 | - // fallback to plain file location |
|
108 | - if ($this->appendScriptIfExist($app_path, $script, $app_url)) { |
|
109 | - return; |
|
110 | - } |
|
111 | - } catch (AppPathNotFoundException) { |
|
112 | - // pass (general error handling happens below) |
|
113 | - } |
|
114 | - |
|
115 | - // missing translations files will be ignored |
|
116 | - if (strpos($script, 'l10n/') === 0) { |
|
117 | - return; |
|
118 | - } |
|
119 | - |
|
120 | - $this->logger->error('Could not find resource {resource} to load', [ |
|
121 | - 'resource' => $app . '/' . $script . '.js', |
|
122 | - 'app' => 'jsresourceloader', |
|
123 | - ]); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $script |
|
128 | - */ |
|
129 | - public function doFindTheme($script) { |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) |
|
134 | - * @see appendIfExist() |
|
135 | - */ |
|
136 | - protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) { |
|
137 | - if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { |
|
138 | - return $this->appendIfExist($root, $file . '.js', $webRoot); |
|
139 | - } |
|
140 | - return true; |
|
141 | - } |
|
142 | - |
|
143 | - protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { |
|
144 | - if (is_file($root.'/'.$file)) { |
|
145 | - if ($this->jsCombiner->process($root, $file, $app)) { |
|
146 | - $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); |
|
147 | - } else { |
|
148 | - // Add all the files from the json |
|
149 | - $files = $this->jsCombiner->getContent($root, $file); |
|
150 | - $app_url = null; |
|
151 | - try { |
|
152 | - $app_url = $this->appManager->getAppWebPath($app); |
|
153 | - } catch (AppPathNotFoundException) { |
|
154 | - // pass |
|
155 | - } |
|
156 | - |
|
157 | - foreach ($files as $jsFile) { |
|
158 | - $this->append($root, $jsFile, $app_url); |
|
159 | - } |
|
160 | - } |
|
161 | - return true; |
|
162 | - } |
|
163 | - |
|
164 | - return false; |
|
165 | - } |
|
35 | + protected JSCombiner $jsCombiner; |
|
36 | + protected IAppManager $appManager; |
|
37 | + |
|
38 | + public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) { |
|
39 | + parent::__construct($logger); |
|
40 | + |
|
41 | + $this->jsCombiner = $JSCombiner; |
|
42 | + $this->appManager = $appManager; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param string $script |
|
47 | + */ |
|
48 | + public function doFind($script) { |
|
49 | + $theme_dir = 'themes/'.$this->theme.'/'; |
|
50 | + |
|
51 | + // Extracting the appId and the script file name |
|
52 | + $app = substr($script, 0, strpos($script, '/')); |
|
53 | + $scriptName = basename($script); |
|
54 | + |
|
55 | + if (strpos($script, '/l10n/') !== false) { |
|
56 | + // For language files we try to load them all, so themes can overwrite |
|
57 | + // single l10n strings without having to translate all of them. |
|
58 | + $found = 0; |
|
59 | + $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script); |
|
60 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script); |
|
61 | + $found += $this->appendScriptIfExist($this->serverroot, $script); |
|
62 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script); |
|
63 | + $found += $this->appendScriptIfExist($this->serverroot, 'apps/'.$script); |
|
64 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script); |
|
65 | + |
|
66 | + if ($found) { |
|
67 | + return; |
|
68 | + } |
|
69 | + } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script) |
|
70 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script) |
|
71 | + || $this->appendScriptIfExist($this->serverroot, $script) |
|
72 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName") |
|
73 | + || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") |
|
74 | + || $this->appendScriptIfExist($this->serverroot, 'apps/'.$script) |
|
75 | + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json') |
|
76 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script) |
|
77 | + || $this->appendScriptIfExist($this->serverroot, 'core/'.$script) |
|
78 | + || (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName") |
|
79 | + || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) |
|
80 | + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json') |
|
81 | + ) { |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + $script = substr($script, strpos($script, '/') + 1); |
|
86 | + $app_url = null; |
|
87 | + |
|
88 | + try { |
|
89 | + $app_url = $this->appManager->getAppWebPath($app); |
|
90 | + } catch (AppPathNotFoundException) { |
|
91 | + // pass |
|
92 | + } |
|
93 | + |
|
94 | + try { |
|
95 | + $app_path = $this->appManager->getAppPath($app); |
|
96 | + |
|
97 | + // Account for the possibility of having symlinks in app path. Only |
|
98 | + // do this if $app_path is set, because an empty argument to realpath |
|
99 | + // gets turned into cwd. |
|
100 | + $app_path = realpath($app_path); |
|
101 | + |
|
102 | + // check combined files |
|
103 | + if ($this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) { |
|
104 | + return; |
|
105 | + } |
|
106 | + |
|
107 | + // fallback to plain file location |
|
108 | + if ($this->appendScriptIfExist($app_path, $script, $app_url)) { |
|
109 | + return; |
|
110 | + } |
|
111 | + } catch (AppPathNotFoundException) { |
|
112 | + // pass (general error handling happens below) |
|
113 | + } |
|
114 | + |
|
115 | + // missing translations files will be ignored |
|
116 | + if (strpos($script, 'l10n/') === 0) { |
|
117 | + return; |
|
118 | + } |
|
119 | + |
|
120 | + $this->logger->error('Could not find resource {resource} to load', [ |
|
121 | + 'resource' => $app . '/' . $script . '.js', |
|
122 | + 'app' => 'jsresourceloader', |
|
123 | + ]); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $script |
|
128 | + */ |
|
129 | + public function doFindTheme($script) { |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`) |
|
134 | + * @see appendIfExist() |
|
135 | + */ |
|
136 | + protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) { |
|
137 | + if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { |
|
138 | + return $this->appendIfExist($root, $file . '.js', $webRoot); |
|
139 | + } |
|
140 | + return true; |
|
141 | + } |
|
142 | + |
|
143 | + protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { |
|
144 | + if (is_file($root.'/'.$file)) { |
|
145 | + if ($this->jsCombiner->process($root, $file, $app)) { |
|
146 | + $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); |
|
147 | + } else { |
|
148 | + // Add all the files from the json |
|
149 | + $files = $this->jsCombiner->getContent($root, $file); |
|
150 | + $app_url = null; |
|
151 | + try { |
|
152 | + $app_url = $this->appManager->getAppWebPath($app); |
|
153 | + } catch (AppPathNotFoundException) { |
|
154 | + // pass |
|
155 | + } |
|
156 | + |
|
157 | + foreach ($files as $jsFile) { |
|
158 | + $this->append($root, $jsFile, $app_url); |
|
159 | + } |
|
160 | + } |
|
161 | + return true; |
|
162 | + } |
|
163 | + |
|
164 | + return false; |
|
165 | + } |
|
166 | 166 | } |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | $this->logger->error('Could not find resource {resource} to load', [ |
121 | - 'resource' => $app . '/' . $script . '.js', |
|
121 | + 'resource' => $app.'/'.$script.'.js', |
|
122 | 122 | 'app' => 'jsresourceloader', |
123 | 123 | ]); |
124 | 124 | } |
@@ -134,8 +134,8 @@ discard block |
||
134 | 134 | * @see appendIfExist() |
135 | 135 | */ |
136 | 136 | protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) { |
137 | - if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) { |
|
138 | - return $this->appendIfExist($root, $file . '.js', $webRoot); |
|
137 | + if (!$this->appendIfExist($root, $file.'.mjs', $webRoot)) { |
|
138 | + return $this->appendIfExist($root, $file.'.js', $webRoot); |
|
139 | 139 | } |
140 | 140 | return true; |
141 | 141 | } |