@@ -50,8 +50,7 @@ |
||
| 50 | 50 | public function rq(string $sClassName = ''): ?JxnCall |
| 51 | 51 | { |
| 52 | 52 | $sClassName = trim($sClassName); |
| 53 | - return $sClassName === '' ? $this->cdi->getFunctionRequestFactory() : |
|
| 54 | - $this->cdi->getComponentRequestFactory($sClassName); |
|
| 53 | + return $sClassName === '' ? $this->cdi->getFunctionRequestFactory() : $this->cdi->getComponentRequestFactory($sClassName); |
|
| 55 | 54 | } |
| 56 | 55 | |
| 57 | 56 | /** |
@@ -51,12 +51,12 @@ |
||
| 51 | 51 | $sMetadataVar = '$xMetadata'; |
| 52 | 52 | $sDataVar = '$xData'; |
| 53 | 53 | $aCalls = ["$sMetadataVar = new " . Metadata::class . '();']; |
| 54 | - foreach($xMetadata->getAttributes() as $sType => $aValues) |
|
| 54 | + foreach ($xMetadata->getAttributes() as $sType => $aValues) |
|
| 55 | 55 | { |
| 56 | - foreach($aValues as $sMethod => $xData) |
|
| 56 | + foreach ($aValues as $sMethod => $xData) |
|
| 57 | 57 | { |
| 58 | 58 | $aCalls[] = "$sDataVar = {$sMetadataVar}->{$sType}('{$sMethod}');"; |
| 59 | - foreach($xData->encode($sDataVar) as $sCall) |
|
| 59 | + foreach ($xData->encode($sDataVar) as $sCall) |
|
| 60 | 60 | { |
| 61 | 61 | $aCalls[] = $sCall; |
| 62 | 62 | } |
@@ -28,60 +28,60 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | class MetadataCache |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * @param string $sCacheDir |
| 33 | 33 | */ |
| 34 | - public function __construct(private string $sCacheDir) |
|
| 35 | - {} |
|
| 34 | +public function __construct(private string $sCacheDir) |
|
| 35 | +{} |
|
| 36 | 36 | |
| 37 | - /** |
|
| 37 | +/** |
|
| 38 | 38 | * @param string $sClass |
| 39 | 39 | * |
| 40 | 40 | * @return string |
| 41 | 41 | */ |
| 42 | - private function filepath(string $sClass): string |
|
| 43 | - { |
|
| 44 | - $sFilename = trim(str_replace(['\\', '.'], DIRECTORY_SEPARATOR, |
|
| 45 | - strtolower($sClass)), DIRECTORY_SEPARATOR); |
|
| 46 | - return rtrim($this->sCacheDir, "/\\") . DIRECTORY_SEPARATOR . |
|
| 47 | - "metadata" . DIRECTORY_SEPARATOR . "{$sFilename}.php"; |
|
| 48 | - } |
|
| 42 | +private function filepath(string $sClass): string |
|
| 43 | +{ |
|
| 44 | +$sFilename = trim(str_replace(['\\', '.'], DIRECTORY_SEPARATOR, |
|
| 45 | +strtolower($sClass)), DIRECTORY_SEPARATOR); |
|
| 46 | +return rtrim($this->sCacheDir, "/\\") . DIRECTORY_SEPARATOR . |
|
| 47 | +"metadata" . DIRECTORY_SEPARATOR . "{$sFilename}.php"; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * Generate the PHP code to create a metadata object. |
| 52 | 52 | * |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - private function encode(Metadata $xMetadata): array |
|
| 55 | +private function encode(Metadata $xMetadata): array |
|
| 56 | +{ |
|
| 57 | +$sMetadataVar = '$xMetadata'; |
|
| 58 | +$sDataVar = '$xData'; |
|
| 59 | +$aCalls = ["$sMetadataVar = new " . Metadata::class . '();']; |
|
| 60 | +foreach($xMetadata->getAttributes() as $sType => $aValues) |
|
| 61 | +{ |
|
| 62 | +foreach($aValues as $sMethod => $xData) |
|
| 63 | +{ |
|
| 64 | + $aCalls[] = "$sDataVar = {$sMetadataVar}->{$sType}('{$sMethod}');"; |
|
| 65 | + foreach($xData->encode($sDataVar) as $sCall) |
|
| 56 | 66 | { |
| 57 | - $sMetadataVar = '$xMetadata'; |
|
| 58 | - $sDataVar = '$xData'; |
|
| 59 | - $aCalls = ["$sMetadataVar = new " . Metadata::class . '();']; |
|
| 60 | - foreach($xMetadata->getAttributes() as $sType => $aValues) |
|
| 61 | - { |
|
| 62 | - foreach($aValues as $sMethod => $xData) |
|
| 63 | - { |
|
| 64 | - $aCalls[] = "$sDataVar = {$sMetadataVar}->{$sType}('{$sMethod}');"; |
|
| 65 | - foreach($xData->encode($sDataVar) as $sCall) |
|
| 66 | - { |
|
| 67 | - $aCalls[] = $sCall; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - $aCalls[] = "return $sMetadataVar;"; |
|
| 72 | - return $aCalls; |
|
| 67 | + $aCalls[] = $sCall; |
|
| 73 | 68 | } |
| 69 | +} |
|
| 70 | +} |
|
| 71 | +$aCalls[] = "return $sMetadataVar;"; |
|
| 72 | +return $aCalls; |
|
| 73 | +} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * @param string $sClass |
| 77 | 77 | * @param Metadata $xMetadata |
| 78 | 78 | * |
| 79 | 79 | * @return void |
| 80 | 80 | */ |
| 81 | - public function save(string $sClass, Metadata $xMetadata): void |
|
| 82 | - { |
|
| 83 | - $sDataCode = implode("\n ", $this->encode($xMetadata)); |
|
| 84 | - $sPhpCode = <<<CODE |
|
| 81 | +public function save(string $sClass, Metadata $xMetadata): void |
|
| 82 | +{ |
|
| 83 | +$sDataCode = implode("\n ", $this->encode($xMetadata)); |
|
| 84 | +$sPhpCode = <<<CODE |
|
| 85 | 85 | <?php |
| 86 | 86 | |
| 87 | 87 | return function() { |
@@ -89,21 +89,21 @@ discard block |
||
| 89 | 89 | }; |
| 90 | 90 | |
| 91 | 91 | CODE; |
| 92 | - // Recursively create the directories. |
|
| 93 | - $sPath = $this->filepath($sClass); |
|
| 94 | - @mkdir(dirname($sPath), 0755, true); |
|
| 95 | - file_put_contents($sPath, $sPhpCode); |
|
| 96 | - } |
|
| 92 | +// Recursively create the directories. |
|
| 93 | +$sPath = $this->filepath($sClass); |
|
| 94 | +@mkdir(dirname($sPath), 0755, true); |
|
| 95 | +file_put_contents($sPath, $sPhpCode); |
|
| 96 | +} |
|
| 97 | 97 | |
| 98 | - /** |
|
| 98 | +/** |
|
| 99 | 99 | * @param string $sClass |
| 100 | 100 | * |
| 101 | 101 | * @return Metadata|null |
| 102 | 102 | */ |
| 103 | - public function read(string $sClass): ?Metadata |
|
| 104 | - { |
|
| 105 | - $sPath = $this->filepath($sClass); |
|
| 106 | - $fCreator = file_exists($sPath) ? require $sPath : null; |
|
| 107 | - return $fCreator instanceof Closure ? $fCreator() : null; |
|
| 108 | - } |
|
| 103 | +public function read(string $sClass): ?Metadata |
|
| 104 | +{ |
|
| 105 | +$sPath = $this->filepath($sClass); |
|
| 106 | +$fCreator = file_exists($sPath) ? require $sPath : null; |
|
| 107 | +return $fCreator instanceof Closure ? $fCreator() : null; |
|
| 108 | +} |
|
| 109 | 109 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | protected function validateAttr(string $sAttr): void |
| 53 | 53 | { |
| 54 | - if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sAttr) > 0) |
|
| 54 | + if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sAttr) > 0) |
|
| 55 | 55 | { |
| 56 | 56 | return; |
| 57 | 57 | } |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | protected function validateClass(string $sClass): void |
| 67 | 67 | { |
| 68 | - if(preg_match('/^(\\\)?([a-zA-Z][a-zA-Z0-9_]*)(\\\[a-zA-Z][a-zA-Z0-9_]*)*$/', $sClass) > 0) |
|
| 68 | + if (preg_match('/^(\\\)?([a-zA-Z][a-zA-Z0-9_]*)(\\\[a-zA-Z][a-zA-Z0-9_]*)*$/', $sClass) > 0) |
|
| 69 | 69 | { |
| 70 | 70 | return; |
| 71 | 71 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | public function encode(string $sVarName): array |
| 93 | 93 | { |
| 94 | 94 | $aCalls = []; |
| 95 | - foreach($this->aProperties as $sAttr => $sClass) |
|
| 95 | + foreach ($this->aProperties as $sAttr => $sClass) |
|
| 96 | 96 | { |
| 97 | 97 | $aCalls[] = "{$sVarName}->addValue('$sAttr', '" . addslashes($sClass) . "');"; |
| 98 | 98 | } |
@@ -21,81 +21,81 @@ |
||
| 21 | 21 | |
| 22 | 22 | class ContainerData extends AbstractData |
| 23 | 23 | { |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * The properties to get from the container |
| 26 | 26 | * |
| 27 | 27 | * @var array |
| 28 | 28 | */ |
| 29 | - protected $aProperties = []; |
|
| 29 | +protected $aProperties = []; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * @return string |
| 33 | 33 | */ |
| 34 | - public function getName(): string |
|
| 35 | - { |
|
| 36 | - return '__di'; |
|
| 37 | - } |
|
| 34 | +public function getName(): string |
|
| 35 | +{ |
|
| 36 | +return '__di'; |
|
| 37 | +} |
|
| 38 | 38 | |
| 39 | - /** |
|
| 39 | +/** |
|
| 40 | 40 | * @return mixed |
| 41 | 41 | */ |
| 42 | - public function getValue(): mixed |
|
| 43 | - { |
|
| 44 | - return $this->aProperties; |
|
| 45 | - } |
|
| 42 | +public function getValue(): mixed |
|
| 43 | +{ |
|
| 44 | +return $this->aProperties; |
|
| 45 | +} |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * @param string $sAttr |
| 49 | 49 | * |
| 50 | 50 | * @return void |
| 51 | 51 | */ |
| 52 | - protected function validateAttr(string $sAttr): void |
|
| 53 | - { |
|
| 54 | - if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sAttr) > 0) |
|
| 55 | - { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 58 | - throw new SetupException("$sAttr is not a valid \"attr\" value for di"); |
|
| 59 | - } |
|
| 52 | +protected function validateAttr(string $sAttr): void |
|
| 53 | +{ |
|
| 54 | +if(preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sAttr) > 0) |
|
| 55 | +{ |
|
| 56 | +return; |
|
| 57 | +} |
|
| 58 | +throw new SetupException("$sAttr is not a valid \"attr\" value for di"); |
|
| 59 | +} |
|
| 60 | 60 | |
| 61 | - /** |
|
| 61 | +/** |
|
| 62 | 62 | * @param string $sClass |
| 63 | 63 | * |
| 64 | 64 | * @return void |
| 65 | 65 | */ |
| 66 | - protected function validateClass(string $sClass): void |
|
| 67 | - { |
|
| 68 | - if(preg_match('/^(\\\)?([a-zA-Z][a-zA-Z0-9_]*)(\\\[a-zA-Z][a-zA-Z0-9_]*)*$/', $sClass) > 0) |
|
| 69 | - { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - throw new SetupException("$sClass is not a valid \"class\" value for di"); |
|
| 73 | - } |
|
| 66 | +protected function validateClass(string $sClass): void |
|
| 67 | +{ |
|
| 68 | +if(preg_match('/^(\\\)?([a-zA-Z][a-zA-Z0-9_]*)(\\\[a-zA-Z][a-zA-Z0-9_]*)*$/', $sClass) > 0) |
|
| 69 | +{ |
|
| 70 | +return; |
|
| 71 | +} |
|
| 72 | +throw new SetupException("$sClass is not a valid \"class\" value for di"); |
|
| 73 | +} |
|
| 74 | 74 | |
| 75 | - /** |
|
| 75 | +/** |
|
| 76 | 76 | * @param string $sAttr |
| 77 | 77 | * @param string $sClass |
| 78 | 78 | * |
| 79 | 79 | * @return void |
| 80 | 80 | */ |
| 81 | - public function addValue(string $sAttr, string $sClass): void |
|
| 82 | - { |
|
| 83 | - $this->validateAttr($sAttr); |
|
| 84 | - $this->validateClass($sClass); |
|
| 81 | +public function addValue(string $sAttr, string $sClass): void |
|
| 82 | +{ |
|
| 83 | +$this->validateAttr($sAttr); |
|
| 84 | +$this->validateClass($sClass); |
|
| 85 | 85 | |
| 86 | - $this->aProperties[$sAttr] = $sClass; |
|
| 87 | - } |
|
| 86 | +$this->aProperties[$sAttr] = $sClass; |
|
| 87 | +} |
|
| 88 | 88 | |
| 89 | - /** |
|
| 89 | +/** |
|
| 90 | 90 | * @inheritDoc |
| 91 | 91 | */ |
| 92 | - public function encode(string $sVarName): array |
|
| 93 | - { |
|
| 94 | - $aCalls = []; |
|
| 95 | - foreach($this->aProperties as $sAttr => $sClass) |
|
| 96 | - { |
|
| 97 | - $aCalls[] = "{$sVarName}->addValue('$sAttr', '" . addslashes($sClass) . "');"; |
|
| 98 | - } |
|
| 99 | - return $aCalls; |
|
| 100 | - } |
|
| 92 | +public function encode(string $sVarName): array |
|
| 93 | +{ |
|
| 94 | +$aCalls = []; |
|
| 95 | +foreach($this->aProperties as $sAttr => $sClass) |
|
| 96 | +{ |
|
| 97 | +$aCalls[] = "{$sVarName}->addValue('$sAttr', '" . addslashes($sClass) . "');"; |
|
| 98 | +} |
|
| 99 | +return $aCalls; |
|
| 100 | +} |
|
| 101 | 101 | } |
@@ -85,8 +85,7 @@ discard block |
||
| 85 | 85 | if(is_string($xOptions)) |
| 86 | 86 | { |
| 87 | 87 | $xOptions = ['include' => $xOptions]; |
| 88 | - } |
|
| 89 | - elseif(!is_array($xOptions)) |
|
| 88 | + } elseif(!is_array($xOptions)) |
|
| 90 | 89 | { |
| 91 | 90 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 92 | 91 | } |
@@ -261,8 +260,7 @@ discard block |
||
| 261 | 260 | |
| 262 | 261 | $sError = 'errors.objects.call'; |
| 263 | 262 | $xCallableObject->call($this->xTarget); |
| 264 | - } |
|
| 265 | - catch(ReflectionException|SetupException $e) |
|
| 263 | + } catch(ReflectionException|SetupException $e) |
|
| 266 | 264 | { |
| 267 | 265 | // Unable to execute the requested class or method |
| 268 | 266 | $this->throwException($e->getMessage(), $sError, $aErrorParams); |
@@ -87,15 +87,15 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function checkOptions(string $sCallable, $xOptions): array |
| 89 | 89 | { |
| 90 | - if(!$this->xValidator->validateClass(trim($sCallable))) |
|
| 90 | + if (!$this->xValidator->validateClass(trim($sCallable))) |
|
| 91 | 91 | { |
| 92 | 92 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 93 | 93 | } |
| 94 | - if(is_string($xOptions)) |
|
| 94 | + if (is_string($xOptions)) |
|
| 95 | 95 | { |
| 96 | 96 | $xOptions = ['include' => $xOptions]; |
| 97 | 97 | } |
| 98 | - elseif(!is_array($xOptions)) |
|
| 98 | + elseif (!is_array($xOptions)) |
|
| 99 | 99 | { |
| 100 | 100 | throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
| 101 | 101 | } |
@@ -140,16 +140,16 @@ discard block |
||
| 140 | 140 | private function addCallable(CallableObject $xCallableObject): void |
| 141 | 141 | { |
| 142 | 142 | $aCallableMethods = $xCallableObject->getCallableMethods(); |
| 143 | - if($xCallableObject->excluded() || count($aCallableMethods) === 0) |
|
| 143 | + if ($xCallableObject->excluded() || count($aCallableMethods) === 0) |
|
| 144 | 144 | { |
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $aCallableObject = &$this->aCallableObjects; |
| 149 | 149 | $sJsName = $xCallableObject->getJsName(); |
| 150 | - foreach(explode('.', $sJsName) as $sName) |
|
| 150 | + foreach (explode('.', $sJsName) as $sName) |
|
| 151 | 151 | { |
| 152 | - if(!isset($aCallableObject['children'][$sName])) |
|
| 152 | + if (!isset($aCallableObject['children'][$sName])) |
|
| 153 | 153 | { |
| 154 | 154 | $aCallableObject['children'][$sName] = []; |
| 155 | 155 | } |
@@ -170,12 +170,11 @@ discard block |
||
| 170 | 170 | private function renderMethod(string $sIndent, array $aTemplateVars): string |
| 171 | 171 | { |
| 172 | 172 | $aOptions = []; |
| 173 | - foreach($aTemplateVars['aMethod']['options'] as $sKey => $sValue) |
|
| 173 | + foreach ($aTemplateVars['aMethod']['options'] as $sKey => $sValue) |
|
| 174 | 174 | { |
| 175 | 175 | $aOptions[] = "$sKey: $sValue"; |
| 176 | 176 | } |
| 177 | - $aTemplateVars['sArguments'] = count($aOptions) === 0 ? 'args' : |
|
| 178 | - 'args, { ' . implode(', ', $aOptions) . ' }'; |
|
| 177 | + $aTemplateVars['sArguments'] = count($aOptions) === 0 ? 'args' : 'args, { ' . implode(', ', $aOptions) . ' }'; |
|
| 179 | 178 | |
| 180 | 179 | return $sIndent . trim($this->xTemplateEngine |
| 181 | 180 | ->render('jaxon::callables/method.js', $aTemplateVars)); |
@@ -197,11 +196,10 @@ discard block |
||
| 197 | 196 | 'aMethod' => $aMethod, |
| 198 | 197 | 'nIndex' => $aCallable['index'] ?? 0, |
| 199 | 198 | ]); |
| 200 | - $aMethods = !isset($aCallable['methods']) ? [] : |
|
| 201 | - array_map($fMethodCallback, $aCallable['methods']); |
|
| 199 | + $aMethods = !isset($aCallable['methods']) ? [] : array_map($fMethodCallback, $aCallable['methods']); |
|
| 202 | 200 | |
| 203 | 201 | $aChildren = []; |
| 204 | - foreach($aCallable['children'] ?? [] as $sName => $aChild) |
|
| 202 | + foreach ($aCallable['children'] ?? [] as $sName => $aChild) |
|
| 205 | 203 | { |
| 206 | 204 | $aChildren[] = $this->renderChild("$sName:", "$sJsClass.$sName", |
| 207 | 205 | $aChild, $nIndent) . ','; |
@@ -243,7 +241,7 @@ discard block |
||
| 243 | 241 | |
| 244 | 242 | $this->aCallableNames = []; |
| 245 | 243 | $this->aCallableObjects = ['children' => []]; |
| 246 | - foreach($this->cdi->getCallableObjects() as $xCallableObject) |
|
| 244 | + foreach ($this->cdi->getCallableObjects() as $xCallableObject) |
|
| 247 | 245 | { |
| 248 | 246 | $this->addCallable($xCallableObject); |
| 249 | 247 | } |
@@ -253,7 +251,7 @@ discard block |
||
| 253 | 251 | 'aCallableNames' => $this->aCallableNames, |
| 254 | 252 | ]) |
| 255 | 253 | ]; |
| 256 | - foreach($this->aCallableObjects['children'] as $sJsClass => $aCallable) |
|
| 254 | + foreach ($this->aCallableObjects['children'] as $sJsClass => $aCallable) |
|
| 257 | 255 | { |
| 258 | 256 | $aScripts[] = $this->renderChild("{$this->sPrefix}$sJsClass =", |
| 259 | 257 | $sJsClass, $aCallable) . ';'; |
@@ -309,7 +307,7 @@ discard block |
||
| 309 | 307 | // Will be used to print a translated error message. |
| 310 | 308 | $aErrorParams = ['class' => $sClassName, 'method' => $sMethodName]; |
| 311 | 309 | |
| 312 | - if(!$this->xValidator->validateJsObject($sClassName) || |
|
| 310 | + if (!$this->xValidator->validateJsObject($sClassName) || |
|
| 313 | 311 | !$this->xValidator->validateMethod($sMethodName)) |
| 314 | 312 | { |
| 315 | 313 | // Unable to find the requested object or method |
@@ -323,7 +321,7 @@ discard block |
||
| 323 | 321 | /** @var CallableObject */ |
| 324 | 322 | $xCallableObject = $this->getCallable($sClassName); |
| 325 | 323 | |
| 326 | - if($xCallableObject->excluded($sMethodName)) |
|
| 324 | + if ($xCallableObject->excluded($sMethodName)) |
|
| 327 | 325 | { |
| 328 | 326 | // Unable to find the requested class or method |
| 329 | 327 | $this->throwException('', 'errors.objects.excluded', $aErrorParams); |
@@ -332,7 +330,7 @@ discard block |
||
| 332 | 330 | $sError = 'errors.objects.call'; |
| 333 | 331 | $xCallableObject->call($this->xTarget); |
| 334 | 332 | } |
| 335 | - catch(ReflectionException|SetupException $e) |
|
| 333 | + catch (ReflectionException|SetupException $e) |
|
| 336 | 334 | { |
| 337 | 335 | // Unable to execute the requested class or method |
| 338 | 336 | $this->throwException($e->getMessage(), $sError, $aErrorParams); |
@@ -20,50 +20,50 @@ |
||
| 20 | 20 | |
| 21 | 21 | class InputData |
| 22 | 22 | { |
| 23 | - /** |
|
| 23 | +/** |
|
| 24 | 24 | * @var ReflectionClass |
| 25 | 25 | */ |
| 26 | - private $xReflectionClass; |
|
| 26 | +private $xReflectionClass; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 28 | +/** |
|
| 29 | 29 | * @param ReflectionClass|string $xClass |
| 30 | 30 | * @param array $aMethods |
| 31 | 31 | * @param array $aProperties |
| 32 | 32 | */ |
| 33 | - public function __construct(ReflectionClass|string $xClass, |
|
| 34 | - private array $aMethods = [], private array $aProperties = []) |
|
| 35 | - { |
|
| 36 | - $this->xReflectionClass = is_string($xClass) ? |
|
| 37 | - new ReflectionClass($xClass) : $xClass; |
|
| 38 | - } |
|
| 33 | +public function __construct(ReflectionClass|string $xClass, |
|
| 34 | +private array $aMethods = [], private array $aProperties = []) |
|
| 35 | +{ |
|
| 36 | +$this->xReflectionClass = is_string($xClass) ? |
|
| 37 | +new ReflectionClass($xClass) : $xClass; |
|
| 38 | +} |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * Get the reflection class |
| 42 | 42 | * |
| 43 | 43 | * @return ReflectionClass |
| 44 | 44 | */ |
| 45 | - public function getReflectionClass(): ReflectionClass |
|
| 46 | - { |
|
| 47 | - return $this->xReflectionClass; |
|
| 48 | - } |
|
| 45 | +public function getReflectionClass(): ReflectionClass |
|
| 46 | +{ |
|
| 47 | +return $this->xReflectionClass; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * The methods to check for metadata |
| 52 | 52 | * |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - public function getMethods(): array |
|
| 56 | - { |
|
| 57 | - return $this->aMethods; |
|
| 58 | - } |
|
| 55 | +public function getMethods(): array |
|
| 56 | +{ |
|
| 57 | +return $this->aMethods; |
|
| 58 | +} |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The properties to check for metadata |
| 62 | 62 | * |
| 63 | 63 | * @return array |
| 64 | 64 | */ |
| 65 | - public function getProperties(): array |
|
| 66 | - { |
|
| 67 | - return $this->aProperties; |
|
| 68 | - } |
|
| 65 | +public function getProperties(): array |
|
| 66 | +{ |
|
| 67 | +return $this->aProperties; |
|
| 68 | +} |
|
| 69 | 69 | } |
@@ -16,42 +16,42 @@ |
||
| 16 | 16 | |
| 17 | 17 | class ExcludeData extends AbstractData |
| 18 | 18 | { |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * @var bool |
| 21 | 21 | */ |
| 22 | - private bool $bValue = true; |
|
| 22 | +private bool $bValue = true; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * @return string |
| 26 | 26 | */ |
| 27 | - public function getName(): string |
|
| 28 | - { |
|
| 29 | - return 'protected'; |
|
| 30 | - } |
|
| 27 | +public function getName(): string |
|
| 28 | +{ |
|
| 29 | +return 'protected'; |
|
| 30 | +} |
|
| 31 | 31 | |
| 32 | - /** |
|
| 32 | +/** |
|
| 33 | 33 | * @return mixed |
| 34 | 34 | */ |
| 35 | - public function getValue(): mixed |
|
| 36 | - { |
|
| 37 | - return $this->bValue; |
|
| 38 | - } |
|
| 35 | +public function getValue(): mixed |
|
| 36 | +{ |
|
| 37 | +return $this->bValue; |
|
| 38 | +} |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * @param bool $bValue |
| 42 | 42 | * |
| 43 | 43 | * @return void |
| 44 | 44 | */ |
| 45 | - public function setValue(bool $bValue): void |
|
| 46 | - { |
|
| 47 | - $this->bValue = $bValue; |
|
| 48 | - } |
|
| 45 | +public function setValue(bool $bValue): void |
|
| 46 | +{ |
|
| 47 | +$this->bValue = $bValue; |
|
| 48 | +} |
|
| 49 | 49 | |
| 50 | - /** |
|
| 50 | +/** |
|
| 51 | 51 | * @inheritDoc |
| 52 | 52 | */ |
| 53 | - public function encode(string $sVarName): array |
|
| 54 | - { |
|
| 55 | - return ["{$sVarName}->setValue(" . ($this->bValue ? 'true' : 'false') . ");"]; |
|
| 56 | - } |
|
| 53 | +public function encode(string $sVarName): array |
|
| 54 | +{ |
|
| 55 | +return ["{$sVarName}->setValue(" . ($this->bValue ? 'true' : 'false') . ");"]; |
|
| 56 | +} |
|
| 57 | 57 | } |
@@ -81,8 +81,7 @@ discard block |
||
| 81 | 81 | { |
| 82 | 82 | // Local file system adapter |
| 83 | 83 | $this->registerAdapter('local', function(string $sRootDir, $xOptions) { |
| 84 | - return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : |
|
| 85 | - new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
| 84 | + return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
| 86 | 85 | }); |
| 87 | 86 | |
| 88 | 87 | // AWS S3 file system adapter |
@@ -140,7 +139,7 @@ discard block |
||
| 140 | 139 | public function filesystem(string $sField = ''): Filesystem |
| 141 | 140 | { |
| 142 | 141 | $sField = trim($sField); |
| 143 | - if(isset($this->aFilesystems[$sField])) |
|
| 142 | + if (isset($this->aFilesystems[$sField])) |
|
| 144 | 143 | { |
| 145 | 144 | return $this->aFilesystems[$sField]; |
| 146 | 145 | } |
@@ -150,18 +149,18 @@ discard block |
||
| 150 | 149 | $sRootDir = $this->xConfigManager->getOption('upload.default.dir', ''); |
| 151 | 150 | $aOptions = $this->xConfigManager->getOption('upload.default.options'); |
| 152 | 151 | $sConfigKey = "upload.files.$sField"; |
| 153 | - if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
| 152 | + if ($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
| 154 | 153 | { |
| 155 | 154 | $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage); |
| 156 | 155 | $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir); |
| 157 | 156 | $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions); |
| 158 | 157 | } |
| 159 | 158 | |
| 160 | - if(!is_string($sRootDir)) |
|
| 159 | + if (!is_string($sRootDir)) |
|
| 161 | 160 | { |
| 162 | 161 | throw new RequestException($this->xTranslator->trans('errors.upload.dir')); |
| 163 | 162 | } |
| 164 | - if(!isset($this->aAdapters[$sStorage])) |
|
| 163 | + if (!isset($this->aAdapters[$sStorage])) |
|
| 165 | 164 | { |
| 166 | 165 | throw new RequestException($this->xTranslator->trans('errors.upload.adapter')); |
| 167 | 166 | } |
@@ -189,8 +189,7 @@ |
||
| 189 | 189 | { |
| 190 | 190 | $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
| 191 | 191 | } |
| 192 | - } |
|
| 193 | - catch(ReflectionException $e) |
|
| 192 | + } catch(ReflectionException $e) |
|
| 194 | 193 | { |
| 195 | 194 | throw new SetupException($this->cn()->g(Translator::class) |
| 196 | 195 | ->trans('errors.class.invalid', ['name' => $sClassName])); |
@@ -141,7 +141,7 @@ |
||
| 141 | 141 | */ |
| 142 | 142 | public function val(string $sKey, $xValue): void |
| 143 | 143 | { |
| 144 | - $this->xContainer->offsetSet($sKey, $xValue); |
|
| 144 | + $this->xContainer->offsetSet($sKey, $xValue); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -40,85 +40,85 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | class ComponentContainer |
| 42 | 42 | { |
| 43 | - use Traits\DiAutoTrait; |
|
| 44 | - use Traits\ComponentTrait; |
|
| 43 | +use Traits\DiAutoTrait; |
|
| 44 | +use Traits\ComponentTrait; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 46 | +/** |
|
| 47 | 47 | * The Dependency Injection Container for registered classes |
| 48 | 48 | * |
| 49 | 49 | * @var PimpleContainer |
| 50 | 50 | */ |
| 51 | - private $xContainer; |
|
| 51 | +private $xContainer; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 53 | +/** |
|
| 54 | 54 | * This will be set only when getting the object targetted by the ajax request. |
| 55 | 55 | * |
| 56 | 56 | * @var Target|null |
| 57 | 57 | */ |
| 58 | - private $xTarget = null; |
|
| 58 | +private $xTarget = null; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * The class constructor |
| 62 | 62 | * |
| 63 | 63 | * @param Container $di |
| 64 | 64 | */ |
| 65 | - public function __construct(private Container $di) |
|
| 66 | - { |
|
| 67 | - $this->xContainer = new PimpleContainer(); |
|
| 68 | - $this->val(ComponentContainer::class, $this); |
|
| 69 | - |
|
| 70 | - // Register the call factory for registered functions |
|
| 71 | - $this->set($this->getRequestFactoryKey(JxnCall::class), fn() => |
|
| 72 | - new JxnCall($this->di->g(ConfigManager::class) |
|
| 73 | - ->getOption('core.prefix.function', ''))); |
|
| 74 | - |
|
| 75 | - // Register the pagination component, but do not export to js. |
|
| 76 | - $this->saveComponent(Pagination::class, [ |
|
| 77 | - 'excluded' => true, |
|
| 78 | - 'separator' => '.', |
|
| 79 | - // The namespace has the same name as the Component class. |
|
| 80 | - 'namespace' => Component::class, |
|
| 81 | - ]); |
|
| 82 | - |
|
| 83 | - // Register the logger component, and export to js. |
|
| 84 | - $this->di->callback()->boot(function() { |
|
| 85 | - if($this->di->config()->loggingEnabled()) |
|
| 86 | - { |
|
| 87 | - $this->saveComponent(LoggerComponent::class, [ |
|
| 88 | - 'separator' => '.', |
|
| 89 | - // The namespace has the same name as the Component class. |
|
| 90 | - 'namespace' => Component::class, |
|
| 91 | - ]); |
|
| 92 | - } |
|
| 93 | - }); |
|
| 94 | - |
|
| 95 | - $this->setComponentPublicMethods('node', NodeComponent::class, ['item', 'html']); |
|
| 96 | - $this->setComponentPublicMethods('func', FuncComponent::class, ['paginator']); |
|
| 97 | - } |
|
| 65 | +public function __construct(private Container $di) |
|
| 66 | +{ |
|
| 67 | +$this->xContainer = new PimpleContainer(); |
|
| 68 | +$this->val(ComponentContainer::class, $this); |
|
| 69 | + |
|
| 70 | +// Register the call factory for registered functions |
|
| 71 | +$this->set($this->getRequestFactoryKey(JxnCall::class), fn() => |
|
| 72 | +new JxnCall($this->di->g(ConfigManager::class) |
|
| 73 | + ->getOption('core.prefix.function', ''))); |
|
| 74 | + |
|
| 75 | +// Register the pagination component, but do not export to js. |
|
| 76 | +$this->saveComponent(Pagination::class, [ |
|
| 77 | +'excluded' => true, |
|
| 78 | +'separator' => '.', |
|
| 79 | +// The namespace has the same name as the Component class. |
|
| 80 | +'namespace' => Component::class, |
|
| 81 | +]); |
|
| 82 | + |
|
| 83 | +// Register the logger component, and export to js. |
|
| 84 | +$this->di->callback()->boot(function() { |
|
| 85 | +if($this->di->config()->loggingEnabled()) |
|
| 86 | +{ |
|
| 87 | + $this->saveComponent(LoggerComponent::class, [ |
|
| 88 | + 'separator' => '.', |
|
| 89 | + // The namespace has the same name as the Component class. |
|
| 90 | + 'namespace' => Component::class, |
|
| 91 | + ]); |
|
| 92 | +} |
|
| 93 | +}); |
|
| 94 | + |
|
| 95 | +$this->setComponentPublicMethods('node', NodeComponent::class, ['item', 'html']); |
|
| 96 | +$this->setComponentPublicMethods('func', FuncComponent::class, ['paginator']); |
|
| 97 | +} |
|
| 98 | 98 | |
| 99 | - /** |
|
| 99 | +/** |
|
| 100 | 100 | * The container for parameters |
| 101 | 101 | * |
| 102 | 102 | * @return Container |
| 103 | 103 | */ |
| 104 | - protected function cn(): Container |
|
| 105 | - { |
|
| 106 | - return $this->di; |
|
| 107 | - } |
|
| 104 | +protected function cn(): Container |
|
| 105 | +{ |
|
| 106 | +return $this->di; |
|
| 107 | +} |
|
| 108 | 108 | |
| 109 | - /** |
|
| 109 | +/** |
|
| 110 | 110 | * Check if a class is defined in the container |
| 111 | 111 | * |
| 112 | 112 | * @param class-string $sClass The full class name |
| 113 | 113 | * |
| 114 | 114 | * @return bool |
| 115 | 115 | */ |
| 116 | - public function has(string $sClass): bool |
|
| 117 | - { |
|
| 118 | - return $this->xContainer->offsetExists($sClass); |
|
| 119 | - } |
|
| 116 | +public function has(string $sClass): bool |
|
| 117 | +{ |
|
| 118 | +return $this->xContainer->offsetExists($sClass); |
|
| 119 | +} |
|
| 120 | 120 | |
| 121 | - /** |
|
| 121 | +/** |
|
| 122 | 122 | * Save a closure in the container |
| 123 | 123 | * |
| 124 | 124 | * @param class-string $sClass The full class name |
@@ -126,12 +126,12 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @return void |
| 128 | 128 | */ |
| 129 | - public function set(string $sClass, Closure $xClosure): void |
|
| 130 | - { |
|
| 131 | - $this->xContainer->offsetSet($sClass, fn() => $xClosure($this)); |
|
| 132 | - } |
|
| 129 | +public function set(string $sClass, Closure $xClosure): void |
|
| 130 | +{ |
|
| 131 | +$this->xContainer->offsetSet($sClass, fn() => $xClosure($this)); |
|
| 132 | +} |
|
| 133 | 133 | |
| 134 | - /** |
|
| 134 | +/** |
|
| 135 | 135 | * Save a value in the container |
| 136 | 136 | * |
| 137 | 137 | * @param string|class-string $sKey The key |
@@ -139,12 +139,12 @@ discard block |
||
| 139 | 139 | * |
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | - public function val(string $sKey, $xValue): void |
|
| 143 | - { |
|
| 144 | - $this->xContainer->offsetSet($sKey, $xValue); |
|
| 145 | - } |
|
| 142 | +public function val(string $sKey, $xValue): void |
|
| 143 | +{ |
|
| 144 | +$this->xContainer->offsetSet($sKey, $xValue); |
|
| 145 | +} |
|
| 146 | 146 | |
| 147 | - /** |
|
| 147 | +/** |
|
| 148 | 148 | * Get a class instance |
| 149 | 149 | * |
| 150 | 150 | * @template T |
@@ -152,12 +152,12 @@ discard block |
||
| 152 | 152 | * |
| 153 | 153 | * @return T |
| 154 | 154 | */ |
| 155 | - public function get(string $sClass): mixed |
|
| 156 | - { |
|
| 157 | - return $this->xContainer->offsetGet($sClass); |
|
| 158 | - } |
|
| 155 | +public function get(string $sClass): mixed |
|
| 156 | +{ |
|
| 157 | +return $this->xContainer->offsetGet($sClass); |
|
| 158 | +} |
|
| 159 | 159 | |
| 160 | - /** |
|
| 160 | +/** |
|
| 161 | 161 | * Get a component when one of its method needs to be called |
| 162 | 162 | * |
| 163 | 163 | * @template T |
@@ -166,17 +166,17 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return T|null |
| 168 | 168 | */ |
| 169 | - public function getTargetComponent(string $sClassName, Target $xTarget): mixed |
|
| 170 | - { |
|
| 171 | - // Set the target only when getting the object targetted by the ajax request. |
|
| 172 | - $this->xTarget = $xTarget; |
|
| 173 | - $xComponent = $this->get($sClassName); |
|
| 174 | - $this->xTarget = null; |
|
| 169 | +public function getTargetComponent(string $sClassName, Target $xTarget): mixed |
|
| 170 | +{ |
|
| 171 | +// Set the target only when getting the object targetted by the ajax request. |
|
| 172 | +$this->xTarget = $xTarget; |
|
| 173 | +$xComponent = $this->get($sClassName); |
|
| 174 | +$this->xTarget = null; |
|
| 175 | 175 | |
| 176 | - return $xComponent; |
|
| 177 | - } |
|
| 176 | +return $xComponent; |
|
| 177 | +} |
|
| 178 | 178 | |
| 179 | - /** |
|
| 179 | +/** |
|
| 180 | 180 | * Register a component and its options |
| 181 | 181 | * |
| 182 | 182 | * @param class-string $sClassName The class name |
@@ -184,40 +184,40 @@ discard block |
||
| 184 | 184 | * |
| 185 | 185 | * @return void |
| 186 | 186 | */ |
| 187 | - public function saveComponent(string $sClassName, array $aOptions): void |
|
| 188 | - { |
|
| 189 | - try |
|
| 190 | - { |
|
| 191 | - // Make sure the registered class exists |
|
| 192 | - if(isset($aOptions['include'])) |
|
| 193 | - { |
|
| 194 | - require_once $aOptions['include']; |
|
| 195 | - } |
|
| 196 | - $xReflectionClass = new ReflectionClass($sClassName); |
|
| 197 | - // Check if the class is registrable |
|
| 198 | - if(!$xReflectionClass->isInstantiable()) |
|
| 199 | - { |
|
| 200 | - return; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $this->_saveClassOptions($sClassName, $aOptions); |
|
| 204 | - |
|
| 205 | - $sClassKey = $this->getReflectionClassKey($sClassName); |
|
| 206 | - $this->val($sClassKey, $xReflectionClass); |
|
| 207 | - // Register the user class, but only if the user didn't already. |
|
| 208 | - if(!$this->has($sClassName)) |
|
| 209 | - { |
|
| 210 | - $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - catch(ReflectionException $e) |
|
| 214 | - { |
|
| 215 | - throw new SetupException($this->cn()->g(Translator::class) |
|
| 216 | - ->trans('errors.class.invalid', ['name' => $sClassName])); |
|
| 217 | - } |
|
| 218 | - } |
|
| 187 | +public function saveComponent(string $sClassName, array $aOptions): void |
|
| 188 | +{ |
|
| 189 | +try |
|
| 190 | +{ |
|
| 191 | +// Make sure the registered class exists |
|
| 192 | +if(isset($aOptions['include'])) |
|
| 193 | +{ |
|
| 194 | + require_once $aOptions['include']; |
|
| 195 | +} |
|
| 196 | +$xReflectionClass = new ReflectionClass($sClassName); |
|
| 197 | +// Check if the class is registrable |
|
| 198 | +if(!$xReflectionClass->isInstantiable()) |
|
| 199 | +{ |
|
| 200 | + return; |
|
| 201 | +} |
|
| 219 | 202 | |
| 220 | - /** |
|
| 203 | +$this->_saveClassOptions($sClassName, $aOptions); |
|
| 204 | + |
|
| 205 | +$sClassKey = $this->getReflectionClassKey($sClassName); |
|
| 206 | +$this->val($sClassKey, $xReflectionClass); |
|
| 207 | +// Register the user class, but only if the user didn't already. |
|
| 208 | +if(!$this->has($sClassName)) |
|
| 209 | +{ |
|
| 210 | + $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
|
| 211 | +} |
|
| 212 | +} |
|
| 213 | +catch(ReflectionException $e) |
|
| 214 | +{ |
|
| 215 | +throw new SetupException($this->cn()->g(Translator::class) |
|
| 216 | + ->trans('errors.class.invalid', ['name' => $sClassName])); |
|
| 217 | +} |
|
| 218 | +} |
|
| 219 | + |
|
| 220 | +/** |
|
| 221 | 221 | * Register a component |
| 222 | 222 | * |
| 223 | 223 | * @param string $sComponentId The component name |
@@ -225,74 +225,74 @@ discard block |
||
| 225 | 225 | * @return string |
| 226 | 226 | * @throws SetupException |
| 227 | 227 | */ |
| 228 | - private function _registerComponent(string $sComponentId): string |
|
| 229 | - { |
|
| 230 | - // Replace all separators ('.' or '_') with antislashes, and trim the class name. |
|
| 231 | - $sClassName = trim(str_replace(['.', '_'], '\\', $sComponentId), '\\'); |
|
| 232 | - |
|
| 233 | - $sComponentObject = $this->getCallableObjectKey($sClassName); |
|
| 234 | - // Prevent duplication. It's important not to use the class name here. |
|
| 235 | - if($this->has($sComponentObject)) |
|
| 236 | - { |
|
| 237 | - return $sClassName; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - // Register the helper class |
|
| 241 | - $this->set($this->getCallableHelperKey($sClassName), function() use($sClassName) { |
|
| 242 | - $xFactory = $this->di->getCallFactory(); |
|
| 243 | - return new ComponentHelper($this, $xFactory->rq($sClassName), |
|
| 244 | - $xFactory, $this->di->getViewRenderer(), |
|
| 245 | - $this->di->getLogger(), $this->di->getSessionManager(), |
|
| 246 | - $this->di->getStash(), $this->di->getUploadHandler()); |
|
| 247 | - }); |
|
| 248 | - |
|
| 249 | - $this->discoverComponent($sClassName); |
|
| 250 | - |
|
| 251 | - // Register the callable object |
|
| 252 | - $this->set($sComponentObject, function() use($sComponentId, $sClassName) { |
|
| 253 | - $aOptions = $this->_getClassOptions($sComponentId); |
|
| 254 | - $xReflectionClass = $this->get($this->getReflectionClassKey($sClassName)); |
|
| 255 | - $xOptions = $this->getComponentOptions($xReflectionClass, $aOptions); |
|
| 256 | - return new CallableObject($this, $this->di, $xReflectionClass, $xOptions); |
|
| 257 | - }); |
|
| 258 | - |
|
| 259 | - // Initialize the user class instance |
|
| 260 | - $this->xContainer->extend($sClassName, function($xClassInstance) use($sClassName) { |
|
| 261 | - if($xClassInstance instanceof AbstractComponent) |
|
| 262 | - { |
|
| 263 | - $xHelper = $this->get($this->getCallableHelperKey($sClassName)); |
|
| 264 | - $xHelper->xTarget = $this->xTarget; |
|
| 265 | - |
|
| 266 | - // Call the protected "initComponent()" method of the Component class. |
|
| 267 | - $cSetter = function($di, $xHelper) { |
|
| 268 | - $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 269 | - }; |
|
| 270 | - $cSetter = $cSetter->bindTo($xClassInstance, $xClassInstance); |
|
| 271 | - call_user_func($cSetter, $this->di, $xHelper); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - // Run the callbacks for class initialisation |
|
| 275 | - $this->di->g(CallbackManager::class)->onInit($xClassInstance); |
|
| 276 | - |
|
| 277 | - // Set attributes from the DI container. |
|
| 278 | - // The class level DI options are set on any component. |
|
| 279 | - // The method level DI options are set only on the targetted component. |
|
| 280 | - /** @var CallableObject */ |
|
| 281 | - $xCallableObject = $this->get($this->getCallableObjectKey($sClassName)); |
|
| 282 | - $xCallableObject->setDiClassAttributes($xClassInstance); |
|
| 283 | - if($this->xTarget !== null) |
|
| 284 | - { |
|
| 285 | - $sMethodName = $this->xTarget->getMethodName(); |
|
| 286 | - $xCallableObject->setDiMethodAttributes($xClassInstance, $sMethodName); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return $xClassInstance; |
|
| 290 | - }); |
|
| 291 | - |
|
| 292 | - return $sClassName; |
|
| 293 | - } |
|
| 228 | +private function _registerComponent(string $sComponentId): string |
|
| 229 | +{ |
|
| 230 | +// Replace all separators ('.' or '_') with antislashes, and trim the class name. |
|
| 231 | +$sClassName = trim(str_replace(['.', '_'], '\\', $sComponentId), '\\'); |
|
| 232 | + |
|
| 233 | +$sComponentObject = $this->getCallableObjectKey($sClassName); |
|
| 234 | +// Prevent duplication. It's important not to use the class name here. |
|
| 235 | +if($this->has($sComponentObject)) |
|
| 236 | +{ |
|
| 237 | +return $sClassName; |
|
| 238 | +} |
|
| 239 | + |
|
| 240 | +// Register the helper class |
|
| 241 | +$this->set($this->getCallableHelperKey($sClassName), function() use($sClassName) { |
|
| 242 | +$xFactory = $this->di->getCallFactory(); |
|
| 243 | +return new ComponentHelper($this, $xFactory->rq($sClassName), |
|
| 244 | + $xFactory, $this->di->getViewRenderer(), |
|
| 245 | + $this->di->getLogger(), $this->di->getSessionManager(), |
|
| 246 | + $this->di->getStash(), $this->di->getUploadHandler()); |
|
| 247 | +}); |
|
| 248 | + |
|
| 249 | +$this->discoverComponent($sClassName); |
|
| 250 | + |
|
| 251 | +// Register the callable object |
|
| 252 | +$this->set($sComponentObject, function() use($sComponentId, $sClassName) { |
|
| 253 | +$aOptions = $this->_getClassOptions($sComponentId); |
|
| 254 | +$xReflectionClass = $this->get($this->getReflectionClassKey($sClassName)); |
|
| 255 | +$xOptions = $this->getComponentOptions($xReflectionClass, $aOptions); |
|
| 256 | +return new CallableObject($this, $this->di, $xReflectionClass, $xOptions); |
|
| 257 | +}); |
|
| 258 | + |
|
| 259 | +// Initialize the user class instance |
|
| 260 | +$this->xContainer->extend($sClassName, function($xClassInstance) use($sClassName) { |
|
| 261 | +if($xClassInstance instanceof AbstractComponent) |
|
| 262 | +{ |
|
| 263 | + $xHelper = $this->get($this->getCallableHelperKey($sClassName)); |
|
| 264 | + $xHelper->xTarget = $this->xTarget; |
|
| 265 | + |
|
| 266 | + // Call the protected "initComponent()" method of the Component class. |
|
| 267 | + $cSetter = function($di, $xHelper) { |
|
| 268 | + $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 269 | + }; |
|
| 270 | + $cSetter = $cSetter->bindTo($xClassInstance, $xClassInstance); |
|
| 271 | + call_user_func($cSetter, $this->di, $xHelper); |
|
| 272 | +} |
|
| 273 | + |
|
| 274 | +// Run the callbacks for class initialisation |
|
| 275 | +$this->di->g(CallbackManager::class)->onInit($xClassInstance); |
|
| 276 | + |
|
| 277 | +// Set attributes from the DI container. |
|
| 278 | +// The class level DI options are set on any component. |
|
| 279 | +// The method level DI options are set only on the targetted component. |
|
| 280 | +/** @var CallableObject */ |
|
| 281 | +$xCallableObject = $this->get($this->getCallableObjectKey($sClassName)); |
|
| 282 | +$xCallableObject->setDiClassAttributes($xClassInstance); |
|
| 283 | +if($this->xTarget !== null) |
|
| 284 | +{ |
|
| 285 | + $sMethodName = $this->xTarget->getMethodName(); |
|
| 286 | + $xCallableObject->setDiMethodAttributes($xClassInstance, $sMethodName); |
|
| 287 | +} |
|
| 288 | + |
|
| 289 | +return $xClassInstance; |
|
| 290 | +}); |
|
| 291 | + |
|
| 292 | +return $sClassName; |
|
| 293 | +} |
|
| 294 | 294 | |
| 295 | - /** |
|
| 295 | +/** |
|
| 296 | 296 | * Get the callable object for a given class |
| 297 | 297 | * The callable object is registered if it is not already in the DI. |
| 298 | 298 | * |
@@ -301,13 +301,13 @@ discard block |
||
| 301 | 301 | * @return CallableObject|null |
| 302 | 302 | * @throws SetupException |
| 303 | 303 | */ |
| 304 | - public function makeCallableObject(string $sComponentId): ?CallableObject |
|
| 305 | - { |
|
| 306 | - $sClassName = $this->_registerComponent($sComponentId); |
|
| 307 | - return $this->get($this->getCallableObjectKey($sClassName)); |
|
| 308 | - } |
|
| 304 | +public function makeCallableObject(string $sComponentId): ?CallableObject |
|
| 305 | +{ |
|
| 306 | +$sClassName = $this->_registerComponent($sComponentId); |
|
| 307 | +return $this->get($this->getCallableObjectKey($sClassName)); |
|
| 308 | +} |
|
| 309 | 309 | |
| 310 | - /** |
|
| 310 | +/** |
|
| 311 | 311 | * Get an instance of a component by name |
| 312 | 312 | * |
| 313 | 313 | * @template T |
@@ -316,53 +316,53 @@ discard block |
||
| 316 | 316 | * @return T|null |
| 317 | 317 | * @throws SetupException |
| 318 | 318 | */ |
| 319 | - public function makeComponent(string $sClassName): mixed |
|
| 320 | - { |
|
| 321 | - $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 322 | - $sClassName = $this->_registerComponent($sComponentId); |
|
| 323 | - return $this->get($sClassName); |
|
| 324 | - } |
|
| 319 | +public function makeComponent(string $sClassName): mixed |
|
| 320 | +{ |
|
| 321 | +$sComponentId = str_replace('\\', '.', $sClassName); |
|
| 322 | +$sClassName = $this->_registerComponent($sComponentId); |
|
| 323 | +return $this->get($sClassName); |
|
| 324 | +} |
|
| 325 | 325 | |
| 326 | - /** |
|
| 326 | +/** |
|
| 327 | 327 | * Get a factory for a call to a registered function. |
| 328 | 328 | * |
| 329 | 329 | * @return JxnCall |
| 330 | 330 | */ |
| 331 | - public function getFunctionRequestFactory(): JxnCall |
|
| 332 | - { |
|
| 333 | - return $this->get($this->getRequestFactoryKey(JxnCall::class)); |
|
| 334 | - } |
|
| 331 | +public function getFunctionRequestFactory(): JxnCall |
|
| 332 | +{ |
|
| 333 | +return $this->get($this->getRequestFactoryKey(JxnCall::class)); |
|
| 334 | +} |
|
| 335 | 335 | |
| 336 | - /** |
|
| 336 | +/** |
|
| 337 | 337 | * Get a factory for a call to a registered component. |
| 338 | 338 | * |
| 339 | 339 | * @param class-string $sClassName |
| 340 | 340 | * |
| 341 | 341 | * @return JxnCall|null |
| 342 | 342 | */ |
| 343 | - public function getComponentRequestFactory(string $sClassName): ?JxnCall |
|
| 343 | +public function getComponentRequestFactory(string $sClassName): ?JxnCall |
|
| 344 | +{ |
|
| 345 | +$sClassName = trim($sClassName, " \t"); |
|
| 346 | +if($sClassName === '') |
|
| 347 | +{ |
|
| 348 | +return null; |
|
| 349 | +} |
|
| 350 | + |
|
| 351 | +$sFactoryKey = $this->getRequestFactoryKey($sClassName); |
|
| 352 | +if(!$this->has($sFactoryKey)) |
|
| 353 | +{ |
|
| 354 | +$this->xContainer->offsetSet($sFactoryKey, function() use($sClassName) { |
|
| 355 | + $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 356 | + if(!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 344 | 357 | { |
| 345 | - $sClassName = trim($sClassName, " \t"); |
|
| 346 | - if($sClassName === '') |
|
| 347 | - { |
|
| 348 | - return null; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $sFactoryKey = $this->getRequestFactoryKey($sClassName); |
|
| 352 | - if(!$this->has($sFactoryKey)) |
|
| 353 | - { |
|
| 354 | - $this->xContainer->offsetSet($sFactoryKey, function() use($sClassName) { |
|
| 355 | - $sComponentId = str_replace('\\', '.', $sClassName); |
|
| 356 | - if(!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 357 | - { |
|
| 358 | - return null; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $xConfigManager = $this->di->g(ConfigManager::class); |
|
| 362 | - $sPrefix = $xConfigManager->getOption('core.prefix.class', ''); |
|
| 363 | - return new JxnClassCall($sPrefix . $xCallable->getJsName()); |
|
| 364 | - }); |
|
| 365 | - } |
|
| 366 | - return $this->get($sFactoryKey); |
|
| 358 | + return null; |
|
| 367 | 359 | } |
| 360 | + |
|
| 361 | + $xConfigManager = $this->di->g(ConfigManager::class); |
|
| 362 | + $sPrefix = $xConfigManager->getOption('core.prefix.class', ''); |
|
| 363 | + return new JxnClassCall($sPrefix . $xCallable->getJsName()); |
|
| 364 | +}); |
|
| 365 | +} |
|
| 366 | +return $this->get($sFactoryKey); |
|
| 367 | +} |
|
| 368 | 368 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | // Register the logger component, and export to js. |
| 84 | 84 | $this->di->callback()->boot(function() { |
| 85 | - if($this->di->config()->loggingEnabled()) |
|
| 85 | + if ($this->di->config()->loggingEnabled()) |
|
| 86 | 86 | { |
| 87 | 87 | $this->saveComponent(LoggerComponent::class, [ |
| 88 | 88 | 'separator' => '.', |
@@ -189,13 +189,13 @@ discard block |
||
| 189 | 189 | try |
| 190 | 190 | { |
| 191 | 191 | // Make sure the registered class exists |
| 192 | - if(isset($aOptions['include'])) |
|
| 192 | + if (isset($aOptions['include'])) |
|
| 193 | 193 | { |
| 194 | 194 | require_once $aOptions['include']; |
| 195 | 195 | } |
| 196 | 196 | $xReflectionClass = new ReflectionClass($sClassName); |
| 197 | 197 | // Check if the class is registrable |
| 198 | - if(!$xReflectionClass->isInstantiable()) |
|
| 198 | + if (!$xReflectionClass->isInstantiable()) |
|
| 199 | 199 | { |
| 200 | 200 | return; |
| 201 | 201 | } |
@@ -205,12 +205,12 @@ discard block |
||
| 205 | 205 | $sClassKey = $this->getReflectionClassKey($sClassName); |
| 206 | 206 | $this->val($sClassKey, $xReflectionClass); |
| 207 | 207 | // Register the user class, but only if the user didn't already. |
| 208 | - if(!$this->has($sClassName)) |
|
| 208 | + if (!$this->has($sClassName)) |
|
| 209 | 209 | { |
| 210 | 210 | $this->set($sClassName, fn() => $this->make($this->get($sClassKey))); |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | - catch(ReflectionException $e) |
|
| 213 | + catch (ReflectionException $e) |
|
| 214 | 214 | { |
| 215 | 215 | throw new SetupException($this->cn()->g(Translator::class) |
| 216 | 216 | ->trans('errors.class.invalid', ['name' => $sClassName])); |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | $sComponentObject = $this->getCallableObjectKey($sClassName); |
| 234 | 234 | // Prevent duplication. It's important not to use the class name here. |
| 235 | - if($this->has($sComponentObject)) |
|
| 235 | + if ($this->has($sComponentObject)) |
|
| 236 | 236 | { |
| 237 | 237 | return $sClassName; |
| 238 | 238 | } |
@@ -258,14 +258,14 @@ discard block |
||
| 258 | 258 | |
| 259 | 259 | // Initialize the user class instance |
| 260 | 260 | $this->xContainer->extend($sClassName, function($xClassInstance) use($sClassName) { |
| 261 | - if($xClassInstance instanceof AbstractComponent) |
|
| 261 | + if ($xClassInstance instanceof AbstractComponent) |
|
| 262 | 262 | { |
| 263 | 263 | $xHelper = $this->get($this->getCallableHelperKey($sClassName)); |
| 264 | 264 | $xHelper->xTarget = $this->xTarget; |
| 265 | 265 | |
| 266 | 266 | // Call the protected "initComponent()" method of the Component class. |
| 267 | 267 | $cSetter = function($di, $xHelper) { |
| 268 | - $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 268 | + $this->initComponent($di, $xHelper); // "$this" here refers to the Component class. |
|
| 269 | 269 | }; |
| 270 | 270 | $cSetter = $cSetter->bindTo($xClassInstance, $xClassInstance); |
| 271 | 271 | call_user_func($cSetter, $this->di, $xHelper); |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | /** @var CallableObject */ |
| 281 | 281 | $xCallableObject = $this->get($this->getCallableObjectKey($sClassName)); |
| 282 | 282 | $xCallableObject->setDiClassAttributes($xClassInstance); |
| 283 | - if($this->xTarget !== null) |
|
| 283 | + if ($this->xTarget !== null) |
|
| 284 | 284 | { |
| 285 | 285 | $sMethodName = $this->xTarget->getMethodName(); |
| 286 | 286 | $xCallableObject->setDiMethodAttributes($xClassInstance, $sMethodName); |
@@ -343,17 +343,17 @@ discard block |
||
| 343 | 343 | public function getComponentRequestFactory(string $sClassName): ?JxnCall |
| 344 | 344 | { |
| 345 | 345 | $sClassName = trim($sClassName, " \t"); |
| 346 | - if($sClassName === '') |
|
| 346 | + if ($sClassName === '') |
|
| 347 | 347 | { |
| 348 | 348 | return null; |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | $sFactoryKey = $this->getRequestFactoryKey($sClassName); |
| 352 | - if(!$this->has($sFactoryKey)) |
|
| 352 | + if (!$this->has($sFactoryKey)) |
|
| 353 | 353 | { |
| 354 | 354 | $this->xContainer->offsetSet($sFactoryKey, function() use($sClassName) { |
| 355 | 355 | $sComponentId = str_replace('\\', '.', $sClassName); |
| 356 | - if(!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 356 | + if (!($xCallable = $this->makeCallableObject($sComponentId))) |
|
| 357 | 357 | { |
| 358 | 358 | return null; |
| 359 | 359 | } |
@@ -16,23 +16,23 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Jaxon |
| 18 | 18 | { |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * @const string |
| 21 | 21 | */ |
| 22 | - public const VERSION = 'Jaxon 5.x'; |
|
| 22 | +public const VERSION = 'Jaxon 5.x'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * @const string |
| 26 | 26 | */ |
| 27 | - public const CALLABLE_CLASS = 'CallableClass'; |
|
| 27 | +public const CALLABLE_CLASS = 'CallableClass'; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * @const string |
| 31 | 31 | */ |
| 32 | - public const CALLABLE_DIR = 'CallableDir'; |
|
| 32 | +public const CALLABLE_DIR = 'CallableDir'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 34 | +/** |
|
| 35 | 35 | * @const string |
| 36 | 36 | */ |
| 37 | - public const CALLABLE_FUNCTION = 'CallableFunction'; |
|
| 37 | +public const CALLABLE_FUNCTION = 'CallableFunction'; |
|
| 38 | 38 | } |