From 0a810b89db0311c453b7d0f5a0c79b16d394d80b Mon Sep 17 00:00:00 2001 From: zero1-machine Date: Thu, 18 Apr 2024 13:41:53 +0100 Subject: [PATCH 1/2] fix: magic methods on component not being called --- docs/Features.md | 2 +- src/Model/Action/Type/Magic.php | 78 ++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/docs/Features.md b/docs/Features.md index a2312b6..7b86259 100644 --- a/docs/Features.md +++ b/docs/Features.md @@ -315,7 +315,7 @@ public function refresh() {} ## Properties Assign properties including a lifecycle ```updating``` and ```updated``` method. ```php -public $foo = 'bar' +public $foo = 'bar'; /** * OPTIONAL METHOD: Gets executed right before the property gets assigned. diff --git a/src/Model/Action/Type/Magic.php b/src/Model/Action/Type/Magic.php index 2e49e56..b8887dc 100644 --- a/src/Model/Action/Type/Magic.php +++ b/src/Model/Action/Type/Magic.php @@ -8,6 +8,7 @@ namespace Magewirephp\Magewire\Model\Action\Type; +use Exception; use Magewirephp\Magewire\Component; use Magewirephp\Magewire\Exception\ComponentException; use Magewirephp\Magewire\Helper\Property as PropertyHelper; @@ -43,22 +44,49 @@ public function toggle(string $property, $component): void */ public function set(string $property, $value, Component $component): void { - if ($this->propertyHelper->containsDots($property)) { - $transform = $this->propertyHelper->transformDots($property, $value, $component); - - $property = $transform['property']; - $value = $transform['data']; - } - - // Transform a magic property value. - if (is_string($value) + try{ + // Transform a magic property value. + if (is_string($value) && strrpos($value, '$') === 0 && ($value = ltrim($value, '$')) && array_key_exists($value, $component->getPublicProperties())) { - $value = $component->{$value}; - } + $value = $component->{$value}; + } + + $nested = $this->propertyHelper->containsDots($property); + $transform = []; + + if ($nested) { + $transform = $this->propertyHelper->transformDots($property, $value, $component); + $property = $transform['property']; + $value = $transform['data']; + } - $component->{$property} = $value; + // Try to run existing pre-assignment methods if they exist. + $value = $this->updating($component, $property, $value); + + if ($nested) { + $component->{ $transform['property'] } = $this->propertyHelper->assignViaDots($transform['path'], $value, $component->{ $transform['property'] }); + } else { + $component->{ $property } = $value; + } + + // Try to run post-assignment methods if they exist. + $value = $this->updated($component, $property, $value); + + if ($nested) { + $component->{ $transform['property'] } = $this->propertyHelper->assignViaDots($transform['path'], $value, $component->{ $transform['property'] }); + } else { + $component->{ $property } = $value; + } + } catch (Exception $exception) { + $this->logger->critical( + sprintf('Magewire: Something went wrong while syncing property "%s" onto component "%s"', $property, $component->name), + ['exception' => $exception] + ); + + return; + } } /** @@ -67,4 +95,30 @@ public function set(string $property, $value, Component $component): void public function refresh(): void //phpcs:ignore { } + + private function updating(Component $component, string $property, $value) + { + $methods = ['updating' . str_replace(' ', '', ucwords(str_replace(['-', '_', '.'], ' ', $property))), 'updating']; + + foreach ($methods as $method) { + if (method_exists($component, $method)) { + $value = $component->{$method}($value, $property); + } + } + + return $value; + } + + private function updated(Component $component, string $property, $value) + { + $methods = ['updated', 'updated' . str_replace(' ', '', ucwords(str_replace(['-', '_', '.'], ' ', $property)))]; + + foreach ($methods as $method) { + if (method_exists($component, $method)) { + $value = $component->{$method}($value, $property); + } + } + + return $value; + } } From c88657eb9a0fb3002ae2bfe77536110d9cc1b2c7 Mon Sep 17 00:00:00 2001 From: adamzero1 Date: Thu, 18 Apr 2024 12:45:53 +0000 Subject: [PATCH 2/2] refact: ci requirements --- src/Model/Action/Type/Magic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Action/Type/Magic.php b/src/Model/Action/Type/Magic.php index b8887dc..8e39283 100644 --- a/src/Model/Action/Type/Magic.php +++ b/src/Model/Action/Type/Magic.php @@ -44,7 +44,7 @@ public function toggle(string $property, $component): void */ public function set(string $property, $value, Component $component): void { - try{ + try { // Transform a magic property value. if (is_string($value) && strrpos($value, '$') === 0