@@ -241,83 +241,83 @@ |
||
| 241 | 241 | $this->aComponentPublicMethods['node'], |
| 242 | 242 | $xReflectionClass->isSubclassOf(FuncComponent::class) => |
| 243 | 243 | $this->aComponentPublicMethods['func'], |
| 244 | - default => [[], []], |
|
| 245 | - }; |
|
| 244 | +default => [[], []], |
|
| 245 | +}; |
|
| 246 | 246 | |
| 247 | - return [$aMethods, ...$aBaseMethods]; |
|
| 248 | - } |
|
| 247 | +return [$aMethods, ...$aBaseMethods]; |
|
| 248 | +} |
|
| 249 | 249 | |
| 250 | - /** |
|
| 250 | +/** |
|
| 251 | 251 | * @param ReflectionClass $xReflectionClass |
| 252 | 252 | * @param array $aMethods |
| 253 | 253 | * @param array $aOptions |
| 254 | 254 | * |
| 255 | 255 | * @return Metadata|null |
| 256 | 256 | */ |
| 257 | - private function getComponentMetadata(ReflectionClass $xReflectionClass, |
|
| 258 | - array $aMethods, array $aOptions): Metadata|null |
|
| 259 | - { |
|
| 260 | - /** @var Config|null */ |
|
| 261 | - $xPackageConfig = $aOptions['config'] ?? null; |
|
| 262 | - if($xPackageConfig === null || (bool)($aOptions['excluded'] ?? false)) |
|
| 263 | - { |
|
| 264 | - return null; |
|
| 265 | - } |
|
| 266 | - $sMetadataFormat = $xPackageConfig->getOption('metadata.format'); |
|
| 267 | - if(!in_array($sMetadataFormat, ['attributes', 'annotations'])) |
|
| 268 | - { |
|
| 269 | - return null; |
|
| 270 | - } |
|
| 257 | +private function getComponentMetadata(ReflectionClass $xReflectionClass, |
|
| 258 | +array $aMethods, array $aOptions): Metadata|null |
|
| 259 | +{ |
|
| 260 | +/** @var Config|null */ |
|
| 261 | +$xPackageConfig = $aOptions['config'] ?? null; |
|
| 262 | +if($xPackageConfig === null || (bool)($aOptions['excluded'] ?? false)) |
|
| 263 | +{ |
|
| 264 | +return null; |
|
| 265 | +} |
|
| 266 | +$sMetadataFormat = $xPackageConfig->getOption('metadata.format'); |
|
| 267 | +if(!in_array($sMetadataFormat, ['attributes', 'annotations'])) |
|
| 268 | +{ |
|
| 269 | +return null; |
|
| 270 | +} |
|
| 271 | 271 | |
| 272 | - // Try to get the class metadata from the cache. |
|
| 273 | - $di = $this->cn(); |
|
| 274 | - $xMetadata = null; |
|
| 275 | - $xMetadataCache = null; |
|
| 276 | - $xConfig = $di->config(); |
|
| 277 | - if($xConfig->getAppOption('metadata.cache.enabled', false)) |
|
| 278 | - { |
|
| 279 | - if(!$di->h('jaxon_metadata_cache_dir')) |
|
| 280 | - { |
|
| 281 | - $sCacheDir = $xConfig->getAppOption('metadata.cache.dir'); |
|
| 282 | - $di->val('jaxon_metadata_cache_dir', $sCacheDir); |
|
| 283 | - } |
|
| 284 | - $xMetadataCache = $di->getMetadataCache(); |
|
| 285 | - $xMetadata = $xMetadataCache->read($xReflectionClass->getName()); |
|
| 286 | - if($xMetadata !== null) |
|
| 287 | - { |
|
| 288 | - return $xMetadata; |
|
| 289 | - } |
|
| 290 | - } |
|
| 272 | +// Try to get the class metadata from the cache. |
|
| 273 | +$di = $this->cn(); |
|
| 274 | +$xMetadata = null; |
|
| 275 | +$xMetadataCache = null; |
|
| 276 | +$xConfig = $di->config(); |
|
| 277 | +if($xConfig->getAppOption('metadata.cache.enabled', false)) |
|
| 278 | +{ |
|
| 279 | +if(!$di->h('jaxon_metadata_cache_dir')) |
|
| 280 | +{ |
|
| 281 | + $sCacheDir = $xConfig->getAppOption('metadata.cache.dir'); |
|
| 282 | + $di->val('jaxon_metadata_cache_dir', $sCacheDir); |
|
| 283 | +} |
|
| 284 | +$xMetadataCache = $di->getMetadataCache(); |
|
| 285 | +$xMetadata = $xMetadataCache->read($xReflectionClass->getName()); |
|
| 286 | +if($xMetadata !== null) |
|
| 287 | +{ |
|
| 288 | + return $xMetadata; |
|
| 289 | +} |
|
| 290 | +} |
|
| 291 | 291 | |
| 292 | - $aProperties = array_map(fn($xProperty) => $xProperty->getName(), |
|
| 293 | - $xReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | |
|
| 294 | - ReflectionProperty::IS_PROTECTED)); |
|
| 292 | +$aProperties = array_map(fn($xProperty) => $xProperty->getName(), |
|
| 293 | +$xReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | |
|
| 294 | + ReflectionProperty::IS_PROTECTED)); |
|
| 295 | 295 | |
| 296 | - $xMetadataReader = $di->getMetadataReader($sMetadataFormat); |
|
| 297 | - $xInput = new InputData($xReflectionClass, $aMethods, $aProperties); |
|
| 298 | - $xMetadata = $xMetadataReader->getAttributes($xInput); |
|
| 296 | +$xMetadataReader = $di->getMetadataReader($sMetadataFormat); |
|
| 297 | +$xInput = new InputData($xReflectionClass, $aMethods, $aProperties); |
|
| 298 | +$xMetadata = $xMetadataReader->getAttributes($xInput); |
|
| 299 | 299 | |
| 300 | - // Try to save the metadata in the cache |
|
| 301 | - if($xMetadataCache !== null) |
|
| 302 | - { |
|
| 303 | - $xMetadataCache->save($xReflectionClass->getName(), $xMetadata); |
|
| 304 | - } |
|
| 300 | +// Try to save the metadata in the cache |
|
| 301 | +if($xMetadataCache !== null) |
|
| 302 | +{ |
|
| 303 | +$xMetadataCache->save($xReflectionClass->getName(), $xMetadata); |
|
| 304 | +} |
|
| 305 | 305 | |
| 306 | - return $xMetadata; |
|
| 307 | - } |
|
| 306 | +return $xMetadata; |
|
| 307 | +} |
|
| 308 | 308 | |
| 309 | - /** |
|
| 309 | +/** |
|
| 310 | 310 | * @param ReflectionClass $xReflectionClass |
| 311 | 311 | * @param array $aOptions |
| 312 | 312 | * |
| 313 | 313 | * @return ComponentOptions |
| 314 | 314 | */ |
| 315 | - public function getComponentOptions(ReflectionClass $xReflectionClass, |
|
| 316 | - array $aOptions): ComponentOptions |
|
| 317 | - { |
|
| 318 | - $aMethods = $this->getPublicMethods($xReflectionClass); |
|
| 319 | - $xMetadata = $this->getComponentMetadata($xReflectionClass, $aMethods[0], $aOptions); |
|
| 315 | +public function getComponentOptions(ReflectionClass $xReflectionClass, |
|
| 316 | +array $aOptions): ComponentOptions |
|
| 317 | +{ |
|
| 318 | +$aMethods = $this->getPublicMethods($xReflectionClass); |
|
| 319 | +$xMetadata = $this->getComponentMetadata($xReflectionClass, $aMethods[0], $aOptions); |
|
| 320 | 320 | |
| 321 | - return new ComponentOptions($aMethods, $aOptions, $xMetadata); |
|
| 322 | - } |
|
| 321 | +return new ComponentOptions($aMethods, $aOptions, $xMetadata); |
|
| 322 | +} |
|
| 323 | 323 | } |
@@ -35,154 +35,154 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | trait PluginTrait |
| 37 | 37 | { |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * Register the values into the container |
| 40 | 40 | * |
| 41 | 41 | * @return void |
| 42 | 42 | */ |
| 43 | - private function registerPlugins(): void |
|
| 44 | - { |
|
| 45 | - // Plugin manager |
|
| 46 | - $this->set(PluginManager::class, function($di) { |
|
| 47 | - $xPluginManager = new PluginManager($di->g(Container::class), |
|
| 48 | - $di->g(CodeGenerator::class), $di->g(Translator::class)); |
|
| 49 | - // Register the Jaxon request and response plugins |
|
| 50 | - $xPluginManager->registerPlugins(); |
|
| 51 | - return $xPluginManager; |
|
| 52 | - }); |
|
| 53 | - // Package manager |
|
| 54 | - $this->set(PackageManager::class, function($di) { |
|
| 55 | - return new PackageManager($di->g(Container::class), $di->g(Translator::class), |
|
| 56 | - $di->g(PluginManager::class), $di->g(ConfigManager::class), |
|
| 57 | - $di->g(CodeGenerator::class), $di->g(ViewRenderer::class), |
|
| 58 | - $di->g(CallbackManager::class), $di->g(ComponentRegistry::class)); |
|
| 59 | - }); |
|
| 60 | - // Code Generation |
|
| 61 | - $this->set(MinifierInterface::class, function() { |
|
| 62 | - return new class extends FileMinifier implements MinifierInterface |
|
| 63 | - {}; |
|
| 64 | - }); |
|
| 65 | - $this->set(AssetManager::class, function($di) { |
|
| 66 | - return new AssetManager($di->g(ConfigManager::class), |
|
| 67 | - $di->g(MinifierInterface::class)); |
|
| 68 | - }); |
|
| 69 | - $this->set(CodeGenerator::class, function($di) { |
|
| 70 | - return new CodeGenerator(Jaxon::VERSION, $di->g(Container::class), |
|
| 71 | - $di->g(TemplateEngine::class)); |
|
| 72 | - }); |
|
| 73 | - $this->set(ConfigScriptGenerator::class, function($di) { |
|
| 74 | - return new ConfigScriptGenerator($di->g(ParameterReader::class), |
|
| 75 | - $di->g(TemplateEngine::class), $di->g(ConfigManager::class)); |
|
| 76 | - }); |
|
| 77 | - $this->set(ReadyScriptGenerator::class, function($di) { |
|
| 78 | - return new ReadyScriptGenerator(); |
|
| 79 | - }); |
|
| 80 | - |
|
| 81 | - // Script response plugin |
|
| 82 | - $this->set(ScriptPlugin::class, function($di) { |
|
| 83 | - return new ScriptPlugin($di->g(CallFactory::class)); |
|
| 84 | - }); |
|
| 85 | - // Databag response plugin |
|
| 86 | - $this->set(DatabagPlugin::class, function($di) { |
|
| 87 | - return new DatabagPlugin($di->g(Container::class)); |
|
| 88 | - }); |
|
| 89 | - // Dialog response plugin |
|
| 90 | - $this->set(DialogPlugin::class, function($di) { |
|
| 91 | - return new DialogPlugin($di->g(DialogCommand::class)); |
|
| 92 | - }); |
|
| 93 | - // Paginator response plugin |
|
| 94 | - $this->set(PaginatorPlugin::class, function($di) { |
|
| 95 | - return new PaginatorPlugin($di->g(RendererInterface::class)); |
|
| 96 | - }); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 43 | +private function registerPlugins(): void |
|
| 44 | +{ |
|
| 45 | +// Plugin manager |
|
| 46 | +$this->set(PluginManager::class, function($di) { |
|
| 47 | +$xPluginManager = new PluginManager($di->g(Container::class), |
|
| 48 | + $di->g(CodeGenerator::class), $di->g(Translator::class)); |
|
| 49 | +// Register the Jaxon request and response plugins |
|
| 50 | +$xPluginManager->registerPlugins(); |
|
| 51 | +return $xPluginManager; |
|
| 52 | +}); |
|
| 53 | +// Package manager |
|
| 54 | +$this->set(PackageManager::class, function($di) { |
|
| 55 | +return new PackageManager($di->g(Container::class), $di->g(Translator::class), |
|
| 56 | + $di->g(PluginManager::class), $di->g(ConfigManager::class), |
|
| 57 | + $di->g(CodeGenerator::class), $di->g(ViewRenderer::class), |
|
| 58 | + $di->g(CallbackManager::class), $di->g(ComponentRegistry::class)); |
|
| 59 | +}); |
|
| 60 | +// Code Generation |
|
| 61 | +$this->set(MinifierInterface::class, function() { |
|
| 62 | +return new class extends FileMinifier implements MinifierInterface |
|
| 63 | +{}; |
|
| 64 | +}); |
|
| 65 | +$this->set(AssetManager::class, function($di) { |
|
| 66 | +return new AssetManager($di->g(ConfigManager::class), |
|
| 67 | + $di->g(MinifierInterface::class)); |
|
| 68 | +}); |
|
| 69 | +$this->set(CodeGenerator::class, function($di) { |
|
| 70 | +return new CodeGenerator(Jaxon::VERSION, $di->g(Container::class), |
|
| 71 | + $di->g(TemplateEngine::class)); |
|
| 72 | +}); |
|
| 73 | +$this->set(ConfigScriptGenerator::class, function($di) { |
|
| 74 | +return new ConfigScriptGenerator($di->g(ParameterReader::class), |
|
| 75 | + $di->g(TemplateEngine::class), $di->g(ConfigManager::class)); |
|
| 76 | +}); |
|
| 77 | +$this->set(ReadyScriptGenerator::class, function($di) { |
|
| 78 | +return new ReadyScriptGenerator(); |
|
| 79 | +}); |
|
| 80 | + |
|
| 81 | +// Script response plugin |
|
| 82 | +$this->set(ScriptPlugin::class, function($di) { |
|
| 83 | +return new ScriptPlugin($di->g(CallFactory::class)); |
|
| 84 | +}); |
|
| 85 | +// Databag response plugin |
|
| 86 | +$this->set(DatabagPlugin::class, function($di) { |
|
| 87 | +return new DatabagPlugin($di->g(Container::class)); |
|
| 88 | +}); |
|
| 89 | +// Dialog response plugin |
|
| 90 | +$this->set(DialogPlugin::class, function($di) { |
|
| 91 | +return new DialogPlugin($di->g(DialogCommand::class)); |
|
| 92 | +}); |
|
| 93 | +// Paginator response plugin |
|
| 94 | +$this->set(PaginatorPlugin::class, function($di) { |
|
| 95 | +return new PaginatorPlugin($di->g(RendererInterface::class)); |
|
| 96 | +}); |
|
| 97 | +} |
|
| 98 | + |
|
| 99 | +/** |
|
| 100 | 100 | * Get the plugin manager |
| 101 | 101 | * |
| 102 | 102 | * @return PluginManager |
| 103 | 103 | */ |
| 104 | - public function getPluginManager(): PluginManager |
|
| 105 | - { |
|
| 106 | - return $this->g(PluginManager::class); |
|
| 107 | - } |
|
| 104 | +public function getPluginManager(): PluginManager |
|
| 105 | +{ |
|
| 106 | +return $this->g(PluginManager::class); |
|
| 107 | +} |
|
| 108 | 108 | |
| 109 | - /** |
|
| 109 | +/** |
|
| 110 | 110 | * Get the package manager |
| 111 | 111 | * |
| 112 | 112 | * @return PackageManager |
| 113 | 113 | */ |
| 114 | - public function getPackageManager(): PackageManager |
|
| 115 | - { |
|
| 116 | - return $this->g(PackageManager::class); |
|
| 117 | - } |
|
| 114 | +public function getPackageManager(): PackageManager |
|
| 115 | +{ |
|
| 116 | +return $this->g(PackageManager::class); |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - /** |
|
| 119 | +/** |
|
| 120 | 120 | * Get the code generator |
| 121 | 121 | * |
| 122 | 122 | * @return CodeGenerator |
| 123 | 123 | */ |
| 124 | - public function getCodeGenerator(): CodeGenerator |
|
| 125 | - { |
|
| 126 | - return $this->g(CodeGenerator::class); |
|
| 127 | - } |
|
| 124 | +public function getCodeGenerator(): CodeGenerator |
|
| 125 | +{ |
|
| 126 | +return $this->g(CodeGenerator::class); |
|
| 127 | +} |
|
| 128 | 128 | |
| 129 | - /** |
|
| 129 | +/** |
|
| 130 | 130 | * Get the asset manager |
| 131 | 131 | * |
| 132 | 132 | * @return AssetManager |
| 133 | 133 | */ |
| 134 | - public function getAssetManager(): AssetManager |
|
| 135 | - { |
|
| 136 | - return $this->g(AssetManager::class); |
|
| 137 | - } |
|
| 134 | +public function getAssetManager(): AssetManager |
|
| 135 | +{ |
|
| 136 | +return $this->g(AssetManager::class); |
|
| 137 | +} |
|
| 138 | 138 | |
| 139 | - /** |
|
| 139 | +/** |
|
| 140 | 140 | * Get the jQuery plugin |
| 141 | 141 | * |
| 142 | 142 | * @return ScriptPlugin |
| 143 | 143 | */ |
| 144 | - public function getScriptPlugin(): ScriptPlugin |
|
| 145 | - { |
|
| 146 | - return $this->g(ScriptPlugin::class); |
|
| 147 | - } |
|
| 144 | +public function getScriptPlugin(): ScriptPlugin |
|
| 145 | +{ |
|
| 146 | +return $this->g(ScriptPlugin::class); |
|
| 147 | +} |
|
| 148 | 148 | |
| 149 | - /** |
|
| 149 | +/** |
|
| 150 | 150 | * Get the dialog plugin |
| 151 | 151 | * |
| 152 | 152 | * @return DialogPlugin |
| 153 | 153 | */ |
| 154 | - public function getDialogPlugin(): DialogPlugin |
|
| 155 | - { |
|
| 156 | - return $this->g(DialogPlugin::class); |
|
| 157 | - } |
|
| 154 | +public function getDialogPlugin(): DialogPlugin |
|
| 155 | +{ |
|
| 156 | +return $this->g(DialogPlugin::class); |
|
| 157 | +} |
|
| 158 | 158 | |
| 159 | - /** |
|
| 159 | +/** |
|
| 160 | 160 | * @param class-string $sClassName The package class name |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - private function getPackageConfigKey(string $sClassName): string |
|
| 165 | - { |
|
| 166 | - return $sClassName . '_PackageConfig'; |
|
| 167 | - } |
|
| 164 | +private function getPackageConfigKey(string $sClassName): string |
|
| 165 | +{ |
|
| 166 | +return $sClassName . '_PackageConfig'; |
|
| 167 | +} |
|
| 168 | 168 | |
| 169 | - /** |
|
| 169 | +/** |
|
| 170 | 170 | * @param class-string $sClassName The package class name |
| 171 | 171 | * @param-closure-this AbstractPackage $cSetter |
| 172 | 172 | * |
| 173 | 173 | * @return void |
| 174 | 174 | */ |
| 175 | - private function extendPackage(string $sClassName, Closure $cSetter): void |
|
| 176 | - { |
|
| 177 | - // Initialize the package instance. |
|
| 178 | - $this->xLibContainer->extend($sClassName, function($xPackage) use($cSetter) { |
|
| 179 | - // Allow the setter to access protected attributes. |
|
| 180 | - call_user_func($cSetter->bindTo($xPackage, $xPackage)); |
|
| 181 | - return $xPackage; |
|
| 182 | - }); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 175 | +private function extendPackage(string $sClassName, Closure $cSetter): void |
|
| 176 | +{ |
|
| 177 | +// Initialize the package instance. |
|
| 178 | +$this->xLibContainer->extend($sClassName, function($xPackage) use($cSetter) { |
|
| 179 | +// Allow the setter to access protected attributes. |
|
| 180 | +call_user_func($cSetter->bindTo($xPackage, $xPackage)); |
|
| 181 | +return $xPackage; |
|
| 182 | +}); |
|
| 183 | +} |
|
| 184 | + |
|
| 185 | +/** |
|
| 186 | 186 | * Register a package |
| 187 | 187 | * |
| 188 | 188 | * @param class-string $sClassName The package class name |
@@ -191,45 +191,45 @@ discard block |
||
| 191 | 191 | * @return void |
| 192 | 192 | * @throws SetupException |
| 193 | 193 | */ |
| 194 | - public function registerPackage(string $sClassName, array $aUserOptions): void |
|
| 195 | - { |
|
| 196 | - // Register the user class, but only if the user didn't already. |
|
| 197 | - if(!$this->h($sClassName)) |
|
| 198 | - { |
|
| 199 | - $this->set($sClassName, fn() => $this->make($sClassName)); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // Save the package config in the container. |
|
| 203 | - $sConfigKey = $this->getPackageConfigKey($sClassName); |
|
| 204 | - $this->set($sConfigKey, function($di) use($aUserOptions) { |
|
| 205 | - $xOptionsProvider = $aUserOptions['provider'] ?? null; |
|
| 206 | - // The user can provide a callable that returns the package options. |
|
| 207 | - if(is_callable($xOptionsProvider)) |
|
| 208 | - { |
|
| 209 | - $aUserOptions = $xOptionsProvider($aUserOptions); |
|
| 210 | - } |
|
| 211 | - return $di->g(ConfigManager::class)->newConfig($aUserOptions); |
|
| 212 | - }); |
|
| 213 | - |
|
| 214 | - // Initialize the package instance. |
|
| 215 | - $di = $this; |
|
| 216 | - $this->extendPackage($sClassName, function() use($di, $sConfigKey) { |
|
| 217 | - // $this here refers to the AbstractPackage instance. |
|
| 218 | - $this->xPkgConfig = $di->g($sConfigKey); |
|
| 219 | - $this->xRenderer = $di->g(ViewRenderer::class); |
|
| 220 | - $this->init(); |
|
| 221 | - }); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 194 | +public function registerPackage(string $sClassName, array $aUserOptions): void |
|
| 195 | +{ |
|
| 196 | +// Register the user class, but only if the user didn't already. |
|
| 197 | +if(!$this->h($sClassName)) |
|
| 198 | +{ |
|
| 199 | +$this->set($sClassName, fn() => $this->make($sClassName)); |
|
| 200 | +} |
|
| 201 | + |
|
| 202 | +// Save the package config in the container. |
|
| 203 | +$sConfigKey = $this->getPackageConfigKey($sClassName); |
|
| 204 | +$this->set($sConfigKey, function($di) use($aUserOptions) { |
|
| 205 | +$xOptionsProvider = $aUserOptions['provider'] ?? null; |
|
| 206 | +// The user can provide a callable that returns the package options. |
|
| 207 | +if(is_callable($xOptionsProvider)) |
|
| 208 | +{ |
|
| 209 | + $aUserOptions = $xOptionsProvider($aUserOptions); |
|
| 210 | +} |
|
| 211 | +return $di->g(ConfigManager::class)->newConfig($aUserOptions); |
|
| 212 | +}); |
|
| 213 | + |
|
| 214 | +// Initialize the package instance. |
|
| 215 | +$di = $this; |
|
| 216 | +$this->extendPackage($sClassName, function() use($di, $sConfigKey) { |
|
| 217 | +// $this here refers to the AbstractPackage instance. |
|
| 218 | +$this->xPkgConfig = $di->g($sConfigKey); |
|
| 219 | +$this->xRenderer = $di->g(ViewRenderer::class); |
|
| 220 | +$this->init(); |
|
| 221 | +}); |
|
| 222 | +} |
|
| 223 | + |
|
| 224 | +/** |
|
| 225 | 225 | * Get the config of a package |
| 226 | 226 | * |
| 227 | 227 | * @param class-string $sClassName The package class name |
| 228 | 228 | * |
| 229 | 229 | * @return Config |
| 230 | 230 | */ |
| 231 | - public function getPackageConfig(string $sClassName): Config |
|
| 232 | - { |
|
| 233 | - return $this->g($this->getPackageConfigKey($sClassName)); |
|
| 234 | - } |
|
| 231 | +public function getPackageConfig(string $sClassName): Config |
|
| 232 | +{ |
|
| 233 | +return $this->g($this->getPackageConfigKey($sClassName)); |
|
| 234 | +} |
|
| 235 | 235 | } |
@@ -39,73 +39,73 @@ discard block |
||
| 39 | 39 | |
| 40 | 40 | class ComponentContainer |
| 41 | 41 | { |
| 42 | - use Traits\DiAutoTrait; |
|
| 43 | - use Traits\ComponentTrait; |
|
| 42 | +use Traits\DiAutoTrait; |
|
| 43 | +use Traits\ComponentTrait; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * The Dependency Injection Container for registered classes |
| 47 | 47 | * |
| 48 | 48 | * @var PimpleContainer |
| 49 | 49 | */ |
| 50 | - private $xContainer; |
|
| 50 | +private $xContainer; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 52 | +/** |
|
| 53 | 53 | * This will be set only when getting the object targetted by the ajax request. |
| 54 | 54 | * |
| 55 | 55 | * @var Target|null |
| 56 | 56 | */ |
| 57 | - private $xTarget = null; |
|
| 57 | +private $xTarget = null; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * The class constructor |
| 61 | 61 | * |
| 62 | 62 | * @param Container $di |
| 63 | 63 | */ |
| 64 | - public function __construct(private Container $di) |
|
| 65 | - { |
|
| 66 | - $this->xContainer = new PimpleContainer(); |
|
| 67 | - $this->val(ComponentContainer::class, $this); |
|
| 68 | - |
|
| 69 | - // Register the call factory for registered functions |
|
| 70 | - $this->set($this->getRequestFactoryKey(JxnCall::class), fn() => |
|
| 71 | - new JxnCall($this->di->g(ConfigManager::class) |
|
| 72 | - ->getOption('core.prefix.function', ''))); |
|
| 73 | - |
|
| 74 | - // Register the pagination component, but do not export to js. |
|
| 75 | - $this->saveComponent(Pagination::class, [ |
|
| 76 | - 'excluded' => true, |
|
| 77 | - 'separator' => '.', |
|
| 78 | - // The namespace has the same name as the Component class. |
|
| 79 | - 'namespace' => Component::class, |
|
| 80 | - ]); |
|
| 81 | - |
|
| 82 | - $this->setComponentPublicMethods('node', NodeComponent::class, ['item', 'html']); |
|
| 83 | - $this->setComponentPublicMethods('func', FuncComponent::class, ['paginator']); |
|
| 84 | - } |
|
| 64 | +public function __construct(private Container $di) |
|
| 65 | +{ |
|
| 66 | +$this->xContainer = new PimpleContainer(); |
|
| 67 | +$this->val(ComponentContainer::class, $this); |
|
| 68 | + |
|
| 69 | +// Register the call factory for registered functions |
|
| 70 | +$this->set($this->getRequestFactoryKey(JxnCall::class), fn() => |
|
| 71 | +new JxnCall($this->di->g(ConfigManager::class) |
|
| 72 | + ->getOption('core.prefix.function', ''))); |
|
| 73 | + |
|
| 74 | +// Register the pagination component, but do not export to js. |
|
| 75 | +$this->saveComponent(Pagination::class, [ |
|
| 76 | +'excluded' => true, |
|
| 77 | +'separator' => '.', |
|
| 78 | +// The namespace has the same name as the Component class. |
|
| 79 | +'namespace' => Component::class, |
|
| 80 | +]); |
|
| 81 | + |
|
| 82 | +$this->setComponentPublicMethods('node', NodeComponent::class, ['item', 'html']); |
|
| 83 | +$this->setComponentPublicMethods('func', FuncComponent::class, ['paginator']); |
|
| 84 | +} |
|
| 85 | 85 | |
| 86 | - /** |
|
| 86 | +/** |
|
| 87 | 87 | * The container for parameters |
| 88 | 88 | * |
| 89 | 89 | * @return Container |
| 90 | 90 | */ |
| 91 | - protected function cn(): Container |
|
| 92 | - { |
|
| 93 | - return $this->di; |
|
| 94 | - } |
|
| 91 | +protected function cn(): Container |
|
| 92 | +{ |
|
| 93 | +return $this->di; |
|
| 94 | +} |
|
| 95 | 95 | |
| 96 | - /** |
|
| 96 | +/** |
|
| 97 | 97 | * Check if a class is defined in the container |
| 98 | 98 | * |
| 99 | 99 | * @param class-string $sClass The full class name |
| 100 | 100 | * |
| 101 | 101 | * @return bool |
| 102 | 102 | */ |
| 103 | - public function has(string $sClass): bool |
|
| 104 | - { |
|
| 105 | - return $this->xContainer->offsetExists($sClass); |
|
| 106 | - } |
|
| 103 | +public function has(string $sClass): bool |
|
| 104 | +{ |
|
| 105 | +return $this->xContainer->offsetExists($sClass); |
|
| 106 | +} |
|
| 107 | 107 | |
| 108 | - /** |
|
| 108 | +/** |
|
| 109 | 109 | * Save a closure in the container |
| 110 | 110 | * |
| 111 | 111 | * @param class-string $sClass The full class name |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return void |
| 115 | 115 | */ |
| 116 | - public function set(string $sClass, Closure $xClosure) |
|
| 117 | - { |
|
| 118 | - $this->xContainer->offsetSet($sClass, fn() => $xClosure($this)); |
|
| 119 | - } |
|
| 116 | +public function set(string $sClass, Closure $xClosure) |
|
| 117 | +{ |
|
| 118 | +$this->xContainer->offsetSet($sClass, fn() => $xClosure($this)); |
|
| 119 | +} |
|
| 120 | 120 | |
| 121 | - /** |
|
| 121 | +/** |
|
| 122 | 122 | * Save a value in the container |
| 123 | 123 | * |
| 124 | 124 | * @param string|class-string $sKey The key |
@@ -126,12 +126,12 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @return void |
| 128 | 128 | */ |
| 129 | - public function val(string $sKey, $xValue) |
|
| 130 | - { |
|
| 131 | - $this->xContainer->offsetSet($sKey, $xValue); |
|
| 132 | - } |
|
| 129 | +public function val(string $sKey, $xValue) |
|
| 130 | +{ |
|
| 131 | +$this->xContainer->offsetSet($sKey, $xValue); |
|
| 132 | +} |
|
| 133 | 133 | |
| 134 | - /** |
|
| 134 | +/** |
|
| 135 | 135 | * Get a class instance |
| 136 | 136 | * |
| 137 | 137 | * @template T |
@@ -139,12 +139,12 @@ discard block |
||
| 139 | 139 | * |
| 140 | 140 | * @return T |
| 141 | 141 | */ |
| 142 | - public function get(string $sClass) |
|
| 143 | - { |
|
| 144 | - return $this->xContainer->offsetGet($sClass); |
|
| 145 | - } |
|
| 142 | +public function get(string $sClass) |
|
| 143 | +{ |
|
| 144 | +return $this->xContainer->offsetGet($sClass); |
|
| 145 | +} |
|
| 146 | 146 | |
| 147 | - /** |
|
| 147 | +/** |
|
| 148 | 148 | * Get a component when one of its method needs to be called |
| 149 | 149 | * |
| 150 | 150 | * @template T |
@@ -153,17 +153,17 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @return T|null |
| 155 | 155 | */ |
| 156 | - public function getTargetComponent(string $sClassName, Target $xTarget): mixed |
|
| 157 | - { |
|
| 158 | - // Set the target only when getting the object targetted by the ajax request. |
|
| 159 | - $this->xTarget = $xTarget; |
|
| 160 | - $xComponent = $this->get($sClassName); |
|
| 161 | - $this->xTarget = null; |
|
| 156 | +public function getTargetComponent(string $sClassName, Target $xTarget): mixed |
|
| 157 | +{ |
|
| 158 | +// Set the target only when getting the object targetted by the ajax request. |
|
| 159 | +$this->xTarget = $xTarget; |
|
| 160 | +$xComponent = $this->get($sClassName); |
|
| 161 | +$this->xTarget = null; |
|
| 162 | 162 | |
| 163 | - return $xComponent; |
|
| 164 | - } |
|
| 163 | +return $xComponent; |
|
| 164 | +} |
|
| 165 | 165 | |
| 166 | - /** |
|
| 166 | +/** |
|
| 167 | 167 | * Register a component and its options |
| 168 | 168 | * |
| 169 | 169 | * @param class-string $sClassName The class name |
@@ -171,40 +171,40 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @return void |
| 173 | 173 | */ |
| 174 | - public function saveComponent(string $sClassName, array $aOptions): void |
|
| 175 | - { |
|
| 176 | - try |
|
| 177 | - { |
|
| 178 | - // Make sure the registered class exists |
|
| 179 | - if(isset($aOptions['include'])) |
|
| 180 | - { |
|
| 181 | - require_once $aOptions['include']; |
|
| 182 | - } |
|
| 183 | - $xReflectionClass = new ReflectionClass($sClassName); |
|
| 184 | - // Check if the class is registrable |
|
| 185 | - if(!$xReflectionClass->isInstantiable()) |
|
| 186 | - { |
|
| 187 | - return; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $this->_saveClassOptions($sClassName, $aOptions); |
|
| 191 | - |
|
| 192 | - $sClassKey = $this->getReflectionClassKey($sClassName); |
|
| 193 | - $this->val($sClassKey, $xReflectionClass); |
|
| 194 | - // Register the user class, but only if the user didn't already. |
|
| 195 | - if(!$this->has($sClassName)) |
|
| 196 | - { |
|
| 197 | - $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - catch(ReflectionException $e) |
|
| 201 | - { |
|
| 202 | - throw new SetupException($this->cn()->g(Translator::class) |
|
| 203 | - ->trans('errors.class.invalid', ['name' => $sClassName])); |
|
| 204 | - } |
|
| 205 | - } |
|
| 174 | +public function saveComponent(string $sClassName, array $aOptions): void |
|
| 175 | +{ |
|
| 176 | +try |
|
| 177 | +{ |
|
| 178 | +// Make sure the registered class exists |
|
| 179 | +if(isset($aOptions['include'])) |
|
| 180 | +{ |
|
| 181 | + require_once $aOptions['include']; |
|
| 182 | +} |
|
| 183 | +$xReflectionClass = new ReflectionClass($sClassName); |
|
| 184 | +// Check if the class is registrable |
|
| 185 | +if(!$xReflectionClass->isInstantiable()) |
|
| 186 | +{ |
|
| 187 | + return; |
|
| 188 | +} |
|
| 189 | + |
|
| 190 | +$this->_saveClassOptions($sClassName, $aOptions); |
|
| 206 | 191 | |
| 207 | - /** |
|
| 192 | +$sClassKey = $this->getReflectionClassKey($sClassName); |
|
| 193 | +$this->val($sClassKey, $xReflectionClass); |
|
| 194 | +// Register the user class, but only if the user didn't already. |
|
| 195 | +if(!$this->has($sClassName)) |
|
| 196 | +{ |
|
| 197 | + $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
|
| 198 | +} |
|
| 199 | +} |
|
| 200 | +catch(ReflectionException $e) |
|
| 201 | +{ |
|
| 202 | +throw new SetupException($this->cn()->g(Translator::class) |
|
| 203 | + ->trans('errors.class.invalid', ['name' => $sClassName])); |
|
| 204 | +} |
|
| 205 | +} |
|
| 206 | + |
|
| 207 | +/** |
|
| 208 | 208 | * Register a component |
| 209 | 209 | * |
| 210 | 210 | * @param string $sComponentId The component name |
@@ -212,74 +212,74 @@ discard block |
||
| 212 | 212 | * @return string |
| 213 | 213 | * @throws SetupException |
| 214 | 214 | */ |
| 215 | - private function _registerComponent(string $sComponentId): string |
|
| 216 | - { |
|
| 217 | - // Replace all separators ('.' or '_') with antislashes, and trim the class name. |
|
| 218 | - $sClassName = trim(str_replace(['.', '_'], '\\', $sComponentId), '\\'); |
|
| 219 | - |
|
| 220 | - $sComponentObject = $this->getCallableObjectKey($sClassName); |
|
| 221 | - // Prevent duplication. It's important not to use the class name here. |
|
| 222 | - if($this->has($sComponentObject)) |
|
| 223 | - { |
|
| 224 | - return $sClassName; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Register the helper class |
|
| 228 | - $this->set($this->getCallableHelperKey($sClassName), function() use($sClassName) { |
|
| 229 | - $xFactory = $this->di->getCallFactory(); |
|
| 230 | - return new ComponentHelper($this, $xFactory->rq($sClassName), |
|
| 231 | - $xFactory, $this->di->getViewRenderer(), |
|
| 232 | - $this->di->getLogger(), $this->di->getSessionManager(), |
|
| 233 | - $this->di->getStash(), $this->di->getUploadHandler()); |
|
| 234 | - }); |
|
| 235 | - |
|
| 236 | - $this->discoverComponent($sClassName); |
|
| 237 | - |
|
| 238 | - // Register the callable object |
|
| 239 | - $this->set($sComponentObject, function() use($sComponentId, $sClassName) { |
|
| 240 | - $aOptions = $this->_getClassOptions($sComponentId); |
|
| 241 | - $xReflectionClass = $this->get($this->getReflectionClassKey($sClassName)); |
|
| 242 | - $xOptions = $this->getComponentOptions($xReflectionClass, $aOptions); |
|
| 243 | - return new CallableObject($this, $this->di, $xReflectionClass, $xOptions); |
|
| 244 | - }); |
|
| 245 | - |
|
| 246 | - // Initialize the user class instance |
|
| 247 | - $this->xContainer->extend($sClassName, function($xClassInstance) use($sClassName) { |
|
| 248 | - if($xClassInstance instanceof AbstractComponent) |
|
| 249 | - { |
|
| 250 | - $xHelper = $this->get($this->getCallableHelperKey($sClassName)); |
|
| 251 | - $xHelper->xTarget = $this->xTarget; |
|
| 252 | - |
|
| 253 | - // Call the protected "initComponent()" method of the Component class. |
|
| 254 | - $cSetter = function($di, $xHelper) { |
|
| 255 | - $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 256 | - }; |
|
| 257 | - $cSetter = $cSetter->bindTo($xClassInstance, $xClassInstance); |
|
| 258 | - call_user_func($cSetter, $this->di, $xHelper); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - // Run the callbacks for class initialisation |
|
| 262 | - $this->di->g(CallbackManager::class)->onInit($xClassInstance); |
|
| 263 | - |
|
| 264 | - // Set attributes from the DI container. |
|
| 265 | - // The class level DI options are set on any component. |
|
| 266 | - // The method level DI options are set only on the targetted component. |
|
| 267 | - /** @var CallableObject */ |
|
| 268 | - $xCallableObject = $this->get($this->getCallableObjectKey($sClassName)); |
|
| 269 | - $xCallableObject->setDiClassAttributes($xClassInstance); |
|
| 270 | - if($this->xTarget !== null) |
|
| 271 | - { |
|
| 272 | - $sMethodName = $this->xTarget->getMethodName(); |
|
| 273 | - $xCallableObject->setDiMethodAttributes($xClassInstance, $sMethodName); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return $xClassInstance; |
|
| 277 | - }); |
|
| 278 | - |
|
| 279 | - return $sClassName; |
|
| 280 | - } |
|
| 215 | +private function _registerComponent(string $sComponentId): string |
|
| 216 | +{ |
|
| 217 | +// Replace all separators ('.' or '_') with antislashes, and trim the class name. |
|
| 218 | +$sClassName = trim(str_replace(['.', '_'], '\\', $sComponentId), '\\'); |
|
| 219 | + |
|
| 220 | +$sComponentObject = $this->getCallableObjectKey($sClassName); |
|
| 221 | +// Prevent duplication. It's important not to use the class name here. |
|
| 222 | +if($this->has($sComponentObject)) |
|
| 223 | +{ |
|
| 224 | +return $sClassName; |
|
| 225 | +} |
|
| 226 | + |
|
| 227 | +// Register the helper class |
|
| 228 | +$this->set($this->getCallableHelperKey($sClassName), function() use($sClassName) { |
|
| 229 | +$xFactory = $this->di->getCallFactory(); |
|
| 230 | +return new ComponentHelper($this, $xFactory->rq($sClassName), |
|
| 231 | + $xFactory, $this->di->getViewRenderer(), |
|
| 232 | + $this->di->getLogger(), $this->di->getSessionManager(), |
|
| 233 | + $this->di->getStash(), $this->di->getUploadHandler()); |
|
| 234 | +}); |
|
| 235 | + |
|
| 236 | +$this->discoverComponent($sClassName); |
|
| 237 | + |
|
| 238 | +// Register the callable object |
|
| 239 | +$this->set($sComponentObject, function() use($sComponentId, $sClassName) { |
|
| 240 | +$aOptions = $this->_getClassOptions($sComponentId); |
|
| 241 | +$xReflectionClass = $this->get($this->getReflectionClassKey($sClassName)); |
|
| 242 | +$xOptions = $this->getComponentOptions($xReflectionClass, $aOptions); |
|
| 243 | +return new CallableObject($this, $this->di, $xReflectionClass, $xOptions); |
|
| 244 | +}); |
|
| 245 | + |
|
| 246 | +// Initialize the user class instance |
|
| 247 | +$this->xContainer->extend($sClassName, function($xClassInstance) use($sClassName) { |
|
| 248 | +if($xClassInstance instanceof AbstractComponent) |
|
| 249 | +{ |
|
| 250 | + $xHelper = $this->get($this->getCallableHelperKey($sClassName)); |
|
| 251 | + $xHelper->xTarget = $this->xTarget; |
|
| 252 | + |
|
| 253 | + // Call the protected "initComponent()" method of the Component class. |
|
| 254 | + $cSetter = function($di, $xHelper) { |
|
| 255 | + $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 256 | + }; |
|
| 257 | + $cSetter = $cSetter->bindTo($xClassInstance, $xClassInstance); |
|
| 258 | + call_user_func($cSetter, $this->di, $xHelper); |
|
| 259 | +} |
|
| 260 | + |
|
| 261 | +// Run the callbacks for class initialisation |
|
| 262 | +$this->di->g(CallbackManager::class)->onInit($xClassInstance); |
|
| 263 | + |
|
| 264 | +// Set attributes from the DI container. |
|
| 265 | +// The class level DI options are set on any component. |
|
| 266 | +// The method level DI options are set only on the targetted component. |
|
| 267 | +/** @var CallableObject */ |
|
| 268 | +$xCallableObject = $this->get($this->getCallableObjectKey($sClassName)); |
|
| 269 | +$xCallableObject->setDiClassAttributes($xClassInstance); |
|
| 270 | +if($this->xTarget !== null) |
|
| 271 | +{ |
|
| 272 | + $sMethodName = $this->xTarget->getMethodName(); |
|
| 273 | + $xCallableObject->setDiMethodAttributes($xClassInstance, $sMethodName); |
|
| 274 | +} |
|
| 275 | + |
|
| 276 | +return $xClassInstance; |
|
| 277 | +}); |
|
| 281 | 278 | |
| 282 | - /** |
|
| 279 | +return $sClassName; |
|
| 280 | +} |
|
| 281 | + |
|
| 282 | +/** |
|
| 283 | 283 | * Get the callable object for a given class |
| 284 | 284 | * The callable object is registered if it is not already in the DI. |
| 285 | 285 | * |
@@ -288,13 +288,13 @@ discard block |
||
| 288 | 288 | * @return CallableObject|null |
| 289 | 289 | * @throws SetupException |
| 290 | 290 | */ |
| 291 | - public function makeCallableObject(string $sComponentId): ?CallableObject |
|
| 292 | - { |
|
| 293 | - $sClassName = $this->_registerComponent($sComponentId); |
|
| 294 | - return $this->get($this->getCallableObjectKey($sClassName)); |
|
| 295 | - } |
|
| 291 | +public function makeCallableObject(string $sComponentId): ?CallableObject |
|
| 292 | +{ |
|
| 293 | +$sClassName = $this->_registerComponent($sComponentId); |
|
| 294 | +return $this->get($this->getCallableObjectKey($sClassName)); |
|
| 295 | +} |
|
| 296 | 296 | |
| 297 | - /** |
|
| 297 | +/** |
|
| 298 | 298 | * Get an instance of a component by name |
| 299 | 299 | * |
| 300 | 300 | * @template T |
@@ -303,53 +303,53 @@ discard block |
||
| 303 | 303 | * @return T|null |
| 304 | 304 | * @throws SetupException |
| 305 | 305 | */ |
| 306 | - public function makeComponent(string $sClassName): mixed |
|
| 307 | - { |
|
| 308 | - $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 309 | - $sClassName = $this->_registerComponent($sComponentId); |
|
| 310 | - return $this->get($sClassName); |
|
| 311 | - } |
|
| 306 | +public function makeComponent(string $sClassName): mixed |
|
| 307 | +{ |
|
| 308 | +$sComponentId = str_replace('\\', '.', $sClassName); |
|
| 309 | +$sClassName = $this->_registerComponent($sComponentId); |
|
| 310 | +return $this->get($sClassName); |
|
| 311 | +} |
|
| 312 | 312 | |
| 313 | - /** |
|
| 313 | +/** |
|
| 314 | 314 | * Get a factory for a call to a registered function. |
| 315 | 315 | * |
| 316 | 316 | * @return JxnCall |
| 317 | 317 | */ |
| 318 | - public function getFunctionRequestFactory(): JxnCall |
|
| 319 | - { |
|
| 320 | - return $this->get($this->getRequestFactoryKey(JxnCall::class)); |
|
| 321 | - } |
|
| 318 | +public function getFunctionRequestFactory(): JxnCall |
|
| 319 | +{ |
|
| 320 | +return $this->get($this->getRequestFactoryKey(JxnCall::class)); |
|
| 321 | +} |
|
| 322 | 322 | |
| 323 | - /** |
|
| 323 | +/** |
|
| 324 | 324 | * Get a factory for a call to a registered component. |
| 325 | 325 | * |
| 326 | 326 | * @param class-string $sClassName |
| 327 | 327 | * |
| 328 | 328 | * @return JxnCall|null |
| 329 | 329 | */ |
| 330 | - public function getComponentRequestFactory(string $sClassName): ?JxnCall |
|
| 330 | +public function getComponentRequestFactory(string $sClassName): ?JxnCall |
|
| 331 | +{ |
|
| 332 | +$sClassName = trim($sClassName, " \t"); |
|
| 333 | +if($sClassName === '') |
|
| 334 | +{ |
|
| 335 | +return null; |
|
| 336 | +} |
|
| 337 | + |
|
| 338 | +$sFactoryKey = $this->getRequestFactoryKey($sClassName); |
|
| 339 | +if(!$this->has($sFactoryKey)) |
|
| 340 | +{ |
|
| 341 | +$this->xContainer->offsetSet($sFactoryKey, function() use($sClassName) { |
|
| 342 | + $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 343 | + if(!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 331 | 344 | { |
| 332 | - $sClassName = trim($sClassName, " \t"); |
|
| 333 | - if($sClassName === '') |
|
| 334 | - { |
|
| 335 | - return null; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $sFactoryKey = $this->getRequestFactoryKey($sClassName); |
|
| 339 | - if(!$this->has($sFactoryKey)) |
|
| 340 | - { |
|
| 341 | - $this->xContainer->offsetSet($sFactoryKey, function() use($sClassName) { |
|
| 342 | - $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 343 | - if(!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 344 | - { |
|
| 345 | - return null; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $xConfigManager = $this->di->g(ConfigManager::class); |
|
| 349 | - $sPrefix = $xConfigManager->getOption('core.prefix.class', ''); |
|
| 350 | - return new JxnClassCall($sPrefix . $xCallable->getJsName()); |
|
| 351 | - }); |
|
| 352 | - } |
|
| 353 | - return $this->get($sFactoryKey); |
|
| 345 | + return null; |
|
| 354 | 346 | } |
| 347 | + |
|
| 348 | + $xConfigManager = $this->di->g(ConfigManager::class); |
|
| 349 | + $sPrefix = $xConfigManager->getOption('core.prefix.class', ''); |
|
| 350 | + return new JxnClassCall($sPrefix . $xCallable->getJsName()); |
|
| 351 | +}); |
|
| 352 | +} |
|
| 353 | +return $this->get($sFactoryKey); |
|
| 354 | +} |
|
| 355 | 355 | } |
@@ -32,145 +32,145 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | class Container |
| 34 | 34 | { |
| 35 | - use Traits\AppTrait; |
|
| 36 | - use Traits\PsrTrait; |
|
| 37 | - use Traits\RequestTrait; |
|
| 38 | - use Traits\ResponseTrait; |
|
| 39 | - use Traits\PluginTrait; |
|
| 40 | - use Traits\CallableTrait; |
|
| 41 | - use Traits\ViewTrait; |
|
| 42 | - use Traits\UtilTrait; |
|
| 43 | - use Traits\MetadataTrait; |
|
| 44 | - use Traits\DiAutoTrait; |
|
| 35 | +use Traits\AppTrait; |
|
| 36 | +use Traits\PsrTrait; |
|
| 37 | +use Traits\RequestTrait; |
|
| 38 | +use Traits\ResponseTrait; |
|
| 39 | +use Traits\PluginTrait; |
|
| 40 | +use Traits\CallableTrait; |
|
| 41 | +use Traits\ViewTrait; |
|
| 42 | +use Traits\UtilTrait; |
|
| 43 | +use Traits\MetadataTrait; |
|
| 44 | +use Traits\DiAutoTrait; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * The library Dependency Injection Container |
| 48 | 48 | * |
| 49 | 49 | * @var PimpleContainer |
| 50 | 50 | */ |
| 51 | - private $xLibContainer; |
|
| 51 | +private $xLibContainer; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * The application or framework Dependency Injection Container |
| 55 | 55 | * |
| 56 | 56 | * @var ContainerInterface |
| 57 | 57 | */ |
| 58 | - private $xAppContainer = null; |
|
| 58 | +private $xAppContainer = null; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The class constructor |
| 62 | 62 | */ |
| 63 | - public function __construct() |
|
| 64 | - { |
|
| 65 | - $this->xLibContainer = new PimpleContainer(); |
|
| 63 | +public function __construct() |
|
| 64 | +{ |
|
| 65 | +$this->xLibContainer = new PimpleContainer(); |
|
| 66 | 66 | |
| 67 | - $this->val(Container::class, $this); |
|
| 67 | +$this->val(Container::class, $this); |
|
| 68 | 68 | |
| 69 | - // Register the null logger by default |
|
| 70 | - $this->setLogger(new NullLogger()); |
|
| 69 | +// Register the null logger by default |
|
| 70 | +$this->setLogger(new NullLogger()); |
|
| 71 | 71 | |
| 72 | - // Template directory |
|
| 73 | - $sTemplateDir = realpath(__DIR__ . '/../../templates'); |
|
| 74 | - $this->val('jaxon.core.dir.template', $sTemplateDir); |
|
| 72 | +// Template directory |
|
| 73 | +$sTemplateDir = realpath(__DIR__ . '/../../templates'); |
|
| 74 | +$this->val('jaxon.core.dir.template', $sTemplateDir); |
|
| 75 | 75 | |
| 76 | - // Translation directory |
|
| 77 | - $sTranslationDir = realpath(__DIR__ . '/../../translations'); |
|
| 78 | - $this->val('jaxon.core.dir.translation', $sTranslationDir); |
|
| 76 | +// Translation directory |
|
| 77 | +$sTranslationDir = realpath(__DIR__ . '/../../translations'); |
|
| 78 | +$this->val('jaxon.core.dir.translation', $sTranslationDir); |
|
| 79 | 79 | |
| 80 | - $this->registerAll(); |
|
| 81 | - $this->setEventHandlers(); |
|
| 82 | - } |
|
| 80 | +$this->registerAll(); |
|
| 81 | +$this->setEventHandlers(); |
|
| 82 | +} |
|
| 83 | 83 | |
| 84 | - /** |
|
| 84 | +/** |
|
| 85 | 85 | * The container for parameters |
| 86 | 86 | * |
| 87 | 87 | * @return Container |
| 88 | 88 | */ |
| 89 | - protected function cn(): Container |
|
| 90 | - { |
|
| 91 | - return $this; |
|
| 92 | - } |
|
| 89 | +protected function cn(): Container |
|
| 90 | +{ |
|
| 91 | +return $this; |
|
| 92 | +} |
|
| 93 | 93 | |
| 94 | - /** |
|
| 94 | +/** |
|
| 95 | 95 | * Register the values into the container |
| 96 | 96 | * |
| 97 | 97 | * @return void |
| 98 | 98 | */ |
| 99 | - private function registerAll() |
|
| 100 | - { |
|
| 101 | - $this->registerApp(); |
|
| 102 | - $this->registerPsr(); |
|
| 103 | - $this->registerRequests(); |
|
| 104 | - $this->registerResponses(); |
|
| 105 | - $this->registerPlugins(); |
|
| 106 | - $this->registerCallables(); |
|
| 107 | - $this->registerViews(); |
|
| 108 | - $this->registerUtils(); |
|
| 109 | - $this->registerMetadataReader(); |
|
| 110 | - } |
|
| 99 | +private function registerAll() |
|
| 100 | +{ |
|
| 101 | +$this->registerApp(); |
|
| 102 | +$this->registerPsr(); |
|
| 103 | +$this->registerRequests(); |
|
| 104 | +$this->registerResponses(); |
|
| 105 | +$this->registerPlugins(); |
|
| 106 | +$this->registerCallables(); |
|
| 107 | +$this->registerViews(); |
|
| 108 | +$this->registerUtils(); |
|
| 109 | +$this->registerMetadataReader(); |
|
| 110 | +} |
|
| 111 | 111 | |
| 112 | - /** |
|
| 112 | +/** |
|
| 113 | 113 | * Set the logger |
| 114 | 114 | * |
| 115 | 115 | * @param LoggerInterface|Closure $xLogger |
| 116 | 116 | * |
| 117 | 117 | * @return void |
| 118 | 118 | */ |
| 119 | - public function setLogger(LoggerInterface|Closure $xLogger) |
|
| 120 | - { |
|
| 121 | - is_a($xLogger, LoggerInterface::class) ? |
|
| 122 | - $this->val(LoggerInterface::class, $xLogger) : |
|
| 123 | - $this->set(LoggerInterface::class, $xLogger); |
|
| 124 | - } |
|
| 119 | +public function setLogger(LoggerInterface|Closure $xLogger) |
|
| 120 | +{ |
|
| 121 | +is_a($xLogger, LoggerInterface::class) ? |
|
| 122 | +$this->val(LoggerInterface::class, $xLogger) : |
|
| 123 | +$this->set(LoggerInterface::class, $xLogger); |
|
| 124 | +} |
|
| 125 | 125 | |
| 126 | - /** |
|
| 126 | +/** |
|
| 127 | 127 | * Get the logger |
| 128 | 128 | * |
| 129 | 129 | * @return LoggerInterface |
| 130 | 130 | */ |
| 131 | - public function getLogger(): LoggerInterface |
|
| 132 | - { |
|
| 133 | - return $this->get(LoggerInterface::class); |
|
| 134 | - } |
|
| 131 | +public function getLogger(): LoggerInterface |
|
| 132 | +{ |
|
| 133 | +return $this->get(LoggerInterface::class); |
|
| 134 | +} |
|
| 135 | 135 | |
| 136 | - /** |
|
| 136 | +/** |
|
| 137 | 137 | * Set the container provided by the integrated framework |
| 138 | 138 | * |
| 139 | 139 | * @param ContainerInterface $xContainer The container implementation |
| 140 | 140 | * |
| 141 | 141 | * @return void |
| 142 | 142 | */ |
| 143 | - public function setContainer(ContainerInterface $xContainer) |
|
| 144 | - { |
|
| 145 | - $this->xAppContainer = $xContainer; |
|
| 146 | - } |
|
| 143 | +public function setContainer(ContainerInterface $xContainer) |
|
| 144 | +{ |
|
| 145 | +$this->xAppContainer = $xContainer; |
|
| 146 | +} |
|
| 147 | 147 | |
| 148 | - /** |
|
| 148 | +/** |
|
| 149 | 149 | * Check if a class is defined in the container |
| 150 | 150 | * |
| 151 | 151 | * @param string $sClass The full class name |
| 152 | 152 | * |
| 153 | 153 | * @return bool |
| 154 | 154 | */ |
| 155 | - public function h(string $sClass): bool |
|
| 156 | - { |
|
| 157 | - return $this->xLibContainer->offsetExists($sClass); |
|
| 158 | - } |
|
| 155 | +public function h(string $sClass): bool |
|
| 156 | +{ |
|
| 157 | +return $this->xLibContainer->offsetExists($sClass); |
|
| 158 | +} |
|
| 159 | 159 | |
| 160 | - /** |
|
| 160 | +/** |
|
| 161 | 161 | * Check if a class is defined in the container |
| 162 | 162 | * |
| 163 | 163 | * @param string $sClass The full class name |
| 164 | 164 | * |
| 165 | 165 | * @return bool |
| 166 | 166 | */ |
| 167 | - public function has(string $sClass): bool |
|
| 168 | - { |
|
| 169 | - return $this->xAppContainer != null && $this->xAppContainer->has($sClass) ? |
|
| 170 | - true : $this->xLibContainer->offsetExists($sClass); |
|
| 171 | - } |
|
| 167 | +public function has(string $sClass): bool |
|
| 168 | +{ |
|
| 169 | +return $this->xAppContainer != null && $this->xAppContainer->has($sClass) ? |
|
| 170 | +true : $this->xLibContainer->offsetExists($sClass); |
|
| 171 | +} |
|
| 172 | 172 | |
| 173 | - /** |
|
| 173 | +/** |
|
| 174 | 174 | * Get a class instance |
| 175 | 175 | * |
| 176 | 176 | * @template T |
@@ -178,12 +178,12 @@ discard block |
||
| 178 | 178 | * |
| 179 | 179 | * @return T |
| 180 | 180 | */ |
| 181 | - public function g(string $sClass): mixed |
|
| 182 | - { |
|
| 183 | - return $this->xLibContainer->offsetGet($sClass); |
|
| 184 | - } |
|
| 181 | +public function g(string $sClass): mixed |
|
| 182 | +{ |
|
| 183 | +return $this->xLibContainer->offsetGet($sClass); |
|
| 184 | +} |
|
| 185 | 185 | |
| 186 | - /** |
|
| 186 | +/** |
|
| 187 | 187 | * Get a class instance |
| 188 | 188 | * |
| 189 | 189 | * @template T |
@@ -192,24 +192,24 @@ discard block |
||
| 192 | 192 | * @return T |
| 193 | 193 | * @throws SetupException |
| 194 | 194 | */ |
| 195 | - public function get(string $sClass): mixed |
|
| 196 | - { |
|
| 197 | - try |
|
| 198 | - { |
|
| 199 | - return $this->xAppContainer != null && $this->xAppContainer->has($sClass) ? |
|
| 200 | - $this->xAppContainer->get($sClass) : $this->xLibContainer->offsetGet($sClass); |
|
| 201 | - } |
|
| 202 | - catch(Throwable $e) |
|
| 203 | - { |
|
| 204 | - $xLogger = $this->g(LoggerInterface::class); |
|
| 205 | - $xTranslator = $this->g(Translator::class); |
|
| 206 | - $sMessage = $e->getMessage() . ': ' . $xTranslator->trans('errors.class.container', ['name' => $sClass]); |
|
| 207 | - $xLogger->error($e->getMessage(), ['message' => $sMessage]); |
|
| 208 | - throw new SetupException($sMessage); |
|
| 209 | - } |
|
| 210 | - } |
|
| 195 | +public function get(string $sClass): mixed |
|
| 196 | +{ |
|
| 197 | +try |
|
| 198 | +{ |
|
| 199 | +return $this->xAppContainer != null && $this->xAppContainer->has($sClass) ? |
|
| 200 | + $this->xAppContainer->get($sClass) : $this->xLibContainer->offsetGet($sClass); |
|
| 201 | +} |
|
| 202 | +catch(Throwable $e) |
|
| 203 | +{ |
|
| 204 | +$xLogger = $this->g(LoggerInterface::class); |
|
| 205 | +$xTranslator = $this->g(Translator::class); |
|
| 206 | +$sMessage = $e->getMessage() . ': ' . $xTranslator->trans('errors.class.container', ['name' => $sClass]); |
|
| 207 | +$xLogger->error($e->getMessage(), ['message' => $sMessage]); |
|
| 208 | +throw new SetupException($sMessage); |
|
| 209 | +} |
|
| 210 | +} |
|
| 211 | 211 | |
| 212 | - /** |
|
| 212 | +/** |
|
| 213 | 213 | * Save a closure in the container |
| 214 | 214 | * |
| 215 | 215 | * @param string|class-string $sClass The full class name |
@@ -218,15 +218,15 @@ discard block |
||
| 218 | 218 | * |
| 219 | 219 | * @return void |
| 220 | 220 | */ |
| 221 | - public function set(string $sClass, Closure $xClosure, bool $bIsSingleton = true) |
|
| 222 | - { |
|
| 223 | - // Wrap the user closure into a new closure, so it can take this container as a parameter. |
|
| 224 | - $xClosure = fn() => $xClosure($this); |
|
| 225 | - $this->xLibContainer->offsetSet($sClass, $bIsSingleton ? |
|
| 226 | - $xClosure : $this->xLibContainer->factory($xClosure)); |
|
| 227 | - } |
|
| 221 | +public function set(string $sClass, Closure $xClosure, bool $bIsSingleton = true) |
|
| 222 | +{ |
|
| 223 | +// Wrap the user closure into a new closure, so it can take this container as a parameter. |
|
| 224 | +$xClosure = fn() => $xClosure($this); |
|
| 225 | +$this->xLibContainer->offsetSet($sClass, $bIsSingleton ? |
|
| 226 | +$xClosure : $this->xLibContainer->factory($xClosure)); |
|
| 227 | +} |
|
| 228 | 228 | |
| 229 | - /** |
|
| 229 | +/** |
|
| 230 | 230 | * Save a value in the container |
| 231 | 231 | * |
| 232 | 232 | * @param string|class-string $sKey The key |
@@ -234,12 +234,12 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return void |
| 236 | 236 | */ |
| 237 | - public function val(string $sKey, $xValue) |
|
| 238 | - { |
|
| 239 | - $this->xLibContainer->offsetSet($sKey, $xValue); |
|
| 240 | - } |
|
| 237 | +public function val(string $sKey, $xValue) |
|
| 238 | +{ |
|
| 239 | +$this->xLibContainer->offsetSet($sKey, $xValue); |
|
| 240 | +} |
|
| 241 | 241 | |
| 242 | - /** |
|
| 242 | +/** |
|
| 243 | 243 | * Set an alias in the container |
| 244 | 244 | * |
| 245 | 245 | * @param string|class-string $sAlias The alias name |
@@ -247,60 +247,60 @@ discard block |
||
| 247 | 247 | * |
| 248 | 248 | * @return void |
| 249 | 249 | */ |
| 250 | - public function alias(string $sAlias, string $sClass) |
|
| 251 | - { |
|
| 252 | - $this->set($sAlias, function($di) use ($sClass) { |
|
| 253 | - return $di->get($sClass); |
|
| 254 | - }); |
|
| 255 | - } |
|
| 250 | +public function alias(string $sAlias, string $sClass) |
|
| 251 | +{ |
|
| 252 | +$this->set($sAlias, function($di) use ($sClass) { |
|
| 253 | +return $di->get($sClass); |
|
| 254 | +}); |
|
| 255 | +} |
|
| 256 | 256 | |
| 257 | - /** |
|
| 257 | +/** |
|
| 258 | 258 | * @param ReflectionClass $xClass |
| 259 | 259 | * @param ReflectionParameter $xParameter |
| 260 | 260 | * |
| 261 | 261 | * @return mixed |
| 262 | 262 | * @throws SetupException |
| 263 | 263 | */ |
| 264 | - public function getParameter(ReflectionClass $xClass, ReflectionParameter $xParameter) |
|
| 265 | - { |
|
| 266 | - $xType = $xParameter->getType(); |
|
| 267 | - // Check the parameter class first. |
|
| 268 | - if($xType instanceof ReflectionNamedType) |
|
| 269 | - { |
|
| 270 | - // Check the class + the name |
|
| 271 | - if($this->has($xType->getName() . ' $' . $xParameter->getName())) |
|
| 272 | - { |
|
| 273 | - return $this->get($xType->getName() . ' $' . $xParameter->getName()); |
|
| 274 | - } |
|
| 275 | - // Check the class only |
|
| 276 | - if($this->has($xType->getName())) |
|
| 277 | - { |
|
| 278 | - return $this->get($xType->getName()); |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - // Check the name only |
|
| 282 | - return $this->get('$' . $xParameter->getName()); |
|
| 283 | - } |
|
| 264 | +public function getParameter(ReflectionClass $xClass, ReflectionParameter $xParameter) |
|
| 265 | +{ |
|
| 266 | +$xType = $xParameter->getType(); |
|
| 267 | +// Check the parameter class first. |
|
| 268 | +if($xType instanceof ReflectionNamedType) |
|
| 269 | +{ |
|
| 270 | +// Check the class + the name |
|
| 271 | +if($this->has($xType->getName() . ' $' . $xParameter->getName())) |
|
| 272 | +{ |
|
| 273 | + return $this->get($xType->getName() . ' $' . $xParameter->getName()); |
|
| 274 | +} |
|
| 275 | +// Check the class only |
|
| 276 | +if($this->has($xType->getName())) |
|
| 277 | +{ |
|
| 278 | + return $this->get($xType->getName()); |
|
| 279 | +} |
|
| 280 | +} |
|
| 281 | +// Check the name only |
|
| 282 | +return $this->get('$' . $xParameter->getName()); |
|
| 283 | +} |
|
| 284 | 284 | |
| 285 | - /** |
|
| 285 | +/** |
|
| 286 | 286 | * Get the session manager |
| 287 | 287 | * |
| 288 | 288 | * @return SessionInterface|null |
| 289 | 289 | */ |
| 290 | - public function getSessionManager(): ?SessionInterface |
|
| 291 | - { |
|
| 292 | - return $this->h(SessionInterface::class) ? $this->g(SessionInterface::class) : null; |
|
| 293 | - } |
|
| 290 | +public function getSessionManager(): ?SessionInterface |
|
| 291 | +{ |
|
| 292 | +return $this->h(SessionInterface::class) ? $this->g(SessionInterface::class) : null; |
|
| 293 | +} |
|
| 294 | 294 | |
| 295 | - /** |
|
| 295 | +/** |
|
| 296 | 296 | * Set the session manager |
| 297 | 297 | * |
| 298 | 298 | * @param Closure $xClosure A closure to create the session manager instance |
| 299 | 299 | * |
| 300 | 300 | * @return void |
| 301 | 301 | */ |
| 302 | - public function setSessionManager(Closure $xClosure) |
|
| 303 | - { |
|
| 304 | - $this->set(SessionInterface::class, $xClosure); |
|
| 305 | - } |
|
| 302 | +public function setSessionManager(Closure $xClosure) |
|
| 303 | +{ |
|
| 304 | +$this->set(SessionInterface::class, $xClosure); |
|
| 305 | +} |
|
| 306 | 306 | } |
@@ -13,25 +13,25 @@ |
||
| 13 | 13 | |
| 14 | 14 | class Page |
| 15 | 15 | { |
| 16 | - /** |
|
| 16 | +/** |
|
| 17 | 17 | * @var string |
| 18 | 18 | */ |
| 19 | - public $sType; |
|
| 19 | +public $sType; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * @var string |
| 23 | 23 | */ |
| 24 | - public $sText; |
|
| 24 | +public $sText; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * @var int |
| 28 | 28 | */ |
| 29 | - public $nNumber; |
|
| 29 | +public $nNumber; |
|
| 30 | 30 | |
| 31 | - public function __construct(string $sType, string $sText, int $nNumber) |
|
| 32 | - { |
|
| 33 | - $this->sType = $sType; |
|
| 34 | - $this->sText = $sText; |
|
| 35 | - $this->nNumber = $nNumber; |
|
| 36 | - } |
|
| 31 | +public function __construct(string $sType, string $sText, int $nNumber) |
|
| 32 | +{ |
|
| 33 | +$this->sType = $sType; |
|
| 34 | +$this->sText = $sText; |
|
| 35 | +$this->nNumber = $nNumber; |
|
| 36 | +} |
|
| 37 | 37 | } |
| 38 | 38 | \ No newline at end of file |
@@ -32,110 +32,110 @@ |
||
| 32 | 32 | |
| 33 | 33 | final class Jaxon |
| 34 | 34 | { |
| 35 | - use Traits\ConfigTrait; |
|
| 36 | - use Traits\ServicesTrait; |
|
| 37 | - use Traits\PluginTrait; |
|
| 38 | - use Traits\RequestTrait; |
|
| 39 | - use Traits\ResponseTrait; |
|
| 40 | - use Traits\SendResponseTrait; |
|
| 41 | - |
|
| 42 | - /** |
|
| 35 | +use Traits\ConfigTrait; |
|
| 36 | +use Traits\ServicesTrait; |
|
| 37 | +use Traits\PluginTrait; |
|
| 38 | +use Traits\RequestTrait; |
|
| 39 | +use Traits\ResponseTrait; |
|
| 40 | +use Traits\SendResponseTrait; |
|
| 41 | + |
|
| 42 | +/** |
|
| 43 | 43 | * @var Jaxon|null |
| 44 | 44 | */ |
| 45 | - private static $xInstance = null; |
|
| 45 | +private static $xInstance = null; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * The constructor |
| 49 | 49 | * |
| 50 | 50 | * @param Container $xContainer |
| 51 | 51 | * @param ComponentContainer $xComponentContainer |
| 52 | 52 | */ |
| 53 | - private function __construct(Container $xContainer, ComponentContainer $xComponentContainer) |
|
| 54 | - { |
|
| 55 | - $this->xContainer = $xContainer; |
|
| 56 | - $this->xComponentContainer = $xComponentContainer; |
|
| 57 | - } |
|
| 53 | +private function __construct(Container $xContainer, ComponentContainer $xComponentContainer) |
|
| 54 | +{ |
|
| 55 | +$this->xContainer = $xContainer; |
|
| 56 | +$this->xComponentContainer = $xComponentContainer; |
|
| 57 | +} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @return Jaxon |
| 61 | 61 | */ |
| 62 | - private static function createInstance(): Jaxon |
|
| 63 | - { |
|
| 64 | - $xContainer = new Container(); |
|
| 65 | - $xComponentContainer = new ComponentContainer($xContainer); |
|
| 66 | - self::$xInstance = new Jaxon($xContainer, $xComponentContainer); |
|
| 62 | +private static function createInstance(): Jaxon |
|
| 63 | +{ |
|
| 64 | +$xContainer = new Container(); |
|
| 65 | +$xComponentContainer = new ComponentContainer($xContainer); |
|
| 66 | +self::$xInstance = new Jaxon($xContainer, $xComponentContainer); |
|
| 67 | 67 | |
| 68 | - // Save the Jaxon and container instances |
|
| 69 | - $xContainer->val(Jaxon::class, self::$xInstance); |
|
| 70 | - $xContainer->val(ComponentContainer::class, $xComponentContainer); |
|
| 68 | +// Save the Jaxon and container instances |
|
| 69 | +$xContainer->val(Jaxon::class, self::$xInstance); |
|
| 70 | +$xContainer->val(ComponentContainer::class, $xComponentContainer); |
|
| 71 | 71 | |
| 72 | - // Make the helpers functions available in the global namespace. |
|
| 73 | - self::$xInstance->getCallbackManager()->boot(function() { |
|
| 74 | - self::$xInstance->config()->globals(); |
|
| 75 | - }); |
|
| 72 | +// Make the helpers functions available in the global namespace. |
|
| 73 | +self::$xInstance->getCallbackManager()->boot(function() { |
|
| 74 | +self::$xInstance->config()->globals(); |
|
| 75 | +}); |
|
| 76 | 76 | |
| 77 | - return self::$xInstance; |
|
| 78 | - } |
|
| 77 | +return self::$xInstance; |
|
| 78 | +} |
|
| 79 | 79 | |
| 80 | - /** |
|
| 80 | +/** |
|
| 81 | 81 | * @return Jaxon |
| 82 | 82 | */ |
| 83 | - public static function getInstance(): Jaxon |
|
| 84 | - { |
|
| 85 | - return self::$xInstance ?: self::$xInstance = self::createInstance(); |
|
| 86 | - } |
|
| 83 | +public static function getInstance(): Jaxon |
|
| 84 | +{ |
|
| 85 | +return self::$xInstance ?: self::$xInstance = self::createInstance(); |
|
| 86 | +} |
|
| 87 | 87 | |
| 88 | - /** |
|
| 88 | +/** |
|
| 89 | 89 | * @return string |
| 90 | 90 | */ |
| 91 | - public function getVersion(): string |
|
| 92 | - { |
|
| 93 | - return \Jaxon\Jaxon::VERSION; |
|
| 94 | - } |
|
| 91 | +public function getVersion(): string |
|
| 92 | +{ |
|
| 93 | +return \Jaxon\Jaxon::VERSION; |
|
| 94 | +} |
|
| 95 | 95 | |
| 96 | - /** |
|
| 96 | +/** |
|
| 97 | 97 | * @return ConfigManager |
| 98 | 98 | */ |
| 99 | - public function config(): ConfigManager |
|
| 100 | - { |
|
| 101 | - return $this->di()->g(ConfigManager::class); |
|
| 102 | - } |
|
| 99 | +public function config(): ConfigManager |
|
| 100 | +{ |
|
| 101 | +return $this->di()->g(ConfigManager::class); |
|
| 102 | +} |
|
| 103 | 103 | |
| 104 | - /** |
|
| 104 | +/** |
|
| 105 | 105 | * Set the ajax endpoint URI |
| 106 | 106 | * |
| 107 | 107 | * @param string $sUri The ajax endpoint URI |
| 108 | 108 | * |
| 109 | 109 | * @return void |
| 110 | 110 | */ |
| 111 | - public function setUri(string $sUri): void |
|
| 112 | - { |
|
| 113 | - $this->config()->setOption('core.request.uri', $sUri); |
|
| 114 | - } |
|
| 111 | +public function setUri(string $sUri): void |
|
| 112 | +{ |
|
| 113 | +$this->config()->setOption('core.request.uri', $sUri); |
|
| 114 | +} |
|
| 115 | 115 | |
| 116 | - /** |
|
| 116 | +/** |
|
| 117 | 117 | * @return AppInterface |
| 118 | 118 | */ |
| 119 | - public function app(): AppInterface |
|
| 120 | - { |
|
| 121 | - return $this->xContainer->getApp(); |
|
| 122 | - } |
|
| 119 | +public function app(): AppInterface |
|
| 120 | +{ |
|
| 121 | +return $this->xContainer->getApp(); |
|
| 122 | +} |
|
| 123 | 123 | |
| 124 | - /** |
|
| 124 | +/** |
|
| 125 | 125 | * Get the callback manager |
| 126 | 126 | * |
| 127 | 127 | * @return CallbackManager |
| 128 | 128 | */ |
| 129 | - public function callback(): CallbackManager |
|
| 130 | - { |
|
| 131 | - return $this->getCallbackManager(); |
|
| 132 | - } |
|
| 129 | +public function callback(): CallbackManager |
|
| 130 | +{ |
|
| 131 | +return $this->getCallbackManager(); |
|
| 132 | +} |
|
| 133 | 133 | |
| 134 | - /** |
|
| 134 | +/** |
|
| 135 | 135 | * @return void |
| 136 | 136 | */ |
| 137 | - public function reset(): void |
|
| 138 | - { |
|
| 139 | - self::$xInstance = null; |
|
| 140 | - } |
|
| 137 | +public function reset(): void |
|
| 138 | +{ |
|
| 139 | +self::$xInstance = null; |
|
| 140 | +} |
|
| 141 | 141 | } |
@@ -28,27 +28,27 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | trait PluginTrait |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * @return PluginManager |
| 33 | 33 | */ |
| 34 | - abstract public function getPluginManager(): PluginManager; |
|
| 34 | +abstract public function getPluginManager(): PluginManager; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 36 | +/** |
|
| 37 | 37 | * @return PackageManager |
| 38 | 38 | */ |
| 39 | - abstract public function getPackageManager(): PackageManager; |
|
| 39 | +abstract public function getPackageManager(): PackageManager; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 41 | +/** |
|
| 42 | 42 | * @return CodeGenerator |
| 43 | 43 | */ |
| 44 | - abstract public function getCodeGenerator(): CodeGenerator; |
|
| 44 | +abstract public function getCodeGenerator(): CodeGenerator; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * @return Bootstrap |
| 48 | 48 | */ |
| 49 | - abstract protected function getBootstrap(): Bootstrap; |
|
| 49 | +abstract protected function getBootstrap(): Bootstrap; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * Register request handlers, including functions, callable classes and directories. |
| 53 | 53 | * |
| 54 | 54 | * @param string $sType The type of request handler being registered |
@@ -65,14 +65,14 @@ discard block |
||
| 65 | 65 | * @return void |
| 66 | 66 | * @throws SetupException |
| 67 | 67 | */ |
| 68 | - public function register(string $sType, string $sName, $xOptions = []): void |
|
| 69 | - { |
|
| 70 | - // We need the library to have been bootstrapped. |
|
| 71 | - $this->getBootstrap()->onBoot(); |
|
| 72 | - $this->getPluginManager()->registerCallable($sType, $sName, $xOptions); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 68 | +public function register(string $sType, string $sName, $xOptions = []): void |
|
| 69 | +{ |
|
| 70 | +// We need the library to have been bootstrapped. |
|
| 71 | +$this->getBootstrap()->onBoot(); |
|
| 72 | +$this->getPluginManager()->registerCallable($sType, $sName, $xOptions); |
|
| 73 | +} |
|
| 74 | + |
|
| 75 | +/** |
|
| 76 | 76 | * Register a plugin |
| 77 | 77 | * |
| 78 | 78 | * Below is a table for priorities and their description: |
@@ -87,14 +87,14 @@ discard block |
||
| 87 | 87 | * @return void |
| 88 | 88 | * @throws SetupException |
| 89 | 89 | */ |
| 90 | - public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
|
| 91 | - { |
|
| 92 | - // We need the library to have been bootstrapped. |
|
| 93 | - $this->getBootstrap()->onBoot(); |
|
| 94 | - $this->getPluginManager()->registerPlugin($sClassName, $sPluginName, $nPriority); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 90 | +public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
|
| 91 | +{ |
|
| 92 | +// We need the library to have been bootstrapped. |
|
| 93 | +$this->getBootstrap()->onBoot(); |
|
| 94 | +$this->getPluginManager()->registerPlugin($sClassName, $sPluginName, $nPriority); |
|
| 95 | +} |
|
| 96 | + |
|
| 97 | +/** |
|
| 98 | 98 | * Register a package |
| 99 | 99 | * |
| 100 | 100 | * @param string $sClassName The package class |
@@ -103,14 +103,14 @@ discard block |
||
| 103 | 103 | * @return void |
| 104 | 104 | * @throws SetupException |
| 105 | 105 | */ |
| 106 | - public function registerPackage(string $sClassName, array $xPkgOptions = []) |
|
| 107 | - { |
|
| 108 | - // We need the library to have been bootstrapped. |
|
| 109 | - $this->getBootstrap()->onBoot(); |
|
| 110 | - $this->getPackageManager()->registerPackage($sClassName, $xPkgOptions); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 106 | +public function registerPackage(string $sClassName, array $xPkgOptions = []) |
|
| 107 | +{ |
|
| 108 | +// We need the library to have been bootstrapped. |
|
| 109 | +$this->getBootstrap()->onBoot(); |
|
| 110 | +$this->getPackageManager()->registerPackage($sClassName, $xPkgOptions); |
|
| 111 | +} |
|
| 112 | + |
|
| 113 | +/** |
|
| 114 | 114 | * Find a response plugin by name or class name |
| 115 | 115 | * |
| 116 | 116 | * @template R of ResponsePluginInterface |
@@ -118,12 +118,12 @@ discard block |
||
| 118 | 118 | * |
| 119 | 119 | * @return ($sName is class-string ? R : ResponsePluginInterface)|null |
| 120 | 120 | */ |
| 121 | - public function plugin(string $sName): ResponsePluginInterface|null |
|
| 122 | - { |
|
| 123 | - return $this->getPluginManager()->getResponsePlugin($sName); |
|
| 124 | - } |
|
| 121 | +public function plugin(string $sName): ResponsePluginInterface|null |
|
| 122 | +{ |
|
| 123 | +return $this->getPluginManager()->getResponsePlugin($sName); |
|
| 124 | +} |
|
| 125 | 125 | |
| 126 | - /** |
|
| 126 | +/** |
|
| 127 | 127 | * Get a package instance |
| 128 | 128 | * |
| 129 | 129 | * @template P of AbstractPackage |
@@ -131,52 +131,52 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return P|null |
| 133 | 133 | */ |
| 134 | - public function package(string $sClassName): ?AbstractPackage |
|
| 135 | - { |
|
| 136 | - return $this->getPackageManager()->getPackage($sClassName); |
|
| 137 | - } |
|
| 134 | +public function package(string $sClassName): ?AbstractPackage |
|
| 135 | +{ |
|
| 136 | +return $this->getPackageManager()->getPackage($sClassName); |
|
| 137 | +} |
|
| 138 | 138 | |
| 139 | - /** |
|
| 139 | +/** |
|
| 140 | 140 | * Get the HTML tags to include Jaxon javascript files into the page. |
| 141 | 141 | * |
| 142 | 142 | * @return string |
| 143 | 143 | */ |
| 144 | - public function getJs(): string |
|
| 145 | - { |
|
| 146 | - return $this->getCodeGenerator()->getJs(); |
|
| 147 | - } |
|
| 144 | +public function getJs(): string |
|
| 145 | +{ |
|
| 146 | +return $this->getCodeGenerator()->getJs(); |
|
| 147 | +} |
|
| 148 | 148 | |
| 149 | - /** |
|
| 149 | +/** |
|
| 150 | 150 | * Get the HTML tags to include Jaxon javascript files into the page. |
| 151 | 151 | * |
| 152 | 152 | * @return string the javascript code |
| 153 | 153 | */ |
| 154 | - public function js(): string |
|
| 155 | - { |
|
| 156 | - return $this->getCodeGenerator()->getJs(); |
|
| 157 | - } |
|
| 154 | +public function js(): string |
|
| 155 | +{ |
|
| 156 | +return $this->getCodeGenerator()->getJs(); |
|
| 157 | +} |
|
| 158 | 158 | |
| 159 | - /** |
|
| 159 | +/** |
|
| 160 | 160 | * Get the HTML tags to include Jaxon CSS code and files into the page. |
| 161 | 161 | * |
| 162 | 162 | * @return string |
| 163 | 163 | */ |
| 164 | - public function getCss(): string |
|
| 165 | - { |
|
| 166 | - return $this->getCodeGenerator()->getCss(); |
|
| 167 | - } |
|
| 164 | +public function getCss(): string |
|
| 165 | +{ |
|
| 166 | +return $this->getCodeGenerator()->getCss(); |
|
| 167 | +} |
|
| 168 | 168 | |
| 169 | - /** |
|
| 169 | +/** |
|
| 170 | 170 | * Get the HTML tags to include Jaxon CSS code and files into the page. |
| 171 | 171 | * |
| 172 | 172 | * @return string |
| 173 | 173 | */ |
| 174 | - public function css(): string |
|
| 175 | - { |
|
| 176 | - return $this->getCodeGenerator()->getCss(); |
|
| 177 | - } |
|
| 174 | +public function css(): string |
|
| 175 | +{ |
|
| 176 | +return $this->getCodeGenerator()->getCss(); |
|
| 177 | +} |
|
| 178 | 178 | |
| 179 | - /** |
|
| 179 | +/** |
|
| 180 | 180 | * Returns the js header and wrapper code to be printed into the page |
| 181 | 181 | * |
| 182 | 182 | * The javascript code returned by this function depends on the plugins |
@@ -188,12 +188,12 @@ discard block |
||
| 188 | 188 | * @return string |
| 189 | 189 | * @throws UriException |
| 190 | 190 | */ |
| 191 | - public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
| 192 | - { |
|
| 193 | - return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
| 194 | - } |
|
| 191 | +public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
| 192 | +{ |
|
| 193 | +return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
| 194 | +} |
|
| 195 | 195 | |
| 196 | - /** |
|
| 196 | +/** |
|
| 197 | 197 | * Returns the js header and wrapper code to be printed into the page |
| 198 | 198 | * |
| 199 | 199 | * @param bool $bIncludeJs Also get the js code |
@@ -202,8 +202,8 @@ discard block |
||
| 202 | 202 | * @return string the javascript code |
| 203 | 203 | * @throws UriException |
| 204 | 204 | */ |
| 205 | - public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
| 206 | - { |
|
| 207 | - return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
| 208 | - } |
|
| 205 | +public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
| 206 | +{ |
|
| 207 | +return $this->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
| 208 | +} |
|
| 209 | 209 | } |
@@ -9,35 +9,35 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class Store implements JsonSerializable, Stringable |
| 11 | 11 | { |
| 12 | - /** |
|
| 12 | +/** |
|
| 13 | 13 | * The view renderer |
| 14 | 14 | * |
| 15 | 15 | * @var ViewInterface|null |
| 16 | 16 | */ |
| 17 | - protected $xRenderer = null; |
|
| 17 | +protected $xRenderer = null; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * The view namespace |
| 21 | 21 | * |
| 22 | 22 | * @var string |
| 23 | 23 | */ |
| 24 | - protected $sNamespace; |
|
| 24 | +protected $sNamespace; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 26 | +/** |
|
| 27 | 27 | * The view name |
| 28 | 28 | * |
| 29 | 29 | * @var string |
| 30 | 30 | */ |
| 31 | - protected $sViewName; |
|
| 31 | +protected $sViewName; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 33 | +/** |
|
| 34 | 34 | * The view data |
| 35 | 35 | * |
| 36 | 36 | * @var array |
| 37 | 37 | */ |
| 38 | - protected $aViewData = []; |
|
| 38 | +protected $aViewData = []; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * Make a piece of data available for the rendered view |
| 42 | 42 | * |
| 43 | 43 | * @param string $sName The data name |
@@ -45,26 +45,26 @@ discard block |
||
| 45 | 45 | * |
| 46 | 46 | * @return Store |
| 47 | 47 | */ |
| 48 | - public function with(string $sName, $xValue): Store |
|
| 49 | - { |
|
| 50 | - $this->aViewData[$sName] = $xValue; |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 48 | +public function with(string $sName, $xValue): Store |
|
| 49 | +{ |
|
| 50 | +$this->aViewData[$sName] = $xValue; |
|
| 51 | +return $this; |
|
| 52 | +} |
|
| 53 | 53 | |
| 54 | - /** |
|
| 54 | +/** |
|
| 55 | 55 | * Set the data to be rendered |
| 56 | 56 | * |
| 57 | 57 | * @param array $aViewData The view data |
| 58 | 58 | * |
| 59 | 59 | * @return Store |
| 60 | 60 | */ |
| 61 | - public function setData(array $aViewData): Store |
|
| 62 | - { |
|
| 63 | - $this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 61 | +public function setData(array $aViewData): Store |
|
| 62 | +{ |
|
| 63 | +$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 64 | +return $this; |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Set the view to be rendered, with optional data |
| 69 | 69 | * |
| 70 | 70 | * @param ViewInterface $xRenderer The view renderer |
@@ -74,65 +74,65 @@ discard block |
||
| 74 | 74 | * |
| 75 | 75 | * @return Store |
| 76 | 76 | */ |
| 77 | - public function setView(ViewInterface $xRenderer, |
|
| 78 | - string $sNamespace, string $sViewName, array $aViewData = []): Store |
|
| 79 | - { |
|
| 80 | - $this->xRenderer = $xRenderer; |
|
| 81 | - $this->sNamespace = trim($sNamespace); |
|
| 82 | - $this->sViewName = trim($sViewName); |
|
| 83 | - $this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 84 | - return $this; |
|
| 85 | - } |
|
| 77 | +public function setView(ViewInterface $xRenderer, |
|
| 78 | +string $sNamespace, string $sViewName, array $aViewData = []): Store |
|
| 79 | +{ |
|
| 80 | +$this->xRenderer = $xRenderer; |
|
| 81 | +$this->sNamespace = trim($sNamespace); |
|
| 82 | +$this->sViewName = trim($sViewName); |
|
| 83 | +$this->aViewData = array_merge($this->aViewData, $aViewData); |
|
| 84 | +return $this; |
|
| 85 | +} |
|
| 86 | 86 | |
| 87 | - /** |
|
| 87 | +/** |
|
| 88 | 88 | * Get the view namespace |
| 89 | 89 | * |
| 90 | 90 | * @return string The view namespace |
| 91 | 91 | */ |
| 92 | - public function getNamespace(): string |
|
| 93 | - { |
|
| 94 | - return $this->sNamespace; |
|
| 95 | - } |
|
| 92 | +public function getNamespace(): string |
|
| 93 | +{ |
|
| 94 | +return $this->sNamespace; |
|
| 95 | +} |
|
| 96 | 96 | |
| 97 | - /** |
|
| 97 | +/** |
|
| 98 | 98 | * Get the view name |
| 99 | 99 | * |
| 100 | 100 | * @return string The view name |
| 101 | 101 | */ |
| 102 | - public function getViewName(): string |
|
| 103 | - { |
|
| 104 | - return $this->sViewName; |
|
| 105 | - } |
|
| 102 | +public function getViewName(): string |
|
| 103 | +{ |
|
| 104 | +return $this->sViewName; |
|
| 105 | +} |
|
| 106 | 106 | |
| 107 | - /** |
|
| 107 | +/** |
|
| 108 | 108 | * Get the view data |
| 109 | 109 | * |
| 110 | 110 | * @return array The view data |
| 111 | 111 | */ |
| 112 | - public function getViewData(): array |
|
| 113 | - { |
|
| 114 | - return $this->aViewData; |
|
| 115 | - } |
|
| 112 | +public function getViewData(): array |
|
| 113 | +{ |
|
| 114 | +return $this->aViewData; |
|
| 115 | +} |
|
| 116 | 116 | |
| 117 | - /** |
|
| 117 | +/** |
|
| 118 | 118 | * Render a view using third party view system |
| 119 | 119 | * |
| 120 | 120 | * @return string The string representation of the view |
| 121 | 121 | */ |
| 122 | - public function __toString(): string |
|
| 123 | - { |
|
| 124 | - return !$this->xRenderer ? '' : $this->xRenderer->render($this); |
|
| 125 | - } |
|
| 122 | +public function __toString(): string |
|
| 123 | +{ |
|
| 124 | +return !$this->xRenderer ? '' : $this->xRenderer->render($this); |
|
| 125 | +} |
|
| 126 | 126 | |
| 127 | - /** |
|
| 127 | +/** |
|
| 128 | 128 | * Convert this object to string for json. |
| 129 | 129 | * |
| 130 | 130 | * This is a method of the JsonSerializable interface. |
| 131 | 131 | * |
| 132 | 132 | * @return string |
| 133 | 133 | */ |
| 134 | - public function jsonSerialize(): string |
|
| 135 | - { |
|
| 136 | - return $this->__toString(); |
|
| 137 | - } |
|
| 134 | +public function jsonSerialize(): string |
|
| 135 | +{ |
|
| 136 | +return $this->__toString(); |
|
| 137 | +} |
|
| 138 | 138 | } |
@@ -13,58 +13,58 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | class ViewRenderer |
| 15 | 15 | { |
| 16 | - /** |
|
| 16 | +/** |
|
| 17 | 17 | * @var Container |
| 18 | 18 | */ |
| 19 | - protected $di; |
|
| 19 | +protected $di; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 21 | +/** |
|
| 22 | 22 | * The view data store |
| 23 | 23 | * |
| 24 | 24 | * @var Store|null |
| 25 | 25 | */ |
| 26 | - protected $xStore = null; |
|
| 26 | +protected $xStore = null; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * The view data store |
| 30 | 30 | * |
| 31 | 31 | * @var Store |
| 32 | 32 | */ |
| 33 | - protected $xEmptyStore = null; |
|
| 33 | +protected $xEmptyStore = null; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 35 | +/** |
|
| 36 | 36 | * The view namespaces |
| 37 | 37 | * |
| 38 | 38 | * @var array |
| 39 | 39 | */ |
| 40 | - protected $aNamespaces = []; |
|
| 40 | +protected $aNamespaces = []; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 42 | +/** |
|
| 43 | 43 | * The default namespace |
| 44 | 44 | * |
| 45 | 45 | * @var string |
| 46 | 46 | */ |
| 47 | - protected $sDefaultNamespace = 'jaxon'; |
|
| 47 | +protected $sDefaultNamespace = 'jaxon'; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 49 | +/** |
|
| 50 | 50 | * The view global data |
| 51 | 51 | * |
| 52 | 52 | * @var array |
| 53 | 53 | */ |
| 54 | - protected $aViewData = []; |
|
| 54 | +protected $aViewData = []; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 56 | +/** |
|
| 57 | 57 | * The class constructor |
| 58 | 58 | * |
| 59 | 59 | * @param Container $di |
| 60 | 60 | */ |
| 61 | - public function __construct(Container $di) |
|
| 62 | - { |
|
| 63 | - $this->di = $di; |
|
| 64 | - $this->xEmptyStore = new Store(); |
|
| 65 | - } |
|
| 61 | +public function __construct(Container $di) |
|
| 62 | +{ |
|
| 63 | +$this->di = $di; |
|
| 64 | +$this->xEmptyStore = new Store(); |
|
| 65 | +} |
|
| 66 | 66 | |
| 67 | - /** |
|
| 67 | +/** |
|
| 68 | 68 | * Add a view namespace, and set the corresponding renderer. |
| 69 | 69 | * |
| 70 | 70 | * @param string $sNamespace The namespace name |
@@ -74,59 +74,59 @@ discard block |
||
| 74 | 74 | * |
| 75 | 75 | * @return void |
| 76 | 76 | */ |
| 77 | - public function addNamespace(string $sNamespace, string $sDirectory, |
|
| 78 | - string $sExtension, string $sRenderer): void |
|
| 79 | - { |
|
| 80 | - $aNamespace = [ |
|
| 81 | - 'directory' => $sDirectory, |
|
| 82 | - 'extension' => $sExtension, |
|
| 83 | - 'renderer' => $sRenderer, |
|
| 84 | - ]; |
|
| 85 | - $this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 86 | - } |
|
| 77 | +public function addNamespace(string $sNamespace, string $sDirectory, |
|
| 78 | +string $sExtension, string $sRenderer): void |
|
| 79 | +{ |
|
| 80 | +$aNamespace = [ |
|
| 81 | +'directory' => $sDirectory, |
|
| 82 | +'extension' => $sExtension, |
|
| 83 | +'renderer' => $sRenderer, |
|
| 84 | +]; |
|
| 85 | +$this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 86 | +} |
|
| 87 | 87 | |
| 88 | - /** |
|
| 88 | +/** |
|
| 89 | 89 | * Set the view namespaces. |
| 90 | 90 | * |
| 91 | 91 | * @param Config $xAppConfig The config options provided in the library |
| 92 | 92 | * |
| 93 | 93 | * @return void |
| 94 | 94 | */ |
| 95 | - public function addNamespaces(Config $xAppConfig): void |
|
| 96 | - { |
|
| 97 | - if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 98 | - { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 95 | +public function addNamespaces(Config $xAppConfig): void |
|
| 96 | +{ |
|
| 97 | +if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
| 98 | +{ |
|
| 99 | +return; |
|
| 100 | +} |
|
| 101 | 101 | |
| 102 | - $sPackage = $xAppConfig->getOption('package', ''); |
|
| 103 | - foreach($aNamespaces as $sNamespace => $sOption) |
|
| 104 | - { |
|
| 105 | - // Save the namespace |
|
| 106 | - $aNamespace = $xAppConfig->getOption($sOption); |
|
| 107 | - $aNamespace['package'] = $sPackage; |
|
| 108 | - if(!isset($aNamespace['renderer'])) |
|
| 109 | - { |
|
| 110 | - $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
|
| 111 | - } |
|
| 112 | - $this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 113 | - } |
|
| 114 | - } |
|
| 102 | +$sPackage = $xAppConfig->getOption('package', ''); |
|
| 103 | +foreach($aNamespaces as $sNamespace => $sOption) |
|
| 104 | +{ |
|
| 105 | +// Save the namespace |
|
| 106 | +$aNamespace = $xAppConfig->getOption($sOption); |
|
| 107 | +$aNamespace['package'] = $sPackage; |
|
| 108 | +if(!isset($aNamespace['renderer'])) |
|
| 109 | +{ |
|
| 110 | + $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
|
| 111 | +} |
|
| 112 | +$this->aNamespaces[$sNamespace] = $aNamespace; |
|
| 113 | +} |
|
| 114 | +} |
|
| 115 | 115 | |
| 116 | - /** |
|
| 116 | +/** |
|
| 117 | 117 | * Get the view renderer |
| 118 | 118 | * |
| 119 | 119 | * @param string $sId The unique identifier of the view renderer |
| 120 | 120 | * |
| 121 | 121 | * @return ViewInterface |
| 122 | 122 | */ |
| 123 | - public function getRenderer(string $sId): ViewInterface |
|
| 124 | - { |
|
| 125 | - // Return the view renderer with the given id |
|
| 126 | - return $this->di->g("jaxon.app.view.$sId"); |
|
| 127 | - } |
|
| 123 | +public function getRenderer(string $sId): ViewInterface |
|
| 124 | +{ |
|
| 125 | +// Return the view renderer with the given id |
|
| 126 | +return $this->di->g("jaxon.app.view.$sId"); |
|
| 127 | +} |
|
| 128 | 128 | |
| 129 | - /** |
|
| 129 | +/** |
|
| 130 | 130 | * Add a view renderer with an id |
| 131 | 131 | * |
| 132 | 132 | * @param string $sId The unique identifier of the view renderer |
@@ -134,25 +134,25 @@ discard block |
||
| 134 | 134 | * |
| 135 | 135 | * @return void |
| 136 | 136 | */ |
| 137 | - public function addRenderer(string $sId, Closure $xClosure): void |
|
| 138 | - { |
|
| 139 | - // Return the initialized view renderer |
|
| 140 | - $this->di->set("jaxon.app.view.$sId", function($di) use($sId, $xClosure) { |
|
| 141 | - // Get the defined renderer |
|
| 142 | - $xRenderer = $xClosure($di); |
|
| 143 | - // Init the renderer with the template namespaces |
|
| 144 | - $aNamespaces = array_filter($this->aNamespaces, function($aOptions) use($sId) { |
|
| 145 | - return $aOptions['renderer'] === $sId; |
|
| 146 | - }); |
|
| 147 | - foreach($aNamespaces as $sName => $aOptions) |
|
| 148 | - { |
|
| 149 | - $xRenderer->addNamespace($sName, $aOptions['directory'], $aOptions['extension']); |
|
| 150 | - } |
|
| 151 | - return $xRenderer; |
|
| 152 | - }); |
|
| 153 | - } |
|
| 137 | +public function addRenderer(string $sId, Closure $xClosure): void |
|
| 138 | +{ |
|
| 139 | +// Return the initialized view renderer |
|
| 140 | +$this->di->set("jaxon.app.view.$sId", function($di) use($sId, $xClosure) { |
|
| 141 | +// Get the defined renderer |
|
| 142 | +$xRenderer = $xClosure($di); |
|
| 143 | +// Init the renderer with the template namespaces |
|
| 144 | +$aNamespaces = array_filter($this->aNamespaces, function($aOptions) use($sId) { |
|
| 145 | + return $aOptions['renderer'] === $sId; |
|
| 146 | +}); |
|
| 147 | +foreach($aNamespaces as $sName => $aOptions) |
|
| 148 | +{ |
|
| 149 | + $xRenderer->addNamespace($sName, $aOptions['directory'], $aOptions['extension']); |
|
| 150 | +} |
|
| 151 | +return $xRenderer; |
|
| 152 | +}); |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * Add a view renderer with an id |
| 157 | 157 | * |
| 158 | 158 | * @param string $sId The unique identifier of the view renderer |
@@ -161,55 +161,55 @@ discard block |
||
| 161 | 161 | * |
| 162 | 162 | * @return void |
| 163 | 163 | */ |
| 164 | - public function setDefaultRenderer(string $sId, string $sExtension, Closure $xClosure): void |
|
| 165 | - { |
|
| 166 | - $this->setDefaultNamespace($sId); |
|
| 167 | - $this->addNamespace($sId, '', $sExtension, $sId); |
|
| 168 | - $this->addRenderer($sId, $xClosure); |
|
| 169 | - } |
|
| 164 | +public function setDefaultRenderer(string $sId, string $sExtension, Closure $xClosure): void |
|
| 165 | +{ |
|
| 166 | +$this->setDefaultNamespace($sId); |
|
| 167 | +$this->addNamespace($sId, '', $sExtension, $sId); |
|
| 168 | +$this->addRenderer($sId, $xClosure); |
|
| 169 | +} |
|
| 170 | 170 | |
| 171 | - /** |
|
| 171 | +/** |
|
| 172 | 172 | * Get the view renderer for a given namespace |
| 173 | 173 | * |
| 174 | 174 | * @param string $sNamespace The namespace name |
| 175 | 175 | * |
| 176 | 176 | * @return ViewInterface|null |
| 177 | 177 | */ |
| 178 | - public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
|
| 179 | - { |
|
| 180 | - if(!isset($this->aNamespaces[$sNamespace])) |
|
| 181 | - { |
|
| 182 | - return null; |
|
| 183 | - } |
|
| 184 | - // Return the view renderer with the configured id |
|
| 185 | - return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']); |
|
| 186 | - } |
|
| 178 | +public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
|
| 179 | +{ |
|
| 180 | +if(!isset($this->aNamespaces[$sNamespace])) |
|
| 181 | +{ |
|
| 182 | +return null; |
|
| 183 | +} |
|
| 184 | +// Return the view renderer with the configured id |
|
| 185 | +return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']); |
|
| 186 | +} |
|
| 187 | 187 | |
| 188 | - /** |
|
| 188 | +/** |
|
| 189 | 189 | * Set the default namespace |
| 190 | 190 | * |
| 191 | 191 | * @param string $sDefaultNamespace |
| 192 | 192 | */ |
| 193 | - public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 194 | - { |
|
| 195 | - $this->sDefaultNamespace = $sDefaultNamespace; |
|
| 196 | - } |
|
| 193 | +public function setDefaultNamespace(string $sDefaultNamespace): void |
|
| 194 | +{ |
|
| 195 | +$this->sDefaultNamespace = $sDefaultNamespace; |
|
| 196 | +} |
|
| 197 | 197 | |
| 198 | - /** |
|
| 198 | +/** |
|
| 199 | 199 | * Get the current store or create a new store |
| 200 | 200 | * |
| 201 | 201 | * @return Store |
| 202 | 202 | */ |
| 203 | - protected function store(): Store |
|
| 204 | - { |
|
| 205 | - if(!$this->xStore) |
|
| 206 | - { |
|
| 207 | - $this->xStore = new Store(); |
|
| 208 | - } |
|
| 209 | - return $this->xStore; |
|
| 210 | - } |
|
| 203 | +protected function store(): Store |
|
| 204 | +{ |
|
| 205 | +if(!$this->xStore) |
|
| 206 | +{ |
|
| 207 | +$this->xStore = new Store(); |
|
| 208 | +} |
|
| 209 | +return $this->xStore; |
|
| 210 | +} |
|
| 211 | 211 | |
| 212 | - /** |
|
| 212 | +/** |
|
| 213 | 213 | * Make a piece of data available for the rendered view |
| 214 | 214 | * |
| 215 | 215 | * @param string $sName The data name |
@@ -217,13 +217,13 @@ discard block |
||
| 217 | 217 | * |
| 218 | 218 | * @return ViewRenderer |
| 219 | 219 | */ |
| 220 | - public function set(string $sName, $xValue): ViewRenderer |
|
| 221 | - { |
|
| 222 | - $this->store()->with($sName, $xValue); |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 220 | +public function set(string $sName, $xValue): ViewRenderer |
|
| 221 | +{ |
|
| 222 | +$this->store()->with($sName, $xValue); |
|
| 223 | +return $this; |
|
| 224 | +} |
|
| 225 | 225 | |
| 226 | - /** |
|
| 226 | +/** |
|
| 227 | 227 | * Make a piece of data available for all views |
| 228 | 228 | * |
| 229 | 229 | * @param string $sName The data name |
@@ -231,29 +231,29 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @return ViewRenderer |
| 233 | 233 | */ |
| 234 | - public function share(string $sName, $xValue): ViewRenderer |
|
| 235 | - { |
|
| 236 | - $this->aViewData[$sName] = $xValue; |
|
| 237 | - return $this; |
|
| 238 | - } |
|
| 234 | +public function share(string $sName, $xValue): ViewRenderer |
|
| 235 | +{ |
|
| 236 | +$this->aViewData[$sName] = $xValue; |
|
| 237 | +return $this; |
|
| 238 | +} |
|
| 239 | 239 | |
| 240 | - /** |
|
| 240 | +/** |
|
| 241 | 241 | * Make an array of data available for all views |
| 242 | 242 | * |
| 243 | 243 | * @param array $aValues The data values |
| 244 | 244 | * |
| 245 | 245 | * @return ViewRenderer |
| 246 | 246 | */ |
| 247 | - public function shareValues(array $aValues): ViewRenderer |
|
| 248 | - { |
|
| 249 | - foreach($aValues as $sName => $xValue) |
|
| 250 | - { |
|
| 251 | - $this->share($sName, $xValue); |
|
| 252 | - } |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 247 | +public function shareValues(array $aValues): ViewRenderer |
|
| 248 | +{ |
|
| 249 | +foreach($aValues as $sName => $xValue) |
|
| 250 | +{ |
|
| 251 | +$this->share($sName, $xValue); |
|
| 252 | +} |
|
| 253 | +return $this; |
|
| 254 | +} |
|
| 255 | 255 | |
| 256 | - /** |
|
| 256 | +/** |
|
| 257 | 257 | * Render a view using a store |
| 258 | 258 | * |
| 259 | 259 | * The store returned by this function will later be used with the make() method to render the view. |
@@ -263,32 +263,32 @@ discard block |
||
| 263 | 263 | * |
| 264 | 264 | * @return Store A store populated with the view data |
| 265 | 265 | */ |
| 266 | - public function render(string $sViewName, array $aViewData = []): Store |
|
| 267 | - { |
|
| 268 | - $xStore = $this->store(); |
|
| 269 | - // Get the default view namespace |
|
| 270 | - $sNamespace = $this->sDefaultNamespace; |
|
| 271 | - // Get the namespace from the view name |
|
| 272 | - $nSeparatorPosition = strrpos($sViewName, '::'); |
|
| 273 | - if($nSeparatorPosition !== false) |
|
| 274 | - { |
|
| 275 | - $sNamespace = substr($sViewName, 0, $nSeparatorPosition); |
|
| 276 | - $sViewName = substr($sViewName, $nSeparatorPosition + 2); |
|
| 277 | - } |
|
| 266 | +public function render(string $sViewName, array $aViewData = []): Store |
|
| 267 | +{ |
|
| 268 | +$xStore = $this->store(); |
|
| 269 | +// Get the default view namespace |
|
| 270 | +$sNamespace = $this->sDefaultNamespace; |
|
| 271 | +// Get the namespace from the view name |
|
| 272 | +$nSeparatorPosition = strrpos($sViewName, '::'); |
|
| 273 | +if($nSeparatorPosition !== false) |
|
| 274 | +{ |
|
| 275 | +$sNamespace = substr($sViewName, 0, $nSeparatorPosition); |
|
| 276 | +$sViewName = substr($sViewName, $nSeparatorPosition + 2); |
|
| 277 | +} |
|
| 278 | 278 | |
| 279 | - $xRenderer = $this->getNamespaceRenderer($sNamespace); |
|
| 280 | - if(!$xRenderer) |
|
| 281 | - { |
|
| 282 | - // Cannot render a view if there's no renderer corresponding to the namespace. |
|
| 283 | - return $this->xEmptyStore; |
|
| 284 | - } |
|
| 279 | +$xRenderer = $this->getNamespaceRenderer($sNamespace); |
|
| 280 | +if(!$xRenderer) |
|
| 281 | +{ |
|
| 282 | +// Cannot render a view if there's no renderer corresponding to the namespace. |
|
| 283 | +return $this->xEmptyStore; |
|
| 284 | +} |
|
| 285 | 285 | |
| 286 | - $xStore->setData(array_merge($this->aViewData, $aViewData)) |
|
| 287 | - ->setView($xRenderer, $sNamespace, $sViewName); |
|
| 286 | +$xStore->setData(array_merge($this->aViewData, $aViewData)) |
|
| 287 | +->setView($xRenderer, $sNamespace, $sViewName); |
|
| 288 | 288 | |
| 289 | - // Set the store to null so a new store will be created for the next view. |
|
| 290 | - $this->xStore = null; |
|
| 291 | - // Return the store |
|
| 292 | - return $xStore; |
|
| 293 | - } |
|
| 289 | +// Set the store to null so a new store will be created for the next view. |
|
| 290 | +$this->xStore = null; |
|
| 291 | +// Return the store |
|
| 292 | +return $xStore; |
|
| 293 | +} |
|
| 294 | 294 | } |