@@ -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 && $xMetadata !== null) |
|
| 302 | - { |
|
| 303 | - $xMetadataCache->save($xReflectionClass->getName(), $xMetadata); |
|
| 304 | - } |
|
| 300 | +// Try to save the metadata in the cache |
|
| 301 | +if($xMetadataCache !== null && $xMetadata !== 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 | } |
@@ -151,15 +151,15 @@ discard block |
||
| 151 | 151 | $xRegistry->updateHash(false); // Disable hash calculation. |
| 152 | 152 | |
| 153 | 153 | $sComponentId = str_replace('\\', '.', $sClassName); |
| 154 | - if(!isset($this->aComponents[$sComponentId])) |
|
| 154 | + if (!isset($this->aComponents[$sComponentId])) |
|
| 155 | 155 | { |
| 156 | 156 | $aOptions = $xRegistry->getNamespaceComponentOptions($sClassName); |
| 157 | - if($aOptions !== null) |
|
| 157 | + if ($aOptions !== null) |
|
| 158 | 158 | { |
| 159 | 159 | $this->saveComponent($sClassName, $aOptions); |
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | - if(isset($this->aComponents[$sComponentId])) |
|
| 162 | + if (isset($this->aComponents[$sComponentId])) |
|
| 163 | 163 | { |
| 164 | 164 | return; // The component is found. |
| 165 | 165 | } |
@@ -167,11 +167,11 @@ discard block |
||
| 167 | 167 | // The component was not found in a registered namespace. We need to parse all |
| 168 | 168 | // the directories to be able to find a component registered without a namespace. |
| 169 | 169 | $sComponentId = str_replace('\\', '_', $sClassName); |
| 170 | - if(!isset($this->aComponents[$sComponentId])) |
|
| 170 | + if (!isset($this->aComponents[$sComponentId])) |
|
| 171 | 171 | { |
| 172 | 172 | $xRegistry->registerComponentsInDirectories(); |
| 173 | 173 | } |
| 174 | - if(isset($this->aComponents[$sComponentId])) |
|
| 174 | + if (isset($this->aComponents[$sComponentId])) |
|
| 175 | 175 | { |
| 176 | 176 | return; // The component is found. |
| 177 | 177 | } |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | public function getCallableObjects(): array |
| 190 | 190 | { |
| 191 | 191 | $aCallableObjects = []; |
| 192 | - foreach($this->aComponents as $sComponentId => $_) |
|
| 192 | + foreach ($this->aComponents as $sComponentId => $_) |
|
| 193 | 193 | { |
| 194 | 194 | $aCallableObjects[$sComponentId] = $this->makeCallableObject($sComponentId); |
| 195 | 195 | } |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | private function setComponentPublicMethods(string $sKey, string $sClass, |
| 207 | 207 | array $aNeverExported): void |
| 208 | 208 | { |
| 209 | - if(isset($this->aComponentPublicMethods[$sKey])) |
|
| 209 | + if (isset($this->aComponentPublicMethods[$sKey])) |
|
| 210 | 210 | { |
| 211 | 211 | return; |
| 212 | 212 | } |
@@ -259,12 +259,12 @@ discard block |
||
| 259 | 259 | { |
| 260 | 260 | /** @var Config|null */ |
| 261 | 261 | $xPackageConfig = $aOptions['config'] ?? null; |
| 262 | - if($xPackageConfig === null || (bool)($aOptions['excluded'] ?? false)) |
|
| 262 | + if ($xPackageConfig === null || (bool)($aOptions['excluded'] ?? false)) |
|
| 263 | 263 | { |
| 264 | 264 | return null; |
| 265 | 265 | } |
| 266 | 266 | $sMetadataFormat = $xPackageConfig->getOption('metadata.format'); |
| 267 | - if(!in_array($sMetadataFormat, ['attributes', 'annotations'])) |
|
| 267 | + if (!in_array($sMetadataFormat, ['attributes', 'annotations'])) |
|
| 268 | 268 | { |
| 269 | 269 | return null; |
| 270 | 270 | } |
@@ -274,23 +274,23 @@ discard block |
||
| 274 | 274 | $xMetadata = null; |
| 275 | 275 | $xMetadataCache = null; |
| 276 | 276 | $xConfig = $di->config(); |
| 277 | - if($xConfig->getAppOption('metadata.cache.enabled', false)) |
|
| 277 | + if ($xConfig->getAppOption('metadata.cache.enabled', false)) |
|
| 278 | 278 | { |
| 279 | - if(!$di->h('jaxon_metadata_cache_dir')) |
|
| 279 | + if (!$di->h('jaxon_metadata_cache_dir')) |
|
| 280 | 280 | { |
| 281 | 281 | $sCacheDir = $xConfig->getAppOption('metadata.cache.dir'); |
| 282 | 282 | $di->val('jaxon_metadata_cache_dir', $sCacheDir); |
| 283 | 283 | } |
| 284 | 284 | $xMetadataCache = $di->getMetadataCache(); |
| 285 | 285 | $xMetadata = $xMetadataCache->read($xReflectionClass->getName()); |
| 286 | - if($xMetadata !== null) |
|
| 286 | + if ($xMetadata !== null) |
|
| 287 | 287 | { |
| 288 | 288 | return $xMetadata; |
| 289 | 289 | } |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | $aProperties = array_map(fn($xProperty) => $xProperty->getName(), |
| 293 | - $xReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | |
|
| 293 | + $xReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC| |
|
| 294 | 294 | ReflectionProperty::IS_PROTECTED)); |
| 295 | 295 | |
| 296 | 296 | $xMetadataReader = $di->getMetadataReader($sMetadataFormat); |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | $xMetadata = $xMetadataReader->getAttributes($xInput); |
| 299 | 299 | |
| 300 | 300 | // Try to save the metadata in the cache |
| 301 | - if($xMetadataCache !== null && $xMetadata !== null) |
|
| 301 | + if ($xMetadataCache !== null && $xMetadata !== null) |
|
| 302 | 302 | { |
| 303 | 303 | $xMetadataCache->save($xReflectionClass->getName(), $xMetadata); |
| 304 | 304 | } |
@@ -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 |
| 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 | } |
@@ -22,213 +22,213 @@ |
||
| 22 | 22 | |
| 23 | 23 | class Metadata |
| 24 | 24 | { |
| 25 | - /** |
|
| 25 | +/** |
|
| 26 | 26 | * @var array<string, array<string, Data\AbstractData>> |
| 27 | 27 | */ |
| 28 | - private array $aAttributes = [ |
|
| 29 | - 'exclude' => [], |
|
| 30 | - 'export' => [], |
|
| 31 | - 'container' => [], |
|
| 32 | - 'databag' => [], |
|
| 33 | - 'callback' => [], |
|
| 34 | - 'before' => [], |
|
| 35 | - 'after' => [], |
|
| 36 | - 'upload' => [], |
|
| 37 | - ]; |
|
| 38 | - |
|
| 39 | - /** |
|
| 28 | +private array $aAttributes = [ |
|
| 29 | +'exclude' => [], |
|
| 30 | +'export' => [], |
|
| 31 | +'container' => [], |
|
| 32 | +'databag' => [], |
|
| 33 | +'callback' => [], |
|
| 34 | +'before' => [], |
|
| 35 | +'after' => [], |
|
| 36 | +'upload' => [], |
|
| 37 | +]; |
|
| 38 | + |
|
| 39 | +/** |
|
| 40 | 40 | * @return array<string, array<string, Data\AbstractData>> |
| 41 | 41 | */ |
| 42 | - public function getAttributes(): array |
|
| 43 | - { |
|
| 44 | - return $this->aAttributes; |
|
| 45 | - } |
|
| 42 | +public function getAttributes(): array |
|
| 43 | +{ |
|
| 44 | +return $this->aAttributes; |
|
| 45 | +} |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * @param string $sMethod |
| 49 | 49 | * |
| 50 | 50 | * @return Data\ExcludeData |
| 51 | 51 | */ |
| 52 | - public function exclude(string $sMethod = '*'): Data\ExcludeData |
|
| 53 | - { |
|
| 54 | - return $this->aAttributes['exclude'][$sMethod] ?? |
|
| 55 | - $this->aAttributes['exclude'][$sMethod] = new Data\ExcludeData(); |
|
| 56 | - } |
|
| 52 | +public function exclude(string $sMethod = '*'): Data\ExcludeData |
|
| 53 | +{ |
|
| 54 | +return $this->aAttributes['exclude'][$sMethod] ?? |
|
| 55 | +$this->aAttributes['exclude'][$sMethod] = new Data\ExcludeData(); |
|
| 56 | +} |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | +/** |
|
| 59 | 59 | * @param string $sMethod |
| 60 | 60 | * |
| 61 | 61 | * @return Data\ExportData |
| 62 | 62 | */ |
| 63 | - public function export(string $sMethod = '*'): Data\ExportData |
|
| 64 | - { |
|
| 65 | - $sMethod = '*'; // On classes only |
|
| 66 | - return $this->aAttributes['export'][$sMethod] ?? |
|
| 67 | - $this->aAttributes['export'][$sMethod] = new Data\ExportData(); |
|
| 68 | - } |
|
| 63 | +public function export(string $sMethod = '*'): Data\ExportData |
|
| 64 | +{ |
|
| 65 | +$sMethod = '*'; // On classes only |
|
| 66 | +return $this->aAttributes['export'][$sMethod] ?? |
|
| 67 | +$this->aAttributes['export'][$sMethod] = new Data\ExportData(); |
|
| 68 | +} |
|
| 69 | 69 | |
| 70 | - /** |
|
| 70 | +/** |
|
| 71 | 71 | * @param string $sMethod |
| 72 | 72 | * |
| 73 | 73 | * @return Data\ContainerData |
| 74 | 74 | */ |
| 75 | - public function container(string $sMethod = '*'): Data\ContainerData |
|
| 76 | - { |
|
| 77 | - return $this->aAttributes['container'][$sMethod] ?? |
|
| 78 | - $this->aAttributes['container'][$sMethod] = new Data\ContainerData(); |
|
| 79 | - } |
|
| 75 | +public function container(string $sMethod = '*'): Data\ContainerData |
|
| 76 | +{ |
|
| 77 | +return $this->aAttributes['container'][$sMethod] ?? |
|
| 78 | +$this->aAttributes['container'][$sMethod] = new Data\ContainerData(); |
|
| 79 | +} |
|
| 80 | 80 | |
| 81 | - /** |
|
| 81 | +/** |
|
| 82 | 82 | * @param string $sMethod |
| 83 | 83 | * |
| 84 | 84 | * @return Data\DatabagData |
| 85 | 85 | */ |
| 86 | - public function databag(string $sMethod = '*'): Data\DatabagData |
|
| 87 | - { |
|
| 88 | - return $this->aAttributes['databag'][$sMethod] ?? |
|
| 89 | - $this->aAttributes['databag'][$sMethod] = new Data\DatabagData(); |
|
| 90 | - } |
|
| 86 | +public function databag(string $sMethod = '*'): Data\DatabagData |
|
| 87 | +{ |
|
| 88 | +return $this->aAttributes['databag'][$sMethod] ?? |
|
| 89 | +$this->aAttributes['databag'][$sMethod] = new Data\DatabagData(); |
|
| 90 | +} |
|
| 91 | 91 | |
| 92 | - /** |
|
| 92 | +/** |
|
| 93 | 93 | * @param string $sMethod |
| 94 | 94 | * |
| 95 | 95 | * @return Data\CallbackData |
| 96 | 96 | */ |
| 97 | - public function callback(string $sMethod = '*'): Data\CallbackData |
|
| 98 | - { |
|
| 99 | - return $this->aAttributes['callback'][$sMethod] ?? |
|
| 100 | - $this->aAttributes['callback'][$sMethod] = new Data\CallbackData(); |
|
| 101 | - } |
|
| 97 | +public function callback(string $sMethod = '*'): Data\CallbackData |
|
| 98 | +{ |
|
| 99 | +return $this->aAttributes['callback'][$sMethod] ?? |
|
| 100 | +$this->aAttributes['callback'][$sMethod] = new Data\CallbackData(); |
|
| 101 | +} |
|
| 102 | 102 | |
| 103 | - /** |
|
| 103 | +/** |
|
| 104 | 104 | * @param string $sMethod |
| 105 | 105 | * |
| 106 | 106 | * @return Data\BeforeData |
| 107 | 107 | */ |
| 108 | - public function before(string $sMethod = '*'): Data\BeforeData |
|
| 109 | - { |
|
| 110 | - return $this->aAttributes['before'][$sMethod] ?? |
|
| 111 | - $this->aAttributes['before'][$sMethod] = new Data\BeforeData(); |
|
| 112 | - } |
|
| 108 | +public function before(string $sMethod = '*'): Data\BeforeData |
|
| 109 | +{ |
|
| 110 | +return $this->aAttributes['before'][$sMethod] ?? |
|
| 111 | +$this->aAttributes['before'][$sMethod] = new Data\BeforeData(); |
|
| 112 | +} |
|
| 113 | 113 | |
| 114 | - /** |
|
| 114 | +/** |
|
| 115 | 115 | * @param string $sMethod |
| 116 | 116 | * |
| 117 | 117 | * @return Data\AfterData |
| 118 | 118 | */ |
| 119 | - public function after(string $sMethod = '*'): Data\AfterData |
|
| 120 | - { |
|
| 121 | - return $this->aAttributes['after'][$sMethod] ?? |
|
| 122 | - $this->aAttributes['after'][$sMethod] = new Data\AfterData(); |
|
| 123 | - } |
|
| 119 | +public function after(string $sMethod = '*'): Data\AfterData |
|
| 120 | +{ |
|
| 121 | +return $this->aAttributes['after'][$sMethod] ?? |
|
| 122 | +$this->aAttributes['after'][$sMethod] = new Data\AfterData(); |
|
| 123 | +} |
|
| 124 | 124 | |
| 125 | - /** |
|
| 125 | +/** |
|
| 126 | 126 | * @param string $sMethod |
| 127 | 127 | * |
| 128 | 128 | * @return Data\UploadData |
| 129 | 129 | */ |
| 130 | - public function upload(string $sMethod = '*'): Data\UploadData |
|
| 131 | - { |
|
| 132 | - return $this->aAttributes['upload'][$sMethod] ?? |
|
| 133 | - $this->aAttributes['upload'][$sMethod] = new Data\UploadData(); |
|
| 134 | - } |
|
| 130 | +public function upload(string $sMethod = '*'): Data\UploadData |
|
| 131 | +{ |
|
| 132 | +return $this->aAttributes['upload'][$sMethod] ?? |
|
| 133 | +$this->aAttributes['upload'][$sMethod] = new Data\UploadData(); |
|
| 134 | +} |
|
| 135 | 135 | |
| 136 | - /** |
|
| 136 | +/** |
|
| 137 | 137 | * True if the class is excluded |
| 138 | 138 | * |
| 139 | 139 | * @return bool |
| 140 | 140 | */ |
| 141 | - public function isExcluded(): bool |
|
| 142 | - { |
|
| 143 | - $xData = $this->aAttributes['exclude']['*'] ?? null; |
|
| 144 | - return $xData !== null && $xData->getValue() === true; |
|
| 145 | - } |
|
| 141 | +public function isExcluded(): bool |
|
| 142 | +{ |
|
| 143 | +$xData = $this->aAttributes['exclude']['*'] ?? null; |
|
| 144 | +return $xData !== null && $xData->getValue() === true; |
|
| 145 | +} |
|
| 146 | 146 | |
| 147 | - /** |
|
| 147 | +/** |
|
| 148 | 148 | * Get the properties of the class methods |
| 149 | 149 | * |
| 150 | 150 | * @return array |
| 151 | 151 | */ |
| 152 | - public function getProperties(): array |
|
| 152 | +public function getProperties(): array |
|
| 153 | +{ |
|
| 154 | +$aProperties = []; |
|
| 155 | +$aClassProperties = []; |
|
| 156 | +foreach($this->aAttributes as $sType => $aValues) |
|
| 157 | +{ |
|
| 158 | +if($sType === 'exclude') |
|
| 159 | +{ |
|
| 160 | + continue; |
|
| 161 | +} |
|
| 162 | + |
|
| 163 | +foreach($aValues as $sMethod => $xData) |
|
| 164 | +{ |
|
| 165 | + if($sMethod === '*') |
|
| 153 | 166 | { |
| 154 | - $aProperties = []; |
|
| 155 | - $aClassProperties = []; |
|
| 156 | - foreach($this->aAttributes as $sType => $aValues) |
|
| 157 | - { |
|
| 158 | - if($sType === 'exclude') |
|
| 159 | - { |
|
| 160 | - continue; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - foreach($aValues as $sMethod => $xData) |
|
| 164 | - { |
|
| 165 | - if($sMethod === '*') |
|
| 166 | - { |
|
| 167 | - $aClassProperties[$xData->getName()] = $xData->getValue(); |
|
| 168 | - continue; |
|
| 169 | - } |
|
| 170 | - $aProperties[$sMethod][$xData->getName()] = $xData->getValue(); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if(count($aClassProperties) > 0) |
|
| 175 | - { |
|
| 176 | - $aProperties['*'] = $aClassProperties; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $aProperties; |
|
| 167 | + $aClassProperties[$xData->getName()] = $xData->getValue(); |
|
| 168 | + continue; |
|
| 180 | 169 | } |
| 170 | + $aProperties[$sMethod][$xData->getName()] = $xData->getValue(); |
|
| 171 | +} |
|
| 172 | +} |
|
| 181 | 173 | |
| 182 | - /** |
|
| 174 | +if(count($aClassProperties) > 0) |
|
| 175 | +{ |
|
| 176 | +$aProperties['*'] = $aClassProperties; |
|
| 177 | +} |
|
| 178 | + |
|
| 179 | +return $aProperties; |
|
| 180 | +} |
|
| 181 | + |
|
| 182 | +/** |
|
| 183 | 183 | * Get the methods in the export attributes |
| 184 | 184 | * |
| 185 | 185 | * @return array |
| 186 | 186 | */ |
| 187 | - public function getExportMethods(): array |
|
| 188 | - { |
|
| 189 | - /** @var array<Data\ExcludeData> */ |
|
| 190 | - $aAttributes = $this->aAttributes['exclude']; |
|
| 191 | - $aExcludeMethods = array_keys($aAttributes); |
|
| 192 | - $aExcludeMethods = array_values(array_filter($aExcludeMethods, |
|
| 193 | - fn(string $sName) => $sName !== '*' && |
|
| 194 | - $aAttributes[$sName]->getValue() === true)); |
|
| 195 | - |
|
| 196 | - /** @var Data\ExportData */ |
|
| 197 | - $xExportData = $this->aAttributes['export']['*'] ?? null; |
|
| 198 | - $aExportMethods = $xExportData?->getValue() ?? []; |
|
| 199 | - |
|
| 200 | - $aExceptMethods = $aExportMethods['except'] ?? []; |
|
| 201 | - $aExportMethods['except'] = array_merge($aExcludeMethods, $aExceptMethods); |
|
| 202 | - return $aExportMethods; |
|
| 203 | - } |
|
| 187 | +public function getExportMethods(): array |
|
| 188 | +{ |
|
| 189 | +/** @var array<Data\ExcludeData> */ |
|
| 190 | +$aAttributes = $this->aAttributes['exclude']; |
|
| 191 | +$aExcludeMethods = array_keys($aAttributes); |
|
| 192 | +$aExcludeMethods = array_values(array_filter($aExcludeMethods, |
|
| 193 | +fn(string $sName) => $sName !== '*' && |
|
| 194 | + $aAttributes[$sName]->getValue() === true)); |
|
| 195 | + |
|
| 196 | +/** @var Data\ExportData */ |
|
| 197 | +$xExportData = $this->aAttributes['export']['*'] ?? null; |
|
| 198 | +$aExportMethods = $xExportData?->getValue() ?? []; |
|
| 199 | + |
|
| 200 | +$aExceptMethods = $aExportMethods['except'] ?? []; |
|
| 201 | +$aExportMethods['except'] = array_merge($aExcludeMethods, $aExceptMethods); |
|
| 202 | +return $aExportMethods; |
|
| 203 | +} |
|
| 204 | 204 | |
| 205 | - /** |
|
| 205 | +/** |
|
| 206 | 206 | * Get the exluded methods |
| 207 | 207 | * |
| 208 | 208 | * @return array |
| 209 | 209 | */ |
| 210 | - public function getExceptMethods(): array |
|
| 211 | - { |
|
| 212 | - return $this->getExportMethods()['except']; |
|
| 213 | - } |
|
| 210 | +public function getExceptMethods(): array |
|
| 211 | +{ |
|
| 212 | +return $this->getExportMethods()['except']; |
|
| 213 | +} |
|
| 214 | 214 | |
| 215 | - /** |
|
| 215 | +/** |
|
| 216 | 216 | * Get the export base methods |
| 217 | 217 | * |
| 218 | 218 | * @return array |
| 219 | 219 | */ |
| 220 | - public function getExportBaseMethods(): array |
|
| 221 | - { |
|
| 222 | - return $this->getExportMethods()['base'] ?? []; |
|
| 223 | - } |
|
| 220 | +public function getExportBaseMethods(): array |
|
| 221 | +{ |
|
| 222 | +return $this->getExportMethods()['base'] ?? []; |
|
| 223 | +} |
|
| 224 | 224 | |
| 225 | - /** |
|
| 225 | +/** |
|
| 226 | 226 | * Get the export only methods |
| 227 | 227 | * |
| 228 | 228 | * @return array |
| 229 | 229 | */ |
| 230 | - public function getExportOnlyMethods(): array |
|
| 231 | - { |
|
| 232 | - return $this->getExportMethods()['only'] ?? []; |
|
| 233 | - } |
|
| 230 | +public function getExportOnlyMethods(): array |
|
| 231 | +{ |
|
| 232 | +return $this->getExportMethods()['only'] ?? []; |
|
| 233 | +} |
|
| 234 | 234 | } |
@@ -22,53 +22,53 @@ |
||
| 22 | 22 | |
| 23 | 23 | class ExportData extends AbstractData |
| 24 | 24 | { |
| 25 | - /** |
|
| 25 | +/** |
|
| 26 | 26 | * @var array<string, array<string>> |
| 27 | 27 | */ |
| 28 | - private array $aMethods = []; |
|
| 28 | +private array $aMethods = []; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 30 | +/** |
|
| 31 | 31 | * @return string |
| 32 | 32 | */ |
| 33 | - public function getName(): string |
|
| 34 | - { |
|
| 35 | - return 'export'; |
|
| 36 | - } |
|
| 33 | +public function getName(): string |
|
| 34 | +{ |
|
| 35 | +return 'export'; |
|
| 36 | +} |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @return mixed |
| 40 | 40 | */ |
| 41 | - public function getValue(): mixed |
|
| 42 | - { |
|
| 43 | - return $this->aMethods; |
|
| 44 | - } |
|
| 41 | +public function getValue(): mixed |
|
| 42 | +{ |
|
| 43 | +return $this->aMethods; |
|
| 44 | +} |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * @param array $aMethods |
| 48 | 48 | * |
| 49 | 49 | * @return void |
| 50 | 50 | */ |
| 51 | - public function setMethods(array $aMethods): void |
|
| 51 | +public function setMethods(array $aMethods): void |
|
| 52 | +{ |
|
| 53 | +foreach(['base', 'only', 'except'] as $sKey) |
|
| 54 | +{ |
|
| 55 | +foreach($aMethods[$sKey] ?? [] as $sMethod) |
|
| 56 | +{ |
|
| 57 | + if(!is_string($sMethod) || !$this->validateMethod($sMethod)) |
|
| 52 | 58 | { |
| 53 | - foreach(['base', 'only', 'except'] as $sKey) |
|
| 54 | - { |
|
| 55 | - foreach($aMethods[$sKey] ?? [] as $sMethod) |
|
| 56 | - { |
|
| 57 | - if(!is_string($sMethod) || !$this->validateMethod($sMethod)) |
|
| 58 | - { |
|
| 59 | - throw new SetupException("'$sMethod' is not a valid method name."); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - $this->aMethods = $aMethods; |
|
| 59 | + throw new SetupException("'$sMethod' is not a valid method name."); |
|
| 64 | 60 | } |
| 61 | +} |
|
| 62 | +} |
|
| 63 | +$this->aMethods = $aMethods; |
|
| 64 | +} |
|
| 65 | 65 | |
| 66 | - /** |
|
| 66 | +/** |
|
| 67 | 67 | * @inheritDoc |
| 68 | 68 | */ |
| 69 | - public function encode(string $sVarName): array |
|
| 70 | - { |
|
| 71 | - $sMethods = addslashes(json_encode($this->aMethods)); |
|
| 72 | - return ["{$sVarName}->setMethods(json_decode(\"$sMethods\", true));"]; |
|
| 73 | - } |
|
| 69 | +public function encode(string $sVarName): array |
|
| 70 | +{ |
|
| 71 | +$sMethods = addslashes(json_encode($this->aMethods)); |
|
| 72 | +return ["{$sVarName}->setMethods(json_decode(\"$sMethods\", true));"]; |
|
| 73 | +} |
|
| 74 | 74 | } |
@@ -50,11 +50,11 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function setMethods(array $aMethods): void |
| 52 | 52 | { |
| 53 | - foreach(['base', 'only', 'except'] as $sKey) |
|
| 53 | + foreach (['base', 'only', 'except'] as $sKey) |
|
| 54 | 54 | { |
| 55 | - foreach($aMethods[$sKey] ?? [] as $sMethod) |
|
| 55 | + foreach ($aMethods[$sKey] ?? [] as $sMethod) |
|
| 56 | 56 | { |
| 57 | - if(!is_string($sMethod) || !$this->validateMethod($sMethod)) |
|
| 57 | + if (!is_string($sMethod) || !$this->validateMethod($sMethod)) |
|
| 58 | 58 | { |
| 59 | 59 | throw new SetupException("'$sMethod' is not a valid method name."); |
| 60 | 60 | } |
@@ -34,221 +34,221 @@ discard block |
||
| 34 | 34 | |
| 35 | 35 | class ComponentOptions |
| 36 | 36 | { |
| 37 | - /** |
|
| 37 | +/** |
|
| 38 | 38 | * Check if the js code for this object must be generated |
| 39 | 39 | * |
| 40 | 40 | * @var bool |
| 41 | 41 | */ |
| 42 | - private $bExcluded = false; |
|
| 42 | +private $bExcluded = false; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 44 | +/** |
|
| 45 | 45 | * The character to use as separator in javascript class names |
| 46 | 46 | * |
| 47 | 47 | * @var string |
| 48 | 48 | */ |
| 49 | - private $sSeparator = '.'; |
|
| 49 | +private $sSeparator = '.'; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * A list of methods of the user registered callable object the library can export to javascript |
| 53 | 53 | * |
| 54 | 54 | * @var array |
| 55 | 55 | */ |
| 56 | - private $aPublicMethods = []; |
|
| 56 | +private $aPublicMethods = []; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 58 | +/** |
|
| 59 | 59 | * The methods in the export attributes |
| 60 | 60 | * |
| 61 | 61 | * @var array |
| 62 | 62 | */ |
| 63 | - private $aExportMethods = ['except' => []]; |
|
| 63 | +private $aExportMethods = ['except' => []]; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 65 | +/** |
|
| 66 | 66 | * A list of methods to call before processing the request |
| 67 | 67 | * |
| 68 | 68 | * @var array |
| 69 | 69 | */ |
| 70 | - private $aBeforeMethods = []; |
|
| 70 | +private $aBeforeMethods = []; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 72 | +/** |
|
| 73 | 73 | * A list of methods to call after processing the request |
| 74 | 74 | * |
| 75 | 75 | * @var array |
| 76 | 76 | */ |
| 77 | - private $aAfterMethods = []; |
|
| 77 | +private $aAfterMethods = []; |
|
| 78 | 78 | |
| 79 | - /** |
|
| 79 | +/** |
|
| 80 | 80 | * The javascript class options |
| 81 | 81 | * |
| 82 | 82 | * @var array |
| 83 | 83 | */ |
| 84 | - private $aJsOptions = []; |
|
| 84 | +private $aJsOptions = []; |
|
| 85 | 85 | |
| 86 | - /** |
|
| 86 | +/** |
|
| 87 | 87 | * The DI options |
| 88 | 88 | * |
| 89 | 89 | * @var array |
| 90 | 90 | */ |
| 91 | - private $aDiOptions = []; |
|
| 91 | +private $aDiOptions = []; |
|
| 92 | 92 | |
| 93 | - /** |
|
| 93 | +/** |
|
| 94 | 94 | * The constructor |
| 95 | 95 | * |
| 96 | 96 | * @param array $aMethods |
| 97 | 97 | * @param array $aOptions |
| 98 | 98 | * @param Metadata|null $xMetadata |
| 99 | 99 | */ |
| 100 | - public function __construct(array $aMethods, array $aOptions, Metadata|null $xMetadata) |
|
| 101 | - { |
|
| 102 | - $this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
|
| 103 | - (bool)($aOptions['excluded'] ?? false); |
|
| 104 | - if($this->bExcluded) |
|
| 105 | - { |
|
| 106 | - return; |
|
| 107 | - } |
|
| 100 | +public function __construct(array $aMethods, array $aOptions, Metadata|null $xMetadata) |
|
| 101 | +{ |
|
| 102 | +$this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
|
| 103 | +(bool)($aOptions['excluded'] ?? false); |
|
| 104 | +if($this->bExcluded) |
|
| 105 | +{ |
|
| 106 | +return; |
|
| 107 | +} |
|
| 108 | 108 | |
| 109 | - // Options from the config. |
|
| 110 | - $sSeparator = $aOptions['separator']; |
|
| 111 | - $this->sSeparator = $sSeparator === '_' ? $sSeparator : '.'; |
|
| 109 | +// Options from the config. |
|
| 110 | +$sSeparator = $aOptions['separator']; |
|
| 111 | +$this->sSeparator = $sSeparator === '_' ? $sSeparator : '.'; |
|
| 112 | 112 | |
| 113 | - $this->addProtectedMethods($aOptions['protected']); |
|
| 113 | +$this->addProtectedMethods($aOptions['protected']); |
|
| 114 | 114 | |
| 115 | - foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 116 | - { |
|
| 117 | - // Names are in a comma-separated list. |
|
| 118 | - $aFunctionNames = explode(',', $sNames); |
|
| 119 | - foreach($aFunctionNames as $sFunctionName) |
|
| 120 | - { |
|
| 121 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 122 | - } |
|
| 123 | - } |
|
| 115 | +foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 116 | +{ |
|
| 117 | +// Names are in a comma-separated list. |
|
| 118 | +$aFunctionNames = explode(',', $sNames); |
|
| 119 | +foreach($aFunctionNames as $sFunctionName) |
|
| 120 | +{ |
|
| 121 | + $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 122 | +} |
|
| 123 | +} |
|
| 124 | 124 | |
| 125 | - // Options from the attributes or annotations. |
|
| 126 | - if($xMetadata !== null) |
|
| 127 | - { |
|
| 128 | - // Excluded methods must be merged with the existing ones. |
|
| 129 | - $aExportMethods = $xMetadata->getExportMethods(); |
|
| 130 | - $aExportMethods['except'] = array_unique(array_merge( |
|
| 131 | - $aExportMethods['except'] ?? [], $this->aExportMethods['except'])); |
|
| 132 | - $this->aExportMethods = $aExportMethods; |
|
| 125 | +// Options from the attributes or annotations. |
|
| 126 | +if($xMetadata !== null) |
|
| 127 | +{ |
|
| 128 | +// Excluded methods must be merged with the existing ones. |
|
| 129 | +$aExportMethods = $xMetadata->getExportMethods(); |
|
| 130 | +$aExportMethods['except'] = array_unique(array_merge( |
|
| 131 | + $aExportMethods['except'] ?? [], $this->aExportMethods['except'])); |
|
| 132 | +$this->aExportMethods = $aExportMethods; |
|
| 133 | 133 | |
| 134 | - foreach($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 135 | - { |
|
| 136 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 137 | - } |
|
| 138 | - } |
|
| 134 | +foreach($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 135 | +{ |
|
| 136 | + $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 137 | +} |
|
| 138 | +} |
|
| 139 | 139 | |
| 140 | - $this->aPublicMethods = $this->filterPublicMethods($aMethods); |
|
| 141 | - } |
|
| 140 | +$this->aPublicMethods = $this->filterPublicMethods($aMethods); |
|
| 141 | +} |
|
| 142 | 142 | |
| 143 | - /** |
|
| 143 | +/** |
|
| 144 | 144 | * @param array|string $xMethods |
| 145 | 145 | * |
| 146 | 146 | * @return void |
| 147 | 147 | */ |
| 148 | - private function addProtectedMethods(array|string $xMethods): void |
|
| 149 | - { |
|
| 150 | - $this->aExportMethods['except'] = array_merge($this->aExportMethods['except'], |
|
| 151 | - !is_array($xMethods) ? [trim((string)$xMethods)] : |
|
| 152 | - array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 153 | - } |
|
| 148 | +private function addProtectedMethods(array|string $xMethods): void |
|
| 149 | +{ |
|
| 150 | +$this->aExportMethods['except'] = array_merge($this->aExportMethods['except'], |
|
| 151 | +!is_array($xMethods) ? [trim((string)$xMethods)] : |
|
| 152 | +array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 153 | +} |
|
| 154 | 154 | |
| 155 | - /** |
|
| 155 | +/** |
|
| 156 | 156 | * @param array $aMethods |
| 157 | 157 | * |
| 158 | 158 | * @return array |
| 159 | 159 | */ |
| 160 | - private function filterPublicMethods(array $aMethods): array |
|
| 161 | - { |
|
| 162 | - if($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 163 | - { |
|
| 164 | - return []; |
|
| 165 | - } |
|
| 160 | +private function filterPublicMethods(array $aMethods): array |
|
| 161 | +{ |
|
| 162 | +if($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 163 | +{ |
|
| 164 | +return []; |
|
| 165 | +} |
|
| 166 | 166 | |
| 167 | - $aBaseMethods = $aMethods[1]; |
|
| 168 | - $aNoMethods = $aMethods[2]; |
|
| 169 | - $aMethods = $aMethods[0]; |
|
| 170 | - if(isset($this->aExportMethods['only'])) |
|
| 171 | - { |
|
| 172 | - $aMethods = array_intersect($aMethods, $this->aExportMethods['only']); |
|
| 173 | - } |
|
| 174 | - $aMethods = array_diff($aMethods, $this->aExportMethods['except']); |
|
| 175 | - if(count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 176 | - { |
|
| 177 | - $aBaseMethods = array_diff($aBaseMethods, $this->aExportMethods['base']); |
|
| 178 | - } |
|
| 167 | +$aBaseMethods = $aMethods[1]; |
|
| 168 | +$aNoMethods = $aMethods[2]; |
|
| 169 | +$aMethods = $aMethods[0]; |
|
| 170 | +if(isset($this->aExportMethods['only'])) |
|
| 171 | +{ |
|
| 172 | +$aMethods = array_intersect($aMethods, $this->aExportMethods['only']); |
|
| 173 | +} |
|
| 174 | +$aMethods = array_diff($aMethods, $this->aExportMethods['except']); |
|
| 175 | +if(count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 176 | +{ |
|
| 177 | +$aBaseMethods = array_diff($aBaseMethods, $this->aExportMethods['base']); |
|
| 178 | +} |
|
| 179 | 179 | |
| 180 | - return array_values(array_diff($aMethods, $aBaseMethods, $aNoMethods)); |
|
| 181 | - } |
|
| 180 | +return array_values(array_diff($aMethods, $aBaseMethods, $aNoMethods)); |
|
| 181 | +} |
|
| 182 | 182 | |
| 183 | - /** |
|
| 183 | +/** |
|
| 184 | 184 | * @return array |
| 185 | 185 | */ |
| 186 | - public function getPublicMethods(): array |
|
| 187 | - { |
|
| 188 | - return $this->aPublicMethods; |
|
| 189 | - } |
|
| 186 | +public function getPublicMethods(): array |
|
| 187 | +{ |
|
| 188 | +return $this->aPublicMethods; |
|
| 189 | +} |
|
| 190 | 190 | |
| 191 | - /** |
|
| 191 | +/** |
|
| 192 | 192 | * @param string $sMethodName |
| 193 | 193 | * |
| 194 | 194 | * @return bool |
| 195 | 195 | */ |
| 196 | - public function isPublicMethod(string $sMethodName): bool |
|
| 197 | - { |
|
| 198 | - return in_array($sMethodName, $this->aPublicMethods); |
|
| 199 | - } |
|
| 196 | +public function isPublicMethod(string $sMethodName): bool |
|
| 197 | +{ |
|
| 198 | +return in_array($sMethodName, $this->aPublicMethods); |
|
| 199 | +} |
|
| 200 | 200 | |
| 201 | - /** |
|
| 201 | +/** |
|
| 202 | 202 | * Check if the js code for this object must be generated |
| 203 | 203 | * |
| 204 | 204 | * @return bool |
| 205 | 205 | */ |
| 206 | - public function excluded(): bool |
|
| 207 | - { |
|
| 208 | - return $this->bExcluded; |
|
| 209 | - } |
|
| 206 | +public function excluded(): bool |
|
| 207 | +{ |
|
| 208 | +return $this->bExcluded; |
|
| 209 | +} |
|
| 210 | 210 | |
| 211 | - /** |
|
| 211 | +/** |
|
| 212 | 212 | * @return string |
| 213 | 213 | */ |
| 214 | - public function separator(): string |
|
| 215 | - { |
|
| 216 | - return $this->sSeparator; |
|
| 217 | - } |
|
| 214 | +public function separator(): string |
|
| 215 | +{ |
|
| 216 | +return $this->sSeparator; |
|
| 217 | +} |
|
| 218 | 218 | |
| 219 | - /** |
|
| 219 | +/** |
|
| 220 | 220 | * @return array |
| 221 | 221 | */ |
| 222 | - public function beforeMethods(): array |
|
| 223 | - { |
|
| 224 | - return $this->aBeforeMethods; |
|
| 225 | - } |
|
| 222 | +public function beforeMethods(): array |
|
| 223 | +{ |
|
| 224 | +return $this->aBeforeMethods; |
|
| 225 | +} |
|
| 226 | 226 | |
| 227 | - /** |
|
| 227 | +/** |
|
| 228 | 228 | * @return array |
| 229 | 229 | */ |
| 230 | - public function afterMethods(): array |
|
| 231 | - { |
|
| 232 | - return $this->aAfterMethods; |
|
| 233 | - } |
|
| 230 | +public function afterMethods(): array |
|
| 231 | +{ |
|
| 232 | +return $this->aAfterMethods; |
|
| 233 | +} |
|
| 234 | 234 | |
| 235 | - /** |
|
| 235 | +/** |
|
| 236 | 236 | * @return array |
| 237 | 237 | */ |
| 238 | - public function diOptions(): array |
|
| 239 | - { |
|
| 240 | - return $this->aDiOptions; |
|
| 241 | - } |
|
| 238 | +public function diOptions(): array |
|
| 239 | +{ |
|
| 240 | +return $this->aDiOptions; |
|
| 241 | +} |
|
| 242 | 242 | |
| 243 | - /** |
|
| 243 | +/** |
|
| 244 | 244 | * @return array |
| 245 | 245 | */ |
| 246 | - public function jsOptions(): array |
|
| 247 | - { |
|
| 248 | - return $this->aJsOptions; |
|
| 249 | - } |
|
| 246 | +public function jsOptions(): array |
|
| 247 | +{ |
|
| 248 | +return $this->aJsOptions; |
|
| 249 | +} |
|
| 250 | 250 | |
| 251 | - /** |
|
| 251 | +/** |
|
| 252 | 252 | * Set hook methods |
| 253 | 253 | * |
| 254 | 254 | * @param array $aHookMethods The array of hook methods |
@@ -256,34 +256,34 @@ discard block |
||
| 256 | 256 | * |
| 257 | 257 | * @return void |
| 258 | 258 | */ |
| 259 | - private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 260 | - { |
|
| 261 | - foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 262 | - { |
|
| 263 | - if(!isset($aHookMethods[$sCalledMethod])) |
|
| 264 | - { |
|
| 265 | - $aHookMethods[$sCalledMethod] = []; |
|
| 266 | - } |
|
| 267 | - if(is_array($xMethodToCall)) |
|
| 268 | - { |
|
| 269 | - $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 270 | - } |
|
| 271 | - elseif(is_string($xMethodToCall)) |
|
| 272 | - { |
|
| 273 | - $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 259 | +private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 260 | +{ |
|
| 261 | +foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 262 | +{ |
|
| 263 | +if(!isset($aHookMethods[$sCalledMethod])) |
|
| 264 | +{ |
|
| 265 | + $aHookMethods[$sCalledMethod] = []; |
|
| 266 | +} |
|
| 267 | +if(is_array($xMethodToCall)) |
|
| 268 | +{ |
|
| 269 | + $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 270 | +} |
|
| 271 | +elseif(is_string($xMethodToCall)) |
|
| 272 | +{ |
|
| 273 | + $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 274 | +} |
|
| 275 | +} |
|
| 276 | +} |
|
| 277 | 277 | |
| 278 | - /** |
|
| 278 | +/** |
|
| 279 | 279 | * @param array $aDiOptions |
| 280 | 280 | */ |
| 281 | - private function addDiOption(array $aDiOptions): void |
|
| 282 | - { |
|
| 283 | - $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 284 | - } |
|
| 281 | +private function addDiOption(array $aDiOptions): void |
|
| 282 | +{ |
|
| 283 | +$this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 284 | +} |
|
| 285 | 285 | |
| 286 | - /** |
|
| 286 | +/** |
|
| 287 | 287 | * Set configuration options / call options for each method |
| 288 | 288 | * |
| 289 | 289 | * @param string $sName The name of the configuration option |
@@ -291,25 +291,25 @@ discard block |
||
| 291 | 291 | * |
| 292 | 292 | * @return void |
| 293 | 293 | */ |
| 294 | - private function addOption(string $sName, $xValue): void |
|
| 295 | - { |
|
| 296 | - switch($sName) |
|
| 297 | - { |
|
| 298 | - // Set the methods to call before processing the request |
|
| 299 | - case '__before': |
|
| 300 | - $this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 301 | - break; |
|
| 302 | - // Set the methods to call after processing the request |
|
| 303 | - case '__after': |
|
| 304 | - $this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 305 | - break; |
|
| 306 | - // Set the attributes to inject in the callable object |
|
| 307 | - case '__di': |
|
| 308 | - $this->addDiOption($xValue); |
|
| 309 | - break; |
|
| 310 | - default: |
|
| 311 | - break; |
|
| 312 | - } |
|
| 294 | +private function addOption(string $sName, $xValue): void |
|
| 295 | +{ |
|
| 296 | +switch($sName) |
|
| 297 | +{ |
|
| 298 | +// Set the methods to call before processing the request |
|
| 299 | +case '__before': |
|
| 300 | +$this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 301 | +break; |
|
| 302 | +// Set the methods to call after processing the request |
|
| 303 | +case '__after': |
|
| 304 | +$this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 305 | +break; |
|
| 306 | +// Set the attributes to inject in the callable object |
|
| 307 | +case '__di': |
|
| 308 | +$this->addDiOption($xValue); |
|
| 309 | +break; |
|
| 310 | +default: |
|
| 311 | +break; |
|
| 312 | +} |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | /** |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | { |
| 102 | 102 | $this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
| 103 | 103 | (bool)($aOptions['excluded'] ?? false); |
| 104 | - if($this->bExcluded) |
|
| 104 | + if ($this->bExcluded) |
|
| 105 | 105 | { |
| 106 | 106 | return; |
| 107 | 107 | } |
@@ -112,18 +112,18 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | $this->addProtectedMethods($aOptions['protected']); |
| 114 | 114 | |
| 115 | - foreach($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 115 | + foreach ($aOptions['functions'] as $sNames => $aFunctionOptions) |
|
| 116 | 116 | { |
| 117 | 117 | // Names are in a comma-separated list. |
| 118 | 118 | $aFunctionNames = explode(',', $sNames); |
| 119 | - foreach($aFunctionNames as $sFunctionName) |
|
| 119 | + foreach ($aFunctionNames as $sFunctionName) |
|
| 120 | 120 | { |
| 121 | 121 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Options from the attributes or annotations. |
| 126 | - if($xMetadata !== null) |
|
| 126 | + if ($xMetadata !== null) |
|
| 127 | 127 | { |
| 128 | 128 | // Excluded methods must be merged with the existing ones. |
| 129 | 129 | $aExportMethods = $xMetadata->getExportMethods(); |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | $aExportMethods['except'] ?? [], $this->aExportMethods['except'])); |
| 132 | 132 | $this->aExportMethods = $aExportMethods; |
| 133 | 133 | |
| 134 | - foreach($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 134 | + foreach ($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 135 | 135 | { |
| 136 | 136 | $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
| 137 | 137 | } |
@@ -148,8 +148,7 @@ discard block |
||
| 148 | 148 | private function addProtectedMethods(array|string $xMethods): void |
| 149 | 149 | { |
| 150 | 150 | $this->aExportMethods['except'] = array_merge($this->aExportMethods['except'], |
| 151 | - !is_array($xMethods) ? [trim((string)$xMethods)] : |
|
| 152 | - array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 151 | + !is_array($xMethods) ? [trim((string)$xMethods)] : array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 153 | 152 | } |
| 154 | 153 | |
| 155 | 154 | /** |
@@ -159,7 +158,7 @@ discard block |
||
| 159 | 158 | */ |
| 160 | 159 | private function filterPublicMethods(array $aMethods): array |
| 161 | 160 | { |
| 162 | - if($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 161 | + if ($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 163 | 162 | { |
| 164 | 163 | return []; |
| 165 | 164 | } |
@@ -167,12 +166,12 @@ discard block |
||
| 167 | 166 | $aBaseMethods = $aMethods[1]; |
| 168 | 167 | $aNoMethods = $aMethods[2]; |
| 169 | 168 | $aMethods = $aMethods[0]; |
| 170 | - if(isset($this->aExportMethods['only'])) |
|
| 169 | + if (isset($this->aExportMethods['only'])) |
|
| 171 | 170 | { |
| 172 | 171 | $aMethods = array_intersect($aMethods, $this->aExportMethods['only']); |
| 173 | 172 | } |
| 174 | 173 | $aMethods = array_diff($aMethods, $this->aExportMethods['except']); |
| 175 | - if(count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 174 | + if (count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 176 | 175 | { |
| 177 | 176 | $aBaseMethods = array_diff($aBaseMethods, $this->aExportMethods['base']); |
| 178 | 177 | } |
@@ -258,17 +257,17 @@ discard block |
||
| 258 | 257 | */ |
| 259 | 258 | private function setHookMethods(array &$aHookMethods, $xValue): void |
| 260 | 259 | { |
| 261 | - foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 260 | + foreach ($xValue as $sCalledMethod => $xMethodToCall) |
|
| 262 | 261 | { |
| 263 | - if(!isset($aHookMethods[$sCalledMethod])) |
|
| 262 | + if (!isset($aHookMethods[$sCalledMethod])) |
|
| 264 | 263 | { |
| 265 | 264 | $aHookMethods[$sCalledMethod] = []; |
| 266 | 265 | } |
| 267 | - if(is_array($xMethodToCall)) |
|
| 266 | + if (is_array($xMethodToCall)) |
|
| 268 | 267 | { |
| 269 | 268 | $aHookMethods[$sCalledMethod] = array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
| 270 | 269 | } |
| 271 | - elseif(is_string($xMethodToCall)) |
|
| 270 | + elseif (is_string($xMethodToCall)) |
|
| 272 | 271 | { |
| 273 | 272 | $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
| 274 | 273 | } |
@@ -293,7 +292,7 @@ discard block |
||
| 293 | 292 | */ |
| 294 | 293 | private function addOption(string $sName, $xValue): void |
| 295 | 294 | { |
| 296 | - switch($sName) |
|
| 295 | + switch ($sName) |
|
| 297 | 296 | { |
| 298 | 297 | // Set the methods to call before processing the request |
| 299 | 298 | case '__before': |
@@ -321,11 +320,11 @@ discard block |
||
| 321 | 320 | */ |
| 322 | 321 | private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue): void |
| 323 | 322 | { |
| 324 | - if(is_string($xOptionValue)) |
|
| 323 | + if (is_string($xOptionValue)) |
|
| 325 | 324 | { |
| 326 | 325 | $xOptionValue = [$xOptionValue]; |
| 327 | 326 | } |
| 328 | - if(!is_array($xOptionValue)) |
|
| 327 | + if (!is_array($xOptionValue)) |
|
| 329 | 328 | { |
| 330 | 329 | return; // Do not save. |
| 331 | 330 | } |
@@ -355,10 +354,10 @@ discard block |
||
| 355 | 354 | */ |
| 356 | 355 | private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue): void |
| 357 | 356 | { |
| 358 | - switch($sOptionName) |
|
| 357 | + switch ($sOptionName) |
|
| 359 | 358 | { |
| 360 | 359 | case 'excluded': |
| 361 | - if((bool)$xOptionValue) |
|
| 360 | + if ((bool)$xOptionValue) |
|
| 362 | 361 | { |
| 363 | 362 | $this->addProtectedMethods($sFunctionName); |
| 364 | 363 | } |
@@ -382,12 +381,11 @@ discard block |
||
| 382 | 381 | */ |
| 383 | 382 | private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions): void |
| 384 | 383 | { |
| 385 | - foreach($aFunctionOptions as $sOptionName => $xOptionValue) |
|
| 384 | + foreach ($aFunctionOptions as $sOptionName => $xOptionValue) |
|
| 386 | 385 | { |
| 387 | 386 | substr($sOptionName, 0, 2) === '__' ? |
| 388 | 387 | // Options for PHP classes. They start with "__". |
| 389 | - $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) : |
|
| 390 | - // Options for javascript code. |
|
| 388 | + $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) : // Options for javascript code. |
|
| 391 | 389 | $this->addJsOption($sFunctionName, $sOptionName, $xOptionValue); |
| 392 | 390 | } |
| 393 | 391 | } |
@@ -403,16 +401,15 @@ discard block |
||
| 403 | 401 | $aOptions = array_merge($this->aJsOptions['*'] ?? []); // Clone the array |
| 404 | 402 | // Then add the method options. |
| 405 | 403 | $aMethodOptions = $this->aJsOptions[$sMethodName] ?? []; |
| 406 | - foreach($aMethodOptions as $sOptionName => $xOptionValue) |
|
| 404 | + foreach ($aMethodOptions as $sOptionName => $xOptionValue) |
|
| 407 | 405 | { |
| 408 | 406 | // For databags and callbacks, merge the values in a single array. |
| 409 | 407 | // For all the other options, keep the last value. |
| 410 | 408 | $aOptions[$sOptionName] = !in_array($sOptionName, ['bags', 'callback']) ? |
| 411 | - $xOptionValue : |
|
| 412 | - array_unique(array_merge($aOptions[$sOptionName] ?? [], $xOptionValue)); |
|
| 409 | + $xOptionValue : array_unique(array_merge($aOptions[$sOptionName] ?? [], $xOptionValue)); |
|
| 413 | 410 | } |
| 414 | 411 | // Since callbacks are js object names, they need a special formatting. |
| 415 | - if(isset($aOptions['callback'])) |
|
| 412 | + if (isset($aOptions['callback'])) |
|
| 416 | 413 | { |
| 417 | 414 | $aOptions['callback'] = str_replace('"', '', json_encode($aOptions['callback'])); |
| 418 | 415 | } |