@@ -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 | } |
@@ -26,13 +26,13 @@ discard block |
||
| 26 | 26 | * @param string $script |
| 27 | 27 | */ |
| 28 | 28 | public function doFind($script) { |
| 29 | - $theme_dir = 'themes/' . $this->theme . '/'; |
|
| 29 | + $theme_dir = 'themes/'.$this->theme.'/'; |
|
| 30 | 30 | |
| 31 | 31 | // Extracting the appId and the script file name |
| 32 | 32 | $app = substr($script, 0, strpos($script, '/')); |
| 33 | 33 | $scriptName = basename($script); |
| 34 | 34 | // Get the app root path |
| 35 | - $appRoot = $this->serverroot . '/apps/'; |
|
| 35 | + $appRoot = $this->serverroot.'/apps/'; |
|
| 36 | 36 | $appWebRoot = null; |
| 37 | 37 | try { |
| 38 | 38 | // We need the dir name as getAppPath appends the appid |
@@ -52,29 +52,29 @@ discard block |
||
| 52 | 52 | // For language files we try to load them all, so themes can overwrite |
| 53 | 53 | // single l10n strings without having to translate all of them. |
| 54 | 54 | $found = 0; |
| 55 | - $found += $this->appendScriptIfExist($this->serverroot, 'core/' . $script); |
|
| 56 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'core/' . $script); |
|
| 55 | + $found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script); |
|
| 56 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script); |
|
| 57 | 57 | $found += $this->appendScriptIfExist($this->serverroot, $script); |
| 58 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . $script); |
|
| 58 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script); |
|
| 59 | 59 | $found += $this->appendScriptIfExist($appRoot, $script, $appWebRoot); |
| 60 | - $found += $this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script); |
|
| 60 | + $found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script); |
|
| 61 | 61 | |
| 62 | 62 | if ($found) { |
| 63 | 63 | return; |
| 64 | 64 | } |
| 65 | - } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir . 'apps/' . $script) |
|
| 66 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir . $script) |
|
| 65 | + } elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script) |
|
| 66 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir.$script) |
|
| 67 | 67 | || $this->appendScriptIfExist($this->serverroot, $script) |
| 68 | - || $this->appendScriptIfExist($this->serverroot, $theme_dir . "dist/$app-$scriptName") |
|
| 68 | + || $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName") |
|
| 69 | 69 | || $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName") |
| 70 | 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") |
|
| 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 | 76 | || $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName"))) |
| 77 | - || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/' . $script . '.json') |
|
| 77 | + || $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json') |
|
| 78 | 78 | ) { |
| 79 | 79 | return; |
| 80 | 80 | } |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | $this->logger->error('Could not find resource {resource} to load', [ |
| 88 | - 'resource' => $script . '.js', |
|
| 88 | + 'resource' => $script.'.js', |
|
| 89 | 89 | 'app' => 'jsresourceloader', |
| 90 | 90 | ]); |
| 91 | 91 | } |
@@ -101,14 +101,14 @@ discard block |
||
| 101 | 101 | * @see appendIfExist() |
| 102 | 102 | */ |
| 103 | 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); |
|
| 104 | + if (!$this->appendIfExist($root, $file.'.mjs', $webRoot)) { |
|
| 105 | + return $this->appendIfExist($root, $file.'.js', $webRoot); |
|
| 106 | 106 | } |
| 107 | 107 | return true; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') { |
| 111 | - if (is_file($root . '/' . $file)) { |
|
| 111 | + if (is_file($root.'/'.$file)) { |
|
| 112 | 112 | if ($this->jsCombiner->process($root, $file, $app)) { |
| 113 | 113 | $this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false); |
| 114 | 114 | } else { |