@@ -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 | } |
@@ -34,224 +34,224 @@ 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 | - |
|
| 105 | - // Options from the config. |
|
| 106 | - $sSeparator = $aOptions['separator'] ?? '.'; |
|
| 107 | - $this->sSeparator = $sSeparator === '_' ? '_' : '.'; |
|
| 108 | - $this->addProtectedMethods($aOptions['protected'] ?? []); |
|
| 109 | - foreach($aOptions['functions'] ?? [] as $sNames => $aFunctionOptions) |
|
| 110 | - { |
|
| 111 | - // Names are in a comma-separated list. |
|
| 112 | - $aFunctionNames = explode(',', $sNames); |
|
| 113 | - foreach($aFunctionNames as $sFunctionName) |
|
| 114 | - { |
|
| 115 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 116 | - } |
|
| 117 | - } |
|
| 100 | +public function __construct(array $aMethods, array $aOptions, Metadata|null $xMetadata) |
|
| 101 | +{ |
|
| 102 | +$this->bExcluded = ($xMetadata?->isExcluded() ?? false) || |
|
| 103 | +(bool)($aOptions['excluded'] ?? false); |
|
| 104 | + |
|
| 105 | +// Options from the config. |
|
| 106 | +$sSeparator = $aOptions['separator'] ?? '.'; |
|
| 107 | +$this->sSeparator = $sSeparator === '_' ? '_' : '.'; |
|
| 108 | +$this->addProtectedMethods($aOptions['protected'] ?? []); |
|
| 109 | +foreach($aOptions['functions'] ?? [] as $sNames => $aFunctionOptions) |
|
| 110 | +{ |
|
| 111 | +// Names are in a comma-separated list. |
|
| 112 | +$aFunctionNames = explode(',', $sNames); |
|
| 113 | +foreach($aFunctionNames as $sFunctionName) |
|
| 114 | +{ |
|
| 115 | + $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 116 | +} |
|
| 117 | +} |
|
| 118 | 118 | |
| 119 | - // Options from the attributes or annotations. |
|
| 120 | - if($xMetadata !== null) |
|
| 121 | - { |
|
| 122 | - $this->readMetadataOptions($xMetadata); |
|
| 123 | - } |
|
| 119 | +// Options from the attributes or annotations. |
|
| 120 | +if($xMetadata !== null) |
|
| 121 | +{ |
|
| 122 | +$this->readMetadataOptions($xMetadata); |
|
| 123 | +} |
|
| 124 | 124 | |
| 125 | - $this->aPublicMethods = $this->filterPublicMethods($aMethods); |
|
| 126 | - } |
|
| 125 | +$this->aPublicMethods = $this->filterPublicMethods($aMethods); |
|
| 126 | +} |
|
| 127 | 127 | |
| 128 | - /** |
|
| 128 | +/** |
|
| 129 | 129 | * @param array|string $xMethods |
| 130 | 130 | * |
| 131 | 131 | * @return void |
| 132 | 132 | */ |
| 133 | - private function addProtectedMethods(array|string $xMethods): void |
|
| 134 | - { |
|
| 135 | - $this->aExportMethods['except'] = array_merge($this->aExportMethods['except'], |
|
| 136 | - !is_array($xMethods) ? [trim((string)$xMethods)] : |
|
| 137 | - array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 138 | - } |
|
| 133 | +private function addProtectedMethods(array|string $xMethods): void |
|
| 134 | +{ |
|
| 135 | +$this->aExportMethods['except'] = array_merge($this->aExportMethods['except'], |
|
| 136 | +!is_array($xMethods) ? [trim((string)$xMethods)] : |
|
| 137 | +array_map(fn($sMethod) => trim((string)$sMethod), $xMethods)); |
|
| 138 | +} |
|
| 139 | 139 | |
| 140 | - /** |
|
| 140 | +/** |
|
| 141 | 141 | * @param Metadata $xMetadata |
| 142 | 142 | * |
| 143 | 143 | * @return void |
| 144 | 144 | */ |
| 145 | - private function readMetadataOptions(Metadata $xMetadata): void |
|
| 146 | - { |
|
| 147 | - // Excluded methods must be merged with the existing ones. |
|
| 148 | - $aExportMethods = $xMetadata->getExportMethods(); |
|
| 149 | - $aExportMethods['except'] = array_unique(array_merge( |
|
| 150 | - $aExportMethods['except'] ?? [], $this->aExportMethods['except'])); |
|
| 151 | - $this->aExportMethods = $aExportMethods; |
|
| 152 | - foreach($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 153 | - { |
|
| 154 | - $this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 155 | - } |
|
| 156 | - } |
|
| 145 | +private function readMetadataOptions(Metadata $xMetadata): void |
|
| 146 | +{ |
|
| 147 | +// Excluded methods must be merged with the existing ones. |
|
| 148 | +$aExportMethods = $xMetadata->getExportMethods(); |
|
| 149 | +$aExportMethods['except'] = array_unique(array_merge( |
|
| 150 | +$aExportMethods['except'] ?? [], $this->aExportMethods['except'])); |
|
| 151 | +$this->aExportMethods = $aExportMethods; |
|
| 152 | +foreach($xMetadata->getProperties() as $sFunctionName => $aFunctionOptions) |
|
| 153 | +{ |
|
| 154 | +$this->addFunctionOptions($sFunctionName, $aFunctionOptions); |
|
| 155 | +} |
|
| 156 | +} |
|
| 157 | 157 | |
| 158 | - /** |
|
| 158 | +/** |
|
| 159 | 159 | * @param array $aMethods |
| 160 | 160 | * |
| 161 | 161 | * @return array |
| 162 | 162 | */ |
| 163 | - private function filterPublicMethods(array $aMethods): array |
|
| 164 | - { |
|
| 165 | - if($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 166 | - { |
|
| 167 | - return []; |
|
| 168 | - } |
|
| 163 | +private function filterPublicMethods(array $aMethods): array |
|
| 164 | +{ |
|
| 165 | +if($this->bExcluded || in_array('*', $this->aExportMethods['except'])) |
|
| 166 | +{ |
|
| 167 | +return []; |
|
| 168 | +} |
|
| 169 | 169 | |
| 170 | - $aBaseMethods = $aMethods[1]; |
|
| 171 | - $aNoMethods = $aMethods[2]; |
|
| 172 | - $aMethods = $aMethods[0]; |
|
| 173 | - if(isset($this->aExportMethods['only'])) |
|
| 174 | - { |
|
| 175 | - $aMethods = array_intersect($aMethods, $this->aExportMethods['only']); |
|
| 176 | - } |
|
| 177 | - $aMethods = array_diff($aMethods, $this->aExportMethods['except']); |
|
| 178 | - if(count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 179 | - { |
|
| 180 | - $aBaseMethods = array_diff($aBaseMethods, $this->aExportMethods['base']); |
|
| 181 | - } |
|
| 170 | +$aBaseMethods = $aMethods[1]; |
|
| 171 | +$aNoMethods = $aMethods[2]; |
|
| 172 | +$aMethods = $aMethods[0]; |
|
| 173 | +if(isset($this->aExportMethods['only'])) |
|
| 174 | +{ |
|
| 175 | +$aMethods = array_intersect($aMethods, $this->aExportMethods['only']); |
|
| 176 | +} |
|
| 177 | +$aMethods = array_diff($aMethods, $this->aExportMethods['except']); |
|
| 178 | +if(count($aBaseMethods) > 0 && isset($this->aExportMethods['base'])) |
|
| 179 | +{ |
|
| 180 | +$aBaseMethods = array_diff($aBaseMethods, $this->aExportMethods['base']); |
|
| 181 | +} |
|
| 182 | 182 | |
| 183 | - return array_values(array_diff($aMethods, $aBaseMethods, $aNoMethods)); |
|
| 184 | - } |
|
| 183 | +return array_values(array_diff($aMethods, $aBaseMethods, $aNoMethods)); |
|
| 184 | +} |
|
| 185 | 185 | |
| 186 | - /** |
|
| 186 | +/** |
|
| 187 | 187 | * @return array |
| 188 | 188 | */ |
| 189 | - public function getPublicMethods(): array |
|
| 190 | - { |
|
| 191 | - return $this->aPublicMethods; |
|
| 192 | - } |
|
| 189 | +public function getPublicMethods(): array |
|
| 190 | +{ |
|
| 191 | +return $this->aPublicMethods; |
|
| 192 | +} |
|
| 193 | 193 | |
| 194 | - /** |
|
| 194 | +/** |
|
| 195 | 195 | * @param string $sMethodName |
| 196 | 196 | * |
| 197 | 197 | * @return bool |
| 198 | 198 | */ |
| 199 | - public function isPublicMethod(string $sMethodName): bool |
|
| 200 | - { |
|
| 201 | - return in_array($sMethodName, $this->aPublicMethods); |
|
| 202 | - } |
|
| 199 | +public function isPublicMethod(string $sMethodName): bool |
|
| 200 | +{ |
|
| 201 | +return in_array($sMethodName, $this->aPublicMethods); |
|
| 202 | +} |
|
| 203 | 203 | |
| 204 | - /** |
|
| 204 | +/** |
|
| 205 | 205 | * Check if the js code for this object must be generated |
| 206 | 206 | * |
| 207 | 207 | * @return bool |
| 208 | 208 | */ |
| 209 | - public function excluded(): bool |
|
| 210 | - { |
|
| 211 | - return $this->bExcluded; |
|
| 212 | - } |
|
| 209 | +public function excluded(): bool |
|
| 210 | +{ |
|
| 211 | +return $this->bExcluded; |
|
| 212 | +} |
|
| 213 | 213 | |
| 214 | - /** |
|
| 214 | +/** |
|
| 215 | 215 | * @return string |
| 216 | 216 | */ |
| 217 | - public function separator(): string |
|
| 218 | - { |
|
| 219 | - return $this->sSeparator; |
|
| 220 | - } |
|
| 217 | +public function separator(): string |
|
| 218 | +{ |
|
| 219 | +return $this->sSeparator; |
|
| 220 | +} |
|
| 221 | 221 | |
| 222 | - /** |
|
| 222 | +/** |
|
| 223 | 223 | * @return array |
| 224 | 224 | */ |
| 225 | - public function beforeMethods(): array |
|
| 226 | - { |
|
| 227 | - return $this->aBeforeMethods; |
|
| 228 | - } |
|
| 225 | +public function beforeMethods(): array |
|
| 226 | +{ |
|
| 227 | +return $this->aBeforeMethods; |
|
| 228 | +} |
|
| 229 | 229 | |
| 230 | - /** |
|
| 230 | +/** |
|
| 231 | 231 | * @return array |
| 232 | 232 | */ |
| 233 | - public function afterMethods(): array |
|
| 234 | - { |
|
| 235 | - return $this->aAfterMethods; |
|
| 236 | - } |
|
| 233 | +public function afterMethods(): array |
|
| 234 | +{ |
|
| 235 | +return $this->aAfterMethods; |
|
| 236 | +} |
|
| 237 | 237 | |
| 238 | - /** |
|
| 238 | +/** |
|
| 239 | 239 | * @return array |
| 240 | 240 | */ |
| 241 | - public function diOptions(): array |
|
| 242 | - { |
|
| 243 | - return $this->aDiOptions; |
|
| 244 | - } |
|
| 241 | +public function diOptions(): array |
|
| 242 | +{ |
|
| 243 | +return $this->aDiOptions; |
|
| 244 | +} |
|
| 245 | 245 | |
| 246 | - /** |
|
| 246 | +/** |
|
| 247 | 247 | * @return array |
| 248 | 248 | */ |
| 249 | - public function jsOptions(): array |
|
| 250 | - { |
|
| 251 | - return $this->aJsOptions; |
|
| 252 | - } |
|
| 249 | +public function jsOptions(): array |
|
| 250 | +{ |
|
| 251 | +return $this->aJsOptions; |
|
| 252 | +} |
|
| 253 | 253 | |
| 254 | - /** |
|
| 254 | +/** |
|
| 255 | 255 | * Set hook methods |
| 256 | 256 | * |
| 257 | 257 | * @param array $aHookMethods The array of hook methods |
@@ -259,36 +259,36 @@ discard block |
||
| 259 | 259 | * |
| 260 | 260 | * @return void |
| 261 | 261 | */ |
| 262 | - private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 263 | - { |
|
| 264 | - foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 265 | - { |
|
| 266 | - if(!isset($aHookMethods[$sCalledMethod])) |
|
| 267 | - { |
|
| 268 | - $aHookMethods[$sCalledMethod] = []; |
|
| 269 | - } |
|
| 270 | - if(is_array($xMethodToCall)) |
|
| 271 | - { |
|
| 272 | - $aHookMethods[$sCalledMethod] = |
|
| 273 | - array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 274 | - continue; |
|
| 275 | - } |
|
| 276 | - if(is_string($xMethodToCall)) |
|
| 277 | - { |
|
| 278 | - $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - } |
|
| 262 | +private function setHookMethods(array &$aHookMethods, $xValue): void |
|
| 263 | +{ |
|
| 264 | +foreach($xValue as $sCalledMethod => $xMethodToCall) |
|
| 265 | +{ |
|
| 266 | +if(!isset($aHookMethods[$sCalledMethod])) |
|
| 267 | +{ |
|
| 268 | + $aHookMethods[$sCalledMethod] = []; |
|
| 269 | +} |
|
| 270 | +if(is_array($xMethodToCall)) |
|
| 271 | +{ |
|
| 272 | + $aHookMethods[$sCalledMethod] = |
|
| 273 | + array_merge($aHookMethods[$sCalledMethod], $xMethodToCall); |
|
| 274 | + continue; |
|
| 275 | +} |
|
| 276 | +if(is_string($xMethodToCall)) |
|
| 277 | +{ |
|
| 278 | + $aHookMethods[$sCalledMethod][] = $xMethodToCall; |
|
| 279 | +} |
|
| 280 | +} |
|
| 281 | +} |
|
| 282 | 282 | |
| 283 | - /** |
|
| 283 | +/** |
|
| 284 | 284 | * @param array $aDiOptions |
| 285 | 285 | */ |
| 286 | - private function addDiOption(array $aDiOptions): void |
|
| 287 | - { |
|
| 288 | - $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 289 | - } |
|
| 286 | +private function addDiOption(array $aDiOptions): void |
|
| 287 | +{ |
|
| 288 | +$this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions); |
|
| 289 | +} |
|
| 290 | 290 | |
| 291 | - /** |
|
| 291 | +/** |
|
| 292 | 292 | * Set configuration options / call options for each method |
| 293 | 293 | * |
| 294 | 294 | * @param string $sName The name of the configuration option |
@@ -296,25 +296,25 @@ discard block |
||
| 296 | 296 | * |
| 297 | 297 | * @return void |
| 298 | 298 | */ |
| 299 | - private function addOption(string $sName, $xValue): void |
|
| 300 | - { |
|
| 301 | - switch($sName) |
|
| 302 | - { |
|
| 303 | - // Set the methods to call before processing the request |
|
| 304 | - case '__before': |
|
| 305 | - $this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 306 | - break; |
|
| 307 | - // Set the methods to call after processing the request |
|
| 308 | - case '__after': |
|
| 309 | - $this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 310 | - break; |
|
| 311 | - // Set the attributes to inject in the callable object |
|
| 312 | - case '__di': |
|
| 313 | - $this->addDiOption($xValue); |
|
| 314 | - break; |
|
| 315 | - default: |
|
| 316 | - break; |
|
| 317 | - } |
|
| 299 | +private function addOption(string $sName, $xValue): void |
|
| 300 | +{ |
|
| 301 | +switch($sName) |
|
| 302 | +{ |
|
| 303 | +// Set the methods to call before processing the request |
|
| 304 | +case '__before': |
|
| 305 | +$this->setHookMethods($this->aBeforeMethods, $xValue); |
|
| 306 | +break; |
|
| 307 | +// Set the methods to call after processing the request |
|
| 308 | +case '__after': |
|
| 309 | +$this->setHookMethods($this->aAfterMethods, $xValue); |
|
| 310 | +break; |
|
| 311 | +// Set the attributes to inject in the callable object |
|
| 312 | +case '__di': |
|
| 313 | +$this->addDiOption($xValue); |
|
| 314 | +break; |
|
| 315 | +default: |
|
| 316 | +break; |
|
| 317 | +} |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -37,207 +37,207 @@ |
||
| 37 | 37 | |
| 38 | 38 | class AnnotationReader implements MetadataReaderInterface |
| 39 | 39 | { |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * @var AnnotationManager |
| 42 | 42 | */ |
| 43 | - protected $xManager; |
|
| 43 | +protected $xManager; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 45 | +/** |
|
| 46 | 46 | * @var Metadata |
| 47 | 47 | */ |
| 48 | - protected $xMetadata; |
|
| 48 | +protected $xMetadata; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Properties types, read from the "var" annotations. |
| 52 | 52 | * |
| 53 | 53 | * @var array |
| 54 | 54 | */ |
| 55 | - protected $aPropTypes; |
|
| 55 | +protected $aPropTypes; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 57 | +/** |
|
| 58 | 58 | * The type of the class member being currently processed. |
| 59 | 59 | * |
| 60 | 60 | * @var string |
| 61 | 61 | */ |
| 62 | - protected $sCurrMemberType; |
|
| 62 | +protected $sCurrMemberType; |
|
| 63 | 63 | |
| 64 | - /** |
|
| 64 | +/** |
|
| 65 | 65 | * The constructor |
| 66 | 66 | * |
| 67 | 67 | * @param AnnotationManager $xManager |
| 68 | 68 | */ |
| 69 | - public function __construct(AnnotationManager $xManager) |
|
| 70 | - { |
|
| 71 | - $this->xManager = $xManager; |
|
| 72 | - $this->xManager->registry['upload'] = UploadAnnotation::class; |
|
| 73 | - $this->xManager->registry['databag'] = DatabagAnnotation::class; |
|
| 74 | - $this->xManager->registry['exclude'] = ExcludeAnnotation::class; |
|
| 75 | - $this->xManager->registry['export'] = ExportAnnotation::class; |
|
| 76 | - $this->xManager->registry['before'] = BeforeAnnotation::class; |
|
| 77 | - $this->xManager->registry['after'] = AfterAnnotation::class; |
|
| 78 | - $this->xManager->registry['di'] = ContainerAnnotation::class; |
|
| 79 | - $this->xManager->registry['callback'] = CallbackAnnotation::class; |
|
| 80 | - // Missing standard annotations. |
|
| 81 | - // We need to define this, otherwise they throw an exception, and make the whole processing fail. |
|
| 82 | - $this->xManager->registry['const'] = false; |
|
| 83 | - $this->xManager->registry['inheritDoc'] = false; |
|
| 84 | - $this->xManager->registry['template'] = false; |
|
| 85 | - $this->xManager->registry['param-closure-this'] = false; |
|
| 86 | - } |
|
| 69 | +public function __construct(AnnotationManager $xManager) |
|
| 70 | +{ |
|
| 71 | +$this->xManager = $xManager; |
|
| 72 | +$this->xManager->registry['upload'] = UploadAnnotation::class; |
|
| 73 | +$this->xManager->registry['databag'] = DatabagAnnotation::class; |
|
| 74 | +$this->xManager->registry['exclude'] = ExcludeAnnotation::class; |
|
| 75 | +$this->xManager->registry['export'] = ExportAnnotation::class; |
|
| 76 | +$this->xManager->registry['before'] = BeforeAnnotation::class; |
|
| 77 | +$this->xManager->registry['after'] = AfterAnnotation::class; |
|
| 78 | +$this->xManager->registry['di'] = ContainerAnnotation::class; |
|
| 79 | +$this->xManager->registry['callback'] = CallbackAnnotation::class; |
|
| 80 | +// Missing standard annotations. |
|
| 81 | +// We need to define this, otherwise they throw an exception, and make the whole processing fail. |
|
| 82 | +$this->xManager->registry['const'] = false; |
|
| 83 | +$this->xManager->registry['inheritDoc'] = false; |
|
| 84 | +$this->xManager->registry['template'] = false; |
|
| 85 | +$this->xManager->registry['param-closure-this'] = false; |
|
| 86 | +} |
|
| 87 | 87 | |
| 88 | - /** |
|
| 88 | +/** |
|
| 89 | 89 | * @return array |
| 90 | 90 | */ |
| 91 | - public function getPropTypes(): array |
|
| 92 | - { |
|
| 93 | - return $this->aPropTypes; |
|
| 94 | - } |
|
| 91 | +public function getPropTypes(): array |
|
| 92 | +{ |
|
| 93 | +return $this->aPropTypes; |
|
| 94 | +} |
|
| 95 | 95 | |
| 96 | - /** |
|
| 96 | +/** |
|
| 97 | 97 | * @return bool |
| 98 | 98 | */ |
| 99 | - public function annotationIsOnProperty(): bool |
|
| 100 | - { |
|
| 101 | - return $this->sCurrMemberType === AnnotationManager::MEMBER_PROPERTY; |
|
| 102 | - } |
|
| 99 | +public function annotationIsOnProperty(): bool |
|
| 100 | +{ |
|
| 101 | +return $this->sCurrMemberType === AnnotationManager::MEMBER_PROPERTY; |
|
| 102 | +} |
|
| 103 | 103 | |
| 104 | - /** |
|
| 104 | +/** |
|
| 105 | 105 | * @param string $sClass |
| 106 | 106 | * |
| 107 | 107 | * @return void |
| 108 | 108 | * @throws AnnotationException |
| 109 | 109 | */ |
| 110 | - private function readClassAnnotations(string $sClass): void |
|
| 111 | - { |
|
| 112 | - // Only keep the annotations declared in this package. |
|
| 113 | - /** @var array<AbstractAnnotation> */ |
|
| 114 | - $aAnnotations = array_filter( |
|
| 115 | - $this->xManager->getClassAnnotations($sClass), |
|
| 116 | - fn($xAnnotation) => is_a($xAnnotation, AbstractAnnotation::class) |
|
| 117 | - ); |
|
| 118 | - // First check if the class is excluded. |
|
| 119 | - foreach($aAnnotations as $xAnnotation) |
|
| 120 | - { |
|
| 121 | - if(is_a($xAnnotation, ExcludeAnnotation::class)) |
|
| 122 | - { |
|
| 123 | - $xAnnotation->saveValue($this->xMetadata); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - if($this->xMetadata->isExcluded()) |
|
| 127 | - { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - foreach($aAnnotations as $xAnnotation) |
|
| 132 | - { |
|
| 133 | - if(!is_a($xAnnotation, ExcludeAnnotation::class)) |
|
| 134 | - { |
|
| 135 | - $xAnnotation->saveValue($this->xMetadata); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 110 | +private function readClassAnnotations(string $sClass): void |
|
| 111 | +{ |
|
| 112 | +// Only keep the annotations declared in this package. |
|
| 113 | +/** @var array<AbstractAnnotation> */ |
|
| 114 | +$aAnnotations = array_filter( |
|
| 115 | +$this->xManager->getClassAnnotations($sClass), |
|
| 116 | +fn($xAnnotation) => is_a($xAnnotation, AbstractAnnotation::class) |
|
| 117 | +); |
|
| 118 | +// First check if the class is excluded. |
|
| 119 | +foreach($aAnnotations as $xAnnotation) |
|
| 120 | +{ |
|
| 121 | +if(is_a($xAnnotation, ExcludeAnnotation::class)) |
|
| 122 | +{ |
|
| 123 | + $xAnnotation->saveValue($this->xMetadata); |
|
| 124 | +} |
|
| 125 | +} |
|
| 126 | +if($this->xMetadata->isExcluded()) |
|
| 127 | +{ |
|
| 128 | +return; |
|
| 129 | +} |
|
| 139 | 130 | |
| 140 | - /** |
|
| 131 | +foreach($aAnnotations as $xAnnotation) |
|
| 132 | +{ |
|
| 133 | +if(!is_a($xAnnotation, ExcludeAnnotation::class)) |
|
| 134 | +{ |
|
| 135 | + $xAnnotation->saveValue($this->xMetadata); |
|
| 136 | +} |
|
| 137 | +} |
|
| 138 | +} |
|
| 139 | + |
|
| 140 | +/** |
|
| 141 | 141 | * @param string $sClass |
| 142 | 142 | * @param string $sProperty |
| 143 | 143 | * |
| 144 | 144 | * @return void |
| 145 | 145 | * @throws AnnotationException |
| 146 | 146 | */ |
| 147 | - private function readPropertyAnnotations(string $sClass, string $sProperty): void |
|
| 147 | +private function readPropertyAnnotations(string $sClass, string $sProperty): void |
|
| 148 | +{ |
|
| 149 | +/** @var array<ContainerAnnotation> */ |
|
| 150 | +// Only keep the annotations declared in this package. |
|
| 151 | +$aAnnotations = array_filter( |
|
| 152 | +$this->xManager->getPropertyAnnotations($sClass, $sProperty), |
|
| 153 | +function($xAnnotation) use($sProperty) { |
|
| 154 | + // Save the property type |
|
| 155 | + if(is_a($xAnnotation, VarAnnotation::class)) |
|
| 148 | 156 | { |
| 149 | - /** @var array<ContainerAnnotation> */ |
|
| 150 | - // Only keep the annotations declared in this package. |
|
| 151 | - $aAnnotations = array_filter( |
|
| 152 | - $this->xManager->getPropertyAnnotations($sClass, $sProperty), |
|
| 153 | - function($xAnnotation) use($sProperty) { |
|
| 154 | - // Save the property type |
|
| 155 | - if(is_a($xAnnotation, VarAnnotation::class)) |
|
| 156 | - { |
|
| 157 | - $this->aPropTypes[$sProperty] = $xAnnotation->type; |
|
| 158 | - } |
|
| 159 | - // Only container annotations are allowed on properties |
|
| 160 | - return is_a($xAnnotation, ContainerAnnotation::class); |
|
| 161 | - } |
|
| 162 | - ); |
|
| 163 | - if(count($aAnnotations) > 1) |
|
| 164 | - { |
|
| 165 | - throw new AnnotationException('Only one @di annotation is allowed on a property'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - foreach($aAnnotations as $xAnnotation) |
|
| 169 | - { |
|
| 170 | - $xAnnotation->setAttr($sProperty); |
|
| 171 | - $xAnnotation->saveValue($this->xMetadata); |
|
| 172 | - } |
|
| 157 | + $this->aPropTypes[$sProperty] = $xAnnotation->type; |
|
| 173 | 158 | } |
| 159 | + // Only container annotations are allowed on properties |
|
| 160 | + return is_a($xAnnotation, ContainerAnnotation::class); |
|
| 161 | +} |
|
| 162 | +); |
|
| 163 | +if(count($aAnnotations) > 1) |
|
| 164 | +{ |
|
| 165 | +throw new AnnotationException('Only one @di annotation is allowed on a property'); |
|
| 166 | +} |
|
| 167 | + |
|
| 168 | +foreach($aAnnotations as $xAnnotation) |
|
| 169 | +{ |
|
| 170 | +$xAnnotation->setAttr($sProperty); |
|
| 171 | +$xAnnotation->saveValue($this->xMetadata); |
|
| 172 | +} |
|
| 173 | +} |
|
| 174 | 174 | |
| 175 | - /** |
|
| 175 | +/** |
|
| 176 | 176 | * @param string $sClass |
| 177 | 177 | * @param string $sMethod |
| 178 | 178 | * |
| 179 | 179 | * @return void |
| 180 | 180 | * @throws AnnotationException |
| 181 | 181 | */ |
| 182 | - private function readMethodAnnotations(string $sClass, string $sMethod): void |
|
| 183 | - { |
|
| 184 | - // Only keep the annotations declared in this package. |
|
| 185 | - /** @var array<AbstractAnnotation> */ |
|
| 186 | - $aAnnotations = array_filter( |
|
| 187 | - $this->xManager->getMethodAnnotations($sClass, $sMethod), |
|
| 188 | - fn($xAnnotation) => is_a($xAnnotation, AbstractAnnotation::class) |
|
| 189 | - ); |
|
| 190 | - foreach($aAnnotations as $xAnnotation) |
|
| 191 | - { |
|
| 192 | - $xAnnotation->saveValue($this->xMetadata, $sMethod); |
|
| 193 | - } |
|
| 194 | - } |
|
| 182 | +private function readMethodAnnotations(string $sClass, string $sMethod): void |
|
| 183 | +{ |
|
| 184 | +// Only keep the annotations declared in this package. |
|
| 185 | +/** @var array<AbstractAnnotation> */ |
|
| 186 | +$aAnnotations = array_filter( |
|
| 187 | +$this->xManager->getMethodAnnotations($sClass, $sMethod), |
|
| 188 | +fn($xAnnotation) => is_a($xAnnotation, AbstractAnnotation::class) |
|
| 189 | +); |
|
| 190 | +foreach($aAnnotations as $xAnnotation) |
|
| 191 | +{ |
|
| 192 | +$xAnnotation->saveValue($this->xMetadata, $sMethod); |
|
| 193 | +} |
|
| 194 | +} |
|
| 195 | 195 | |
| 196 | - /** |
|
| 196 | +/** |
|
| 197 | 197 | * @throws SetupException |
| 198 | 198 | */ |
| 199 | - public function getAttributes(InputData $xInput): Metadata |
|
| 200 | - { |
|
| 201 | - ContainerAnnotation::$xReader = $this; |
|
| 202 | - $this->aPropTypes = []; |
|
| 203 | - $this->xMetadata = new Metadata(); |
|
| 204 | - $sClass = $xInput->getReflectionClass()->getName(); |
|
| 205 | - |
|
| 206 | - try |
|
| 207 | - { |
|
| 208 | - // Processing class annotations |
|
| 209 | - $this->sCurrMemberType = AnnotationManager::MEMBER_CLASS; |
|
| 210 | - |
|
| 211 | - $this->readClassAnnotations($sClass); |
|
| 212 | - |
|
| 213 | - // Processing properties annotations |
|
| 214 | - $this->sCurrMemberType = AnnotationManager::MEMBER_PROPERTY; |
|
| 215 | - |
|
| 216 | - // Properties annotations |
|
| 217 | - foreach($xInput->getProperties() as $sProperty) |
|
| 218 | - { |
|
| 219 | - $this->readPropertyAnnotations($sClass, $sProperty); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // The methods annotations are not taken for excluded classes. |
|
| 223 | - if($this->xMetadata->isExcluded()) |
|
| 224 | - { |
|
| 225 | - return $this->xMetadata; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Processing methods annotations |
|
| 229 | - $this->sCurrMemberType = AnnotationManager::MEMBER_METHOD; |
|
| 230 | - |
|
| 231 | - foreach($xInput->getMethods() as $sMethod) |
|
| 232 | - { |
|
| 233 | - $this->readMethodAnnotations($sClass, $sMethod); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $this->xMetadata; |
|
| 237 | - } |
|
| 238 | - catch(AnnotationException $e) |
|
| 239 | - { |
|
| 240 | - throw new SetupException($e->getMessage()); |
|
| 241 | - } |
|
| 242 | - } |
|
| 199 | +public function getAttributes(InputData $xInput): Metadata |
|
| 200 | +{ |
|
| 201 | +ContainerAnnotation::$xReader = $this; |
|
| 202 | +$this->aPropTypes = []; |
|
| 203 | +$this->xMetadata = new Metadata(); |
|
| 204 | +$sClass = $xInput->getReflectionClass()->getName(); |
|
| 205 | + |
|
| 206 | +try |
|
| 207 | +{ |
|
| 208 | +// Processing class annotations |
|
| 209 | +$this->sCurrMemberType = AnnotationManager::MEMBER_CLASS; |
|
| 210 | + |
|
| 211 | +$this->readClassAnnotations($sClass); |
|
| 212 | + |
|
| 213 | +// Processing properties annotations |
|
| 214 | +$this->sCurrMemberType = AnnotationManager::MEMBER_PROPERTY; |
|
| 215 | + |
|
| 216 | +// Properties annotations |
|
| 217 | +foreach($xInput->getProperties() as $sProperty) |
|
| 218 | +{ |
|
| 219 | + $this->readPropertyAnnotations($sClass, $sProperty); |
|
| 220 | +} |
|
| 221 | + |
|
| 222 | +// The methods annotations are not taken for excluded classes. |
|
| 223 | +if($this->xMetadata->isExcluded()) |
|
| 224 | +{ |
|
| 225 | + return $this->xMetadata; |
|
| 226 | +} |
|
| 227 | + |
|
| 228 | +// Processing methods annotations |
|
| 229 | +$this->sCurrMemberType = AnnotationManager::MEMBER_METHOD; |
|
| 230 | + |
|
| 231 | +foreach($xInput->getMethods() as $sMethod) |
|
| 232 | +{ |
|
| 233 | + $this->readMethodAnnotations($sClass, $sMethod); |
|
| 234 | +} |
|
| 235 | + |
|
| 236 | +return $this->xMetadata; |
|
| 237 | +} |
|
| 238 | +catch(AnnotationException $e) |
|
| 239 | +{ |
|
| 240 | +throw new SetupException($e->getMessage()); |
|
| 241 | +} |
|
| 242 | +} |
|
| 243 | 243 | } |