@@ -258,8 +258,7 @@ |
||
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | return $this->xMetadata; |
| 261 | - } |
|
| 262 | - catch(Exception|Error $e) |
|
| 261 | + } catch(Exception|Error $e) |
|
| 263 | 262 | { |
| 264 | 263 | throw new SetupException($e->getMessage()); |
| 265 | 264 | } |
@@ -6,7 +6,8 @@ discard block |
||
| 6 | 6 | if(<?php echo $sJsClass ?> === undefined) { |
| 7 | 7 | <?php echo $sJsClass ?> = {}; |
| 8 | 8 | } |
| 9 | -<?php else: ?> |
|
| 9 | +<?php else { |
|
| 10 | + : ?> |
|
| 10 | 11 | <?php echo $sJsClass ?> = {}; |
| 11 | 12 | <?php endif ?> |
| 12 | 13 | <?php foreach($this->aMethods as $aMethod): ?> |
@@ -17,3 +18,4 @@ discard block |
||
| 17 | 18 | echo $sKey ?>: <?php echo $sValue ?><?php endforeach ?> }); |
| 18 | 19 | }; |
| 19 | 20 | <?php endforeach; |
| 21 | +} |
|
@@ -199,8 +199,7 @@ |
||
| 199 | 199 | { |
| 200 | 200 | return $this->xAppContainer != null && $this->xAppContainer->has($sClass) ? |
| 201 | 201 | $this->xAppContainer->get($sClass) : $this->xLibContainer->offsetGet($sClass); |
| 202 | - } |
|
| 203 | - catch(Exception|Throwable $e) |
|
| 202 | + } catch(Exception|Throwable $e) |
|
| 204 | 203 | { |
| 205 | 204 | $xLogger = $this->g(LoggerInterface::class); |
| 206 | 205 | $xTranslator = $this->g(Translator::class); |
@@ -214,8 +214,7 @@ discard block |
||
| 214 | 214 | return $this->make($this->get($this->getReflectionClassKey($sClassName))); |
| 215 | 215 | }); |
| 216 | 216 | } |
| 217 | - } |
|
| 218 | - catch(ReflectionException $e) |
|
| 217 | + } catch(ReflectionException $e) |
|
| 219 | 218 | { |
| 220 | 219 | throw new SetupException($this->di->g(Translator::class) |
| 221 | 220 | ->trans('errors.class.invalid', ['name' => $sClassName])); |
@@ -240,8 +239,7 @@ discard block |
||
| 240 | 239 | if($aOptions !== null) |
| 241 | 240 | { |
| 242 | 241 | $this->registerComponent($sClassName, $aOptions); |
| 243 | - } |
|
| 244 | - else // if(!isset($this->aComponents[$sClassName])) |
|
| 242 | + } else // if(!isset($this->aComponents[$sClassName])) |
|
| 245 | 243 | { |
| 246 | 244 | // The component was not found in a registered namespace. We need to parse all |
| 247 | 245 | // the directories to be able to find a component registered without a namespace. |
@@ -214,8 +214,7 @@ |
||
| 214 | 214 | if($this->bOnConfirm) |
| 215 | 215 | { |
| 216 | 216 | $this->aConfirmCommands[] = $xCommand; |
| 217 | - } |
|
| 218 | - else |
|
| 217 | + } else |
|
| 219 | 218 | { |
| 220 | 219 | $this->aCommands[] = $xCommand; |
| 221 | 220 | } |
@@ -218,8 +218,7 @@ |
||
| 218 | 218 | { |
| 219 | 219 | // A string is supposed to be the path to a config file. |
| 220 | 220 | $aLibOptions = $this->xConfigManager->read($aLibOptions); |
| 221 | - } |
|
| 222 | - elseif(!is_array($aLibOptions)) |
|
| 221 | + } elseif(!is_array($aLibOptions)) |
|
| 223 | 222 | { |
| 224 | 223 | // Otherwise, anything else than an array is not accepted. |
| 225 | 224 | $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | class PackageManager |
| 37 | 37 | { |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * The constructor |
| 40 | 40 | * |
| 41 | 41 | * @param Container $di |
@@ -47,48 +47,48 @@ discard block |
||
| 47 | 47 | * @param CallbackManager $xCallbackManager |
| 48 | 48 | * @param ComponentRegistry $xRegistry |
| 49 | 49 | */ |
| 50 | - public function __construct(private Container $di, private Translator $xTranslator, |
|
| 51 | - private PluginManager $xPluginManager, private ConfigManager $xConfigManager, |
|
| 52 | - private CodeGenerator $xCodeGenerator, private ViewRenderer $xViewRenderer, |
|
| 53 | - private CallbackManager $xCallbackManager, private ComponentRegistry $xRegistry) |
|
| 54 | - {} |
|
| 50 | +public function __construct(private Container $di, private Translator $xTranslator, |
|
| 51 | +private PluginManager $xPluginManager, private ConfigManager $xConfigManager, |
|
| 52 | +private CodeGenerator $xCodeGenerator, private ViewRenderer $xViewRenderer, |
|
| 53 | +private CallbackManager $xCallbackManager, private ComponentRegistry $xRegistry) |
|
| 54 | +{} |
|
| 55 | 55 | |
| 56 | - /** |
|
| 56 | +/** |
|
| 57 | 57 | * Save items in the DI container |
| 58 | 58 | * |
| 59 | 59 | * @param Config $xConfig |
| 60 | 60 | * |
| 61 | 61 | * @return void |
| 62 | 62 | */ |
| 63 | - private function updateContainer(Config $xConfig): void |
|
| 64 | - { |
|
| 65 | - $aOptions = $xConfig->getOption('container.set', []); |
|
| 66 | - foreach($aOptions as $xKey => $xValue) |
|
| 67 | - { |
|
| 68 | - // The key is the class name. It must be a string. |
|
| 69 | - $this->di->set((string)$xKey, $xValue); |
|
| 70 | - } |
|
| 71 | - $aOptions = $xConfig->getOption('container.val', []); |
|
| 72 | - foreach($aOptions as $xKey => $xValue) |
|
| 73 | - { |
|
| 74 | - // The key is the class name. It must be a string. |
|
| 75 | - $this->di->val((string)$xKey, $xValue); |
|
| 76 | - } |
|
| 77 | - $aOptions = $xConfig->getOption('container.auto', []); |
|
| 78 | - foreach($aOptions as $xValue) |
|
| 79 | - { |
|
| 80 | - // The key is the class name. It must be a string. |
|
| 81 | - $this->di->auto((string)$xValue); |
|
| 82 | - } |
|
| 83 | - $aOptions = $xConfig->getOption('container.alias', []); |
|
| 84 | - foreach($aOptions as $xKey => $xValue) |
|
| 85 | - { |
|
| 86 | - // The key is the class name. It must be a string. |
|
| 87 | - $this->di->alias((string)$xKey, (string)$xValue); |
|
| 88 | - } |
|
| 89 | - } |
|
| 63 | +private function updateContainer(Config $xConfig): void |
|
| 64 | +{ |
|
| 65 | +$aOptions = $xConfig->getOption('container.set', []); |
|
| 66 | +foreach($aOptions as $xKey => $xValue) |
|
| 67 | +{ |
|
| 68 | +// The key is the class name. It must be a string. |
|
| 69 | +$this->di->set((string)$xKey, $xValue); |
|
| 70 | +} |
|
| 71 | +$aOptions = $xConfig->getOption('container.val', []); |
|
| 72 | +foreach($aOptions as $xKey => $xValue) |
|
| 73 | +{ |
|
| 74 | +// The key is the class name. It must be a string. |
|
| 75 | +$this->di->val((string)$xKey, $xValue); |
|
| 76 | +} |
|
| 77 | +$aOptions = $xConfig->getOption('container.auto', []); |
|
| 78 | +foreach($aOptions as $xValue) |
|
| 79 | +{ |
|
| 80 | +// The key is the class name. It must be a string. |
|
| 81 | +$this->di->auto((string)$xValue); |
|
| 82 | +} |
|
| 83 | +$aOptions = $xConfig->getOption('container.alias', []); |
|
| 84 | +foreach($aOptions as $xKey => $xValue) |
|
| 85 | +{ |
|
| 86 | +// The key is the class name. It must be a string. |
|
| 87 | +$this->di->alias((string)$xKey, (string)$xValue); |
|
| 88 | +} |
|
| 89 | +} |
|
| 90 | 90 | |
| 91 | - /** |
|
| 91 | +/** |
|
| 92 | 92 | * Register callables from a section of the config |
| 93 | 93 | * |
| 94 | 94 | * @param array $aOptions The content of the config section |
@@ -97,40 +97,40 @@ discard block |
||
| 97 | 97 | * @return void |
| 98 | 98 | * @throws SetupException |
| 99 | 99 | */ |
| 100 | - private function registerCallables(array $aOptions, string $sCallableType): void |
|
| 101 | - { |
|
| 102 | - foreach($aOptions as $xKey => $xValue) |
|
| 103 | - { |
|
| 104 | - if(is_integer($xKey) && is_string($xValue)) |
|
| 105 | - { |
|
| 106 | - // Register a function without options |
|
| 107 | - $this->xPluginManager->registerCallable($sCallableType, $xValue); |
|
| 108 | - continue; |
|
| 109 | - } |
|
| 110 | - if(is_string($xKey) && (is_array($xValue) || is_string($xValue))) |
|
| 111 | - { |
|
| 112 | - // Register a function with options |
|
| 113 | - $this->xPluginManager->registerCallable($sCallableType, $xKey, $xValue); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } |
|
| 100 | +private function registerCallables(array $aOptions, string $sCallableType): void |
|
| 101 | +{ |
|
| 102 | +foreach($aOptions as $xKey => $xValue) |
|
| 103 | +{ |
|
| 104 | +if(is_integer($xKey) && is_string($xValue)) |
|
| 105 | +{ |
|
| 106 | + // Register a function without options |
|
| 107 | + $this->xPluginManager->registerCallable($sCallableType, $xValue); |
|
| 108 | + continue; |
|
| 109 | +} |
|
| 110 | +if(is_string($xKey) && (is_array($xValue) || is_string($xValue))) |
|
| 111 | +{ |
|
| 112 | + // Register a function with options |
|
| 113 | + $this->xPluginManager->registerCallable($sCallableType, $xKey, $xValue); |
|
| 114 | +} |
|
| 115 | +} |
|
| 116 | +} |
|
| 117 | 117 | |
| 118 | - /** |
|
| 118 | +/** |
|
| 119 | 119 | * Register exceptions handlers |
| 120 | 120 | * |
| 121 | 121 | * @param Config $xConfig |
| 122 | 122 | * |
| 123 | 123 | * @return void |
| 124 | 124 | */ |
| 125 | - private function registerExceptionHandlers(Config $xConfig): void |
|
| 126 | - { |
|
| 127 | - foreach($xConfig->getOption('exceptions', []) as $sExClass => $xExHandler) |
|
| 128 | - { |
|
| 129 | - $this->xCallbackManager->error($xExHandler, is_string($sExClass) ? $sExClass : ''); |
|
| 130 | - } |
|
| 131 | - } |
|
| 125 | +private function registerExceptionHandlers(Config $xConfig): void |
|
| 126 | +{ |
|
| 127 | +foreach($xConfig->getOption('exceptions', []) as $sExClass => $xExHandler) |
|
| 128 | +{ |
|
| 129 | +$this->xCallbackManager->error($xExHandler, is_string($sExClass) ? $sExClass : ''); |
|
| 130 | +} |
|
| 131 | +} |
|
| 132 | 132 | |
| 133 | - /** |
|
| 133 | +/** |
|
| 134 | 134 | * Get a callable list from config |
| 135 | 135 | * |
| 136 | 136 | * @param Config $xConfig |
@@ -140,35 +140,35 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @return void |
| 142 | 142 | */ |
| 143 | - private function registerCallablesFromConfig(Config $xConfig, |
|
| 144 | - string $sOptionName, string $sOptionKey, string $sCallableType): void |
|
| 145 | - { |
|
| 146 | - // The callable (directory path, class or function name) can be used as the |
|
| 147 | - // key of the array item, a string as the value of an entry without a key, |
|
| 148 | - // or set with the key $sOptionKey in an array entry without a key. |
|
| 149 | - $aCallables = []; |
|
| 150 | - foreach($xConfig->getOption($sOptionName, []) as $xKey => $xValue) |
|
| 151 | - { |
|
| 152 | - if(is_string($xKey)) |
|
| 153 | - { |
|
| 154 | - $aCallables[$xKey] = $xValue; |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - if(is_string($xValue)) |
|
| 158 | - { |
|
| 159 | - $aCallables[] = $xValue; |
|
| 160 | - continue; |
|
| 161 | - } |
|
| 162 | - if(is_array($xValue) && isset($xValue[$sOptionKey])) |
|
| 163 | - { |
|
| 164 | - $aCallables[$xValue[$sOptionKey]] = $xValue; |
|
| 165 | - } |
|
| 166 | - // Invalid values are ignored. |
|
| 167 | - } |
|
| 168 | - $this->registerCallables($aCallables, $sCallableType); |
|
| 169 | - } |
|
| 143 | +private function registerCallablesFromConfig(Config $xConfig, |
|
| 144 | +string $sOptionName, string $sOptionKey, string $sCallableType): void |
|
| 145 | +{ |
|
| 146 | +// The callable (directory path, class or function name) can be used as the |
|
| 147 | +// key of the array item, a string as the value of an entry without a key, |
|
| 148 | +// or set with the key $sOptionKey in an array entry without a key. |
|
| 149 | +$aCallables = []; |
|
| 150 | +foreach($xConfig->getOption($sOptionName, []) as $xKey => $xValue) |
|
| 151 | +{ |
|
| 152 | +if(is_string($xKey)) |
|
| 153 | +{ |
|
| 154 | + $aCallables[$xKey] = $xValue; |
|
| 155 | + continue; |
|
| 156 | +} |
|
| 157 | +if(is_string($xValue)) |
|
| 158 | +{ |
|
| 159 | + $aCallables[] = $xValue; |
|
| 160 | + continue; |
|
| 161 | +} |
|
| 162 | +if(is_array($xValue) && isset($xValue[$sOptionKey])) |
|
| 163 | +{ |
|
| 164 | + $aCallables[$xValue[$sOptionKey]] = $xValue; |
|
| 165 | +} |
|
| 166 | +// Invalid values are ignored. |
|
| 167 | +} |
|
| 168 | +$this->registerCallables($aCallables, $sCallableType); |
|
| 169 | +} |
|
| 170 | 170 | |
| 171 | - /** |
|
| 171 | +/** |
|
| 172 | 172 | * Read and set Jaxon options from a JSON config file |
| 173 | 173 | * |
| 174 | 174 | * @param Config $xConfig The config options |
@@ -176,33 +176,33 @@ discard block |
||
| 176 | 176 | * @return void |
| 177 | 177 | * @throws SetupException |
| 178 | 178 | */ |
| 179 | - private function registerItemsFromConfig(Config $xConfig): void |
|
| 180 | - { |
|
| 181 | - // Set the config for the registered callables. |
|
| 182 | - $this->xRegistry->setPackageConfig($xConfig); |
|
| 179 | +private function registerItemsFromConfig(Config $xConfig): void |
|
| 180 | +{ |
|
| 181 | +// Set the config for the registered callables. |
|
| 182 | +$this->xRegistry->setPackageConfig($xConfig); |
|
| 183 | 183 | |
| 184 | - // Register functions, classes and directories |
|
| 185 | - $this->registerCallablesFromConfig($xConfig, |
|
| 186 | - 'functions', 'name', Jaxon::CALLABLE_FUNCTION); |
|
| 187 | - $this->registerCallablesFromConfig($xConfig, |
|
| 188 | - 'classes', 'name', Jaxon::CALLABLE_CLASS); |
|
| 189 | - $this->registerCallablesFromConfig($xConfig, |
|
| 190 | - 'directories', 'path', Jaxon::CALLABLE_DIR); |
|
| 184 | +// Register functions, classes and directories |
|
| 185 | +$this->registerCallablesFromConfig($xConfig, |
|
| 186 | +'functions', 'name', Jaxon::CALLABLE_FUNCTION); |
|
| 187 | +$this->registerCallablesFromConfig($xConfig, |
|
| 188 | +'classes', 'name', Jaxon::CALLABLE_CLASS); |
|
| 189 | +$this->registerCallablesFromConfig($xConfig, |
|
| 190 | +'directories', 'path', Jaxon::CALLABLE_DIR); |
|
| 191 | 191 | |
| 192 | - // Unset the current config. |
|
| 193 | - $this->xRegistry->unsetPackageConfig(); |
|
| 192 | +// Unset the current config. |
|
| 193 | +$this->xRegistry->unsetPackageConfig(); |
|
| 194 | 194 | |
| 195 | - // Register the view namespaces |
|
| 196 | - // Note: the $xUserConfig can provide a "template" option, which is used to customize |
|
| 197 | - // the user defined view namespaces. That's why it is needed here. |
|
| 198 | - $this->xViewRenderer->addNamespaces($xConfig); |
|
| 199 | - // Save items in the DI container |
|
| 200 | - $this->updateContainer($xConfig); |
|
| 201 | - // Register the exception handlers |
|
| 202 | - $this->registerExceptionHandlers($xConfig); |
|
| 203 | - } |
|
| 195 | +// Register the view namespaces |
|
| 196 | +// Note: the $xUserConfig can provide a "template" option, which is used to customize |
|
| 197 | +// the user defined view namespaces. That's why it is needed here. |
|
| 198 | +$this->xViewRenderer->addNamespaces($xConfig); |
|
| 199 | +// Save items in the DI container |
|
| 200 | +$this->updateContainer($xConfig); |
|
| 201 | +// Register the exception handlers |
|
| 202 | +$this->registerExceptionHandlers($xConfig); |
|
| 203 | +} |
|
| 204 | 204 | |
| 205 | - /** |
|
| 205 | +/** |
|
| 206 | 206 | * Get the options provided by the package library |
| 207 | 207 | * |
| 208 | 208 | * @param class-string $sClassName The package class |
@@ -210,27 +210,27 @@ discard block |
||
| 210 | 210 | * @return Config |
| 211 | 211 | * @throws SetupException |
| 212 | 212 | */ |
| 213 | - private function getPackageLibConfig(string $sClassName): Config |
|
| 214 | - { |
|
| 215 | - // $this->aPackages contains packages config file paths. |
|
| 216 | - $aLibOptions = $sClassName::config(); |
|
| 217 | - if(is_string($aLibOptions)) |
|
| 218 | - { |
|
| 219 | - // A string is supposed to be the path to a config file. |
|
| 220 | - $aLibOptions = $this->xConfigManager->read($aLibOptions); |
|
| 221 | - } |
|
| 222 | - elseif(!is_array($aLibOptions)) |
|
| 223 | - { |
|
| 224 | - // Otherwise, anything else than an array is not accepted. |
|
| 225 | - $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 226 | - throw new SetupException($sMessage); |
|
| 227 | - } |
|
| 228 | - // Add the package name to the config |
|
| 229 | - $aLibOptions['package'] = $sClassName; |
|
| 230 | - return $this->xConfigManager->newConfig($aLibOptions); |
|
| 231 | - } |
|
| 213 | +private function getPackageLibConfig(string $sClassName): Config |
|
| 214 | +{ |
|
| 215 | +// $this->aPackages contains packages config file paths. |
|
| 216 | +$aLibOptions = $sClassName::config(); |
|
| 217 | +if(is_string($aLibOptions)) |
|
| 218 | +{ |
|
| 219 | +// A string is supposed to be the path to a config file. |
|
| 220 | +$aLibOptions = $this->xConfigManager->read($aLibOptions); |
|
| 221 | +} |
|
| 222 | +elseif(!is_array($aLibOptions)) |
|
| 223 | +{ |
|
| 224 | +// Otherwise, anything else than an array is not accepted. |
|
| 225 | +$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 226 | +throw new SetupException($sMessage); |
|
| 227 | +} |
|
| 228 | +// Add the package name to the config |
|
| 229 | +$aLibOptions['package'] = $sClassName; |
|
| 230 | +return $this->xConfigManager->newConfig($aLibOptions); |
|
| 231 | +} |
|
| 232 | 232 | |
| 233 | - /** |
|
| 233 | +/** |
|
| 234 | 234 | * Register a package |
| 235 | 235 | * |
| 236 | 236 | * @param class-string $sClassName The package class |
@@ -239,27 +239,27 @@ discard block |
||
| 239 | 239 | * @return void |
| 240 | 240 | * @throws SetupException |
| 241 | 241 | */ |
| 242 | - public function registerPackage(string $sClassName, array $aUserOptions = []): void |
|
| 243 | - { |
|
| 244 | - $sClassName = trim($sClassName, '\\ '); |
|
| 245 | - if(!is_subclass_of($sClassName, AbstractPackage::class)) |
|
| 246 | - { |
|
| 247 | - $sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 248 | - throw new SetupException($sMessage); |
|
| 249 | - } |
|
| 242 | +public function registerPackage(string $sClassName, array $aUserOptions = []): void |
|
| 243 | +{ |
|
| 244 | +$sClassName = trim($sClassName, '\\ '); |
|
| 245 | +if(!is_subclass_of($sClassName, AbstractPackage::class)) |
|
| 246 | +{ |
|
| 247 | +$sMessage = $this->xTranslator->trans('errors.register.invalid', ['name' => $sClassName]); |
|
| 248 | +throw new SetupException($sMessage); |
|
| 249 | +} |
|
| 250 | 250 | |
| 251 | - // Register the declarations in the package config. |
|
| 252 | - $xAppConfig = $this->getPackageLibConfig($sClassName); |
|
| 253 | - $this->registerItemsFromConfig($xAppConfig); |
|
| 251 | +// Register the declarations in the package config. |
|
| 252 | +$xAppConfig = $this->getPackageLibConfig($sClassName); |
|
| 253 | +$this->registerItemsFromConfig($xAppConfig); |
|
| 254 | 254 | |
| 255 | - // Register the package and its options in the DI |
|
| 256 | - $this->di->registerPackage($sClassName, $aUserOptions); |
|
| 255 | +// Register the package and its options in the DI |
|
| 256 | +$this->di->registerPackage($sClassName, $aUserOptions); |
|
| 257 | 257 | |
| 258 | - // Register the package as a code generator. |
|
| 259 | - $this->xCodeGenerator->addCodeGenerator($sClassName, 500); |
|
| 260 | - } |
|
| 258 | +// Register the package as a code generator. |
|
| 259 | +$this->xCodeGenerator->addCodeGenerator($sClassName, 500); |
|
| 260 | +} |
|
| 261 | 261 | |
| 262 | - /** |
|
| 262 | +/** |
|
| 263 | 263 | * Get a package instance |
| 264 | 264 | * |
| 265 | 265 | * @template T of AbstractPackage |
@@ -267,41 +267,41 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return T|null |
| 269 | 269 | */ |
| 270 | - public function getPackage(string $sClassName): ?AbstractPackage |
|
| 271 | - { |
|
| 272 | - $sClassName = trim($sClassName, '\\ '); |
|
| 273 | - return $this->di->h($sClassName) ? $this->di->g($sClassName) : null; |
|
| 274 | - } |
|
| 270 | +public function getPackage(string $sClassName): ?AbstractPackage |
|
| 271 | +{ |
|
| 272 | +$sClassName = trim($sClassName, '\\ '); |
|
| 273 | +return $this->di->h($sClassName) ? $this->di->g($sClassName) : null; |
|
| 274 | +} |
|
| 275 | 275 | |
| 276 | - /** |
|
| 276 | +/** |
|
| 277 | 277 | * Read and set Jaxon options from the config |
| 278 | 278 | * |
| 279 | 279 | * @return void |
| 280 | 280 | * @throws SetupException |
| 281 | 281 | */ |
| 282 | - public function registerFromConfig(): void |
|
| 283 | - { |
|
| 284 | - $xAppConfig = $this->xConfigManager->getAppConfig(); |
|
| 285 | - $this->registerItemsFromConfig($xAppConfig); |
|
| 282 | +public function registerFromConfig(): void |
|
| 283 | +{ |
|
| 284 | +$xAppConfig = $this->xConfigManager->getAppConfig(); |
|
| 285 | +$this->registerItemsFromConfig($xAppConfig); |
|
| 286 | 286 | |
| 287 | - // Register packages |
|
| 288 | - $aPackageConfig = $xAppConfig->getOption('packages', []); |
|
| 289 | - foreach($aPackageConfig as $xKey => $xValue) |
|
| 290 | - { |
|
| 291 | - if(is_integer($xKey) && is_string($xValue)) |
|
| 292 | - { |
|
| 293 | - // Register a package without options |
|
| 294 | - $sClassName = $xValue; |
|
| 295 | - $this->registerPackage($sClassName); |
|
| 296 | - continue; |
|
| 297 | - } |
|
| 298 | - if(is_string($xKey) && is_array($xValue)) |
|
| 299 | - { |
|
| 300 | - // Register a package with options |
|
| 301 | - $sClassName = $xKey; |
|
| 302 | - $aPkgOptions = $xValue; |
|
| 303 | - $this->registerPackage($sClassName, $aPkgOptions); |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - } |
|
| 287 | +// Register packages |
|
| 288 | +$aPackageConfig = $xAppConfig->getOption('packages', []); |
|
| 289 | +foreach($aPackageConfig as $xKey => $xValue) |
|
| 290 | +{ |
|
| 291 | +if(is_integer($xKey) && is_string($xValue)) |
|
| 292 | +{ |
|
| 293 | + // Register a package without options |
|
| 294 | + $sClassName = $xValue; |
|
| 295 | + $this->registerPackage($sClassName); |
|
| 296 | + continue; |
|
| 297 | +} |
|
| 298 | +if(is_string($xKey) && is_array($xValue)) |
|
| 299 | +{ |
|
| 300 | + // Register a package with options |
|
| 301 | + $sClassName = $xKey; |
|
| 302 | + $aPkgOptions = $xValue; |
|
| 303 | + $this->registerPackage($sClassName, $aPkgOptions); |
|
| 304 | +} |
|
| 305 | +} |
|
| 306 | +} |
|
| 307 | 307 | } |
@@ -85,8 +85,7 @@ discard block |
||
| 85 | 85 | if(is_string($xOptions)) |
| 86 | 86 | { |
| 87 | 87 | $xOptions = ['include' => $xOptions]; |
| 88 | - } |
|
| 89 | - elseif(!is_array($xOptions)) |
|
| 88 | + } elseif(!is_array($xOptions)) |
|
| 90 | 89 | { |
| 91 | 90 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 92 | 91 | } |
@@ -262,8 +261,7 @@ discard block |
||
| 262 | 261 | |
| 263 | 262 | // Unable to find the requested class or method |
| 264 | 263 | $this->throwException('', 'errors.objects.excluded', $aErrorParams); |
| 265 | - } |
|
| 266 | - catch(ReflectionException|SetupException $e) |
|
| 264 | + } catch(ReflectionException|SetupException $e) |
|
| 267 | 265 | { |
| 268 | 266 | // Unable to execute the requested class or method |
| 269 | 267 | $this->throwException($e->getMessage(), $sError, $aErrorParams); |
@@ -226,8 +226,7 @@ |
||
| 226 | 226 | if(is_array($xMethodToCall)) |
| 227 | 227 | { |
| 228 | 228 | $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
| 229 | - } |
|
| 230 | - elseif(is_string($xMethodToCall)) |
|
| 229 | + } elseif(is_string($xMethodToCall)) |
|
| 231 | 230 | { |
| 232 | 231 | $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
| 233 | 232 | } |
@@ -31,183 +31,183 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | class ComponentOptions |
| 33 | 33 | { |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * Check if the js code for this object must be generated |
| 36 | 36 | * |
| 37 | 37 | * @var bool |
| 38 | 38 | */ |
| 39 | - private $bExcluded = false; |
|
| 39 | +private $bExcluded = false; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 41 | +/** |
|
| 42 | 42 | * The character to use as separator in javascript class names |
| 43 | 43 | * |
| 44 | 44 | * @var string |
| 45 | 45 | */ |
| 46 | - private $sSeparator = '.'; |
|
| 46 | +private $sSeparator = '.'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 48 | +/** |
|
| 49 | 49 | * A list of methods of the user registered callable object the library must not export to javascript |
| 50 | 50 | * |
| 51 | 51 | * @var array |
| 52 | 52 | */ |
| 53 | - private $aProtectedMethods = []; |
|
| 53 | +private $aProtectedMethods = []; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 55 | +/** |
|
| 56 | 56 | * A list of methods to call before processing the request |
| 57 | 57 | * |
| 58 | 58 | * @var array |
| 59 | 59 | */ |
| 60 | - private $aBeforeMethods = []; |
|
| 60 | +private $aBeforeMethods = []; |
|
| 61 | 61 | |
| 62 | - /** |
|
| 62 | +/** |
|
| 63 | 63 | * A list of methods to call after processing the request |
| 64 | 64 | * |
| 65 | 65 | * @var array |
| 66 | 66 | */ |
| 67 | - private $aAfterMethods = []; |
|
| 67 | +private $aAfterMethods = []; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 69 | +/** |
|
| 70 | 70 | * The javascript class options |
| 71 | 71 | * |
| 72 | 72 | * @var array |
| 73 | 73 | */ |
| 74 | - private $aJsOptions = []; |
|
| 74 | +private $aJsOptions = []; |
|
| 75 | 75 | |
| 76 | - /** |
|
| 76 | +/** |
|
| 77 | 77 | * The DI options |
| 78 | 78 | * |
| 79 | 79 | * @var array |
| 80 | 80 | */ |
| 81 | - private $aDiOptions = []; |
|
| 81 | +private $aDiOptions = []; |
|
| 82 | 82 | |
| 83 | - /** |
|
| 83 | +/** |
|
| 84 | 84 | * The constructor |
| 85 | 85 | * |
| 86 | 86 | * @param ReflectionClass $xReflectionClass |
| 87 | 87 | * @param array $aOptions |
| 88 | 88 | * @param MetadataInterface|null $xMetadata |
| 89 | 89 | */ |
| 90 | - public function __construct(private ReflectionClass $xReflectionClass, |
|
| 91 | - array $aOptions, ?MetadataInterface $xMetadata) |
|
| 92 | - { |
|
| 93 | - $this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
|
| 94 | - (bool)($aOptions['excluded'] ?? false); |
|
| 95 | - if($this->bExcluded) |
|
| 96 | - { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 90 | +public function __construct(private ReflectionClass $xReflectionClass, |
|
| 91 | +array $aOptions, ?MetadataInterface $xMetadata) |
|
| 92 | +{ |
|
| 93 | +$this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
|
| 94 | +(bool)($aOptions['excluded'] ?? false); |
|
| 95 | +if($this->bExcluded) |
|
| 96 | +{ |
|
| 97 | +return; |
|
| 98 | +} |
|
| 99 | 99 | |
| 100 | - $sSeparator = $aOptions['separator']; |
|
| 101 | - if($sSeparator === '_' || $sSeparator === '.') |
|
| 102 | - { |
|
| 103 | - $this->sSeparator = $sSeparator; |
|
| 104 | - } |
|
| 105 | - $this->addProtectedMethods($aOptions['protected']); |
|
| 106 | - $this->addProtectedMethods($xMetadata?->getProtectedMethods() ?? []); |
|
| 100 | +$sSeparator = $aOptions['separator']; |
|
| 101 | +if($sSeparator === '_' || $sSeparator === '.') |
|
| 102 | +{ |
|
| 103 | +$this->sSeparator = $sSeparator; |
|
| 104 | +} |
|
| 105 | +$this->addProtectedMethods($aOptions['protected']); |
|
| 106 | +$this->addProtectedMethods($xMetadata?->getProtectedMethods() ?? []); |
|
| 107 | 107 | |
| 108 | - foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 109 | - { |
|
| 110 | - $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list. |
|
| 111 | - foreach($aFunctionNames as $sFunctionName) |
|
| 112 | - { |
|
| 113 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - foreach($xMetadata?->getProperties() ?? [] as $sFunctionName => $aFunctionOptions) |
|
| 117 | - { |
|
| 118 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 119 | - } |
|
| 120 | - } |
|
| 108 | +foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 109 | +{ |
|
| 110 | +$aFunctionNames = explode(',', $sNames); // Names are in comma-separated list. |
|
| 111 | +foreach($aFunctionNames as $sFunctionName) |
|
| 112 | +{ |
|
| 113 | + $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 114 | +} |
|
| 115 | +} |
|
| 116 | +foreach($xMetadata?->getProperties() ?? [] as $sFunctionName => $aFunctionOptions) |
|
| 117 | +{ |
|
| 118 | +$this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 119 | +} |
|
| 120 | +} |
|
| 121 | 121 | |
| 122 | - /** |
|
| 122 | +/** |
|
| 123 | 123 | * @param mixed $xMethods |
| 124 | 124 | * |
| 125 | 125 | * @return void |
| 126 | 126 | */ |
| 127 | - private function addProtectedMethods($xMethods): void |
|
| 128 | - { |
|
| 129 | - if(!is_array($xMethods)) |
|
| 130 | - { |
|
| 131 | - $this->aProtectedMethods[trim((string)$xMethods)] = true; |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - foreach($xMethods as $sMethod) |
|
| 135 | - { |
|
| 136 | - $this->aProtectedMethods[trim((string)$sMethod)] = true; |
|
| 137 | - } |
|
| 138 | - } |
|
| 127 | +private function addProtectedMethods($xMethods): void |
|
| 128 | +{ |
|
| 129 | +if(!is_array($xMethods)) |
|
| 130 | +{ |
|
| 131 | +$this->aProtectedMethods[trim((string)$xMethods)] = true; |
|
| 132 | +return; |
|
| 133 | +} |
|
| 134 | +foreach($xMethods as $sMethod) |
|
| 135 | +{ |
|
| 136 | +$this->aProtectedMethods[trim((string)$sMethod)] = true; |
|
| 137 | +} |
|
| 138 | +} |
|
| 139 | 139 | |
| 140 | - /** |
|
| 140 | +/** |
|
| 141 | 141 | * Check if the js code for this object must be generated |
| 142 | 142 | * |
| 143 | 143 | * @return bool |
| 144 | 144 | */ |
| 145 | - public function excluded(): bool |
|
| 146 | - { |
|
| 147 | - return $this->bExcluded; |
|
| 148 | - } |
|
| 145 | +public function excluded(): bool |
|
| 146 | +{ |
|
| 147 | +return $this->bExcluded; |
|
| 148 | +} |
|
| 149 | 149 | |
| 150 | - /** |
|
| 150 | +/** |
|
| 151 | 151 | * @return string |
| 152 | 152 | */ |
| 153 | - public function separator(): string |
|
| 154 | - { |
|
| 155 | - return $this->sSeparator; |
|
| 156 | - } |
|
| 153 | +public function separator(): string |
|
| 154 | +{ |
|
| 155 | +return $this->sSeparator; |
|
| 156 | +} |
|
| 157 | 157 | |
| 158 | - /** |
|
| 158 | +/** |
|
| 159 | 159 | * @param string $sMethodName |
| 160 | 160 | * @param bool $bTakeAll |
| 161 | 161 | * |
| 162 | 162 | * @return bool |
| 163 | 163 | */ |
| 164 | - public function isProtectedMethod(string $sMethodName, bool $bTakeAll): bool |
|
| 165 | - { |
|
| 166 | - // The public methods of the Component base classes are protected. |
|
| 167 | - if(($this->xReflectionClass->isSubclassOf(NodeComponent::class) && |
|
| 168 | - in_array($sMethodName, ['item', 'html'])) || |
|
| 169 | - ($this->xReflectionClass->isSubclassOf(FuncComponent::class) && |
|
| 170 | - in_array($sMethodName, ['paginator']))) |
|
| 171 | - { |
|
| 172 | - return true; |
|
| 173 | - } |
|
| 174 | - return !$bTakeAll && (isset($this->aProtectedMethods['*']) |
|
| 175 | - || isset($this->aProtectedMethods[$sMethodName])); |
|
| 176 | - } |
|
| 164 | +public function isProtectedMethod(string $sMethodName, bool $bTakeAll): bool |
|
| 165 | +{ |
|
| 166 | +// The public methods of the Component base classes are protected. |
|
| 167 | +if(($this->xReflectionClass->isSubclassOf(NodeComponent::class) && |
|
| 168 | +in_array($sMethodName, ['item', 'html'])) || |
|
| 169 | +($this->xReflectionClass->isSubclassOf(FuncComponent::class) && |
|
| 170 | +in_array($sMethodName, ['paginator']))) |
|
| 171 | +{ |
|
| 172 | +return true; |
|
| 173 | +} |
|
| 174 | +return !$bTakeAll && (isset($this->aProtectedMethods['*']) |
|
| 175 | +|| isset($this->aProtectedMethods[$sMethodName])); |
|
| 176 | +} |
|
| 177 | 177 | |
| 178 | - /** |
|
| 178 | +/** |
|
| 179 | 179 | * @return array |
| 180 | 180 | */ |
| 181 | - public function beforeMethods(): array |
|
| 182 | - { |
|
| 183 | - return $this->aBeforeMethods; |
|
| 184 | - } |
|
| 181 | +public function beforeMethods(): array |
|
| 182 | +{ |
|
| 183 | +return $this->aBeforeMethods; |
|
| 184 | +} |
|
| 185 | 185 | |
| 186 | - /** |
|
| 186 | +/** |
|
| 187 | 187 | * @return array |
| 188 | 188 | */ |
| 189 | - public function afterMethods(): array |
|
| 190 | - { |
|
| 191 | - return $this->aAfterMethods; |
|
| 192 | - } |
|
| 189 | +public function afterMethods(): array |
|
| 190 | +{ |
|
| 191 | +return $this->aAfterMethods; |
|
| 192 | +} |
|
| 193 | 193 | |
| 194 | - /** |
|
| 194 | +/** |
|
| 195 | 195 | * @return array |
| 196 | 196 | */ |
| 197 | - public function diOptions(): array |
|
| 198 | - { |
|
| 199 | - return $this->aDiOptions; |
|
| 200 | - } |
|
| 197 | +public function diOptions(): array |
|
| 198 | +{ |
|
| 199 | +return $this->aDiOptions; |
|
| 200 | +} |
|
| 201 | 201 | |
| 202 | - /** |
|
| 202 | +/** |
|
| 203 | 203 | * @return array |
| 204 | 204 | */ |
| 205 | - public function jsOptions(): array |
|
| 206 | - { |
|
| 207 | - return $this->aJsOptions; |
|
| 208 | - } |
|
| 205 | +public function jsOptions(): array |
|
| 206 | +{ |
|
| 207 | +return $this->aJsOptions; |
|
| 208 | +} |
|
| 209 | 209 | |
| 210 | - /** |
|
| 210 | +/** |
|
| 211 | 211 | * Set hook methods |
| 212 | 212 | * |
| 213 | 213 | * @param array $aHookMethods The array of hook methods |
@@ -215,34 +215,34 @@ discard block |
||
| 215 | 215 | * |
| 216 | 216 | * @return void |
| 217 | 217 | */ |
| 218 | - private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 219 | - { |
|
| 220 | - foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 221 | - { |
|
| 222 | - if(!isset($aHookMethods[$sCalledMethod])) |
|
| 223 | - { |
|
| 224 | - $aHookMethods[$sCalledMethod] = []; |
|
| 225 | - } |
|
| 226 | - if(is_array($xMethodToCall)) |
|
| 227 | - { |
|
| 228 | - $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 229 | - } |
|
| 230 | - elseif(is_string($xMethodToCall)) |
|
| 231 | - { |
|
| 232 | - $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 233 | - } |
|
| 234 | - } |
|
| 235 | - } |
|
| 218 | +private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 219 | +{ |
|
| 220 | +foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 221 | +{ |
|
| 222 | +if(!isset($aHookMethods[$sCalledMethod])) |
|
| 223 | +{ |
|
| 224 | + $aHookMethods[$sCalledMethod] = []; |
|
| 225 | +} |
|
| 226 | +if(is_array($xMethodToCall)) |
|
| 227 | +{ |
|
| 228 | + $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 229 | +} |
|
| 230 | +elseif(is_string($xMethodToCall)) |
|
| 231 | +{ |
|
| 232 | + $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 233 | +} |
|
| 234 | +} |
|
| 235 | +} |
|
| 236 | 236 | |
| 237 | - /** |
|
| 237 | +/** |
|
| 238 | 238 | * @param array $aDiOptions |
| 239 | 239 | */ |
| 240 | - private function addDiOption(array $aDiOptions): void |
|
| 241 | - { |
|
| 242 | - $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 243 | - } |
|
| 240 | +private function addDiOption(array $aDiOptions): void |
|
| 241 | +{ |
|
| 242 | +$this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 243 | +} |
|
| 244 | 244 | |
| 245 | - /** |
|
| 245 | +/** |
|
| 246 | 246 | * Set configuration options / call options for each method |
| 247 | 247 | * |
| 248 | 248 | * @param string $sName The name of the configuration option |
@@ -250,25 +250,25 @@ discard block |
||
| 250 | 250 | * |
| 251 | 251 | * @return void |
| 252 | 252 | */ |
| 253 | - private function addOption(string $sName, $xValue): void |
|
| 254 | - { |
|
| 255 | - switch($sName) |
|
| 256 | - { |
|
| 257 | - // Set the methods to call before processing the request |
|
| 258 | - case '__before': |
|
| 259 | - $this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 260 | - break; |
|
| 261 | - // Set the methods to call after processing the request |
|
| 262 | - case '__after': |
|
| 263 | - $this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 264 | - break; |
|
| 265 | - // Set the attributes to inject in the callable object |
|
| 266 | - case '__di': |
|
| 267 | - $this->addDiOption($xValue); |
|
| 268 | - break; |
|
| 269 | - default: |
|
| 270 | - break; |
|
| 271 | - } |
|
| 253 | +private function addOption(string $sName, $xValue): void |
|
| 254 | +{ |
|
| 255 | +switch($sName) |
|
| 256 | +{ |
|
| 257 | +// Set the methods to call before processing the request |
|
| 258 | +case '__before': |
|
| 259 | +$this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 260 | +break; |
|
| 261 | +// Set the methods to call after processing the request |
|
| 262 | +case '__after': |
|
| 263 | +$this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 264 | +break; |
|
| 265 | +// Set the attributes to inject in the callable object |
|
| 266 | +case '__di': |
|
| 267 | +$this->addDiOption($xValue); |
|
| 268 | +break; |
|
| 269 | +default: |
|
| 270 | +break; |
|
| 271 | +} |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -92,8 +92,7 @@ discard block |
||
| 92 | 92 | if(is_string($xOptions)) |
| 93 | 93 | { |
| 94 | 94 | $xOptions = ['include' => $xOptions]; |
| 95 | - } |
|
| 96 | - elseif(!is_array($xOptions)) |
|
| 95 | + } elseif(!is_array($xOptions)) |
|
| 97 | 96 | { |
| 98 | 97 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 99 | 98 | } |
@@ -236,8 +235,7 @@ discard block |
||
| 236 | 235 | { |
| 237 | 236 | /** @var CallableFunction */ |
| 238 | 237 | $xFunction = $this->getCallable($sRequestedFunction); |
| 239 | - } |
|
| 240 | - catch(Exception $e) |
|
| 238 | + } catch(Exception $e) |
|
| 241 | 239 | { |
| 242 | 240 | // Unable to find the requested function |
| 243 | 241 | $this->throwException($e, $this->xTranslator->trans('errors.functions.invalid', |
@@ -246,8 +244,7 @@ discard block |
||
| 246 | 244 | try |
| 247 | 245 | { |
| 248 | 246 | $xFunction->call($this->xTarget->args()); |
| 249 | - } |
|
| 250 | - catch(Exception $e) |
|
| 247 | + } catch(Exception $e) |
|
| 251 | 248 | { |
| 252 | 249 | // Unable to execute the requested function |
| 253 | 250 | $this->throwException($e, $this->xTranslator->trans('errors.functions.call', |