From f06785df998ec33a38c325037c65dbbf371c09bc Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 11 Dec 2024 10:47:27 -0500 Subject: [PATCH] WIP --- src/Generators/ComponentGenerator.php | 2 +- src/Models/Statements/ResourceStatement.php | 2 +- src/Models/Statements/SendStatement.php | 18 +++++++++--- src/Models/Statements/SessionStatement.php | 28 +++++++++++++++---- .../components/properties-statements.php | 8 +++--- .../livewire-properties-statements.yaml | 1 + 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/Generators/ComponentGenerator.php b/src/Generators/ComponentGenerator.php index d536d9b2..dde697e4 100644 --- a/src/Generators/ComponentGenerator.php +++ b/src/Generators/ComponentGenerator.php @@ -122,7 +122,7 @@ protected function buildMethods(Component $component): string } elseif ($statement instanceof RespondStatement) { $body .= self::INDENT . $statement->output() . PHP_EOL; } elseif ($statement instanceof SessionStatement) { - $body .= self::INDENT . $statement->output() . PHP_EOL; + $body .= self::INDENT . $statement->output($component->properties(), true) . PHP_EOL; } elseif ($statement instanceof EloquentStatement) { dump($statement->operation()); $body .= self::INDENT . $statement->output($component->prefix(), $name, $using_validation) . PHP_EOL; diff --git a/src/Models/Statements/ResourceStatement.php b/src/Models/Statements/ResourceStatement.php index c7721fa3..f3d3d09b 100644 --- a/src/Models/Statements/ResourceStatement.php +++ b/src/Models/Statements/ResourceStatement.php @@ -43,7 +43,7 @@ public function paginate(): bool return $this->paginate; } - public function output(): string + public function output(array $properties = []): string { return sprintf('return new %s($%s);', $this->name(), $this->reference()); } diff --git a/src/Models/Statements/SendStatement.php b/src/Models/Statements/SendStatement.php index a0c921c8..6ace58de 100644 --- a/src/Models/Statements/SendStatement.php +++ b/src/Models/Statements/SendStatement.php @@ -83,7 +83,7 @@ private function mailOutput(): string $code = 'Mail::'; if ($this->to()) { - $code .= 'to($' . str_replace('.', '->', $this->to()) . ')->'; + $code .= sprintf('to(%s)->', $this->buildTo()); } $code .= 'send(new ' . $this->mail() . '('; @@ -102,7 +102,7 @@ private function notificationFacadeOutput(): string $code = 'Notification::'; if ($this->to()) { - $code .= 'send($' . str_replace('.', '->', $this->to()) . ', new ' . $this->mail() . '('; + $code .= sprintf('send(%s, new %s(', $this->buildTo(), $this->mail()); } if ($this->data()) { @@ -119,8 +119,7 @@ private function notificationModelOutput(): string $code = ''; if ($this->to()) { - $code .= sprintf('$%s->', str_replace('.', '->', $this->to())); - $code .= 'notify(new ' . $this->mail() . '('; + $code .= sprintf('%s->notify(new %s(', $this->buildTo(), $this->mail()); } if ($this->data()) { @@ -131,4 +130,15 @@ private function notificationModelOutput(): string return $code; } + + private function buildTo(): string + { + $variable = str_replace('.', '->', $this->to()); + + if (in_array(Str::before($this->to(), '.'), $this->properties())) { + $variable = 'this->' . $variable; + } + + return '$' . $variable; + } } diff --git a/src/Models/Statements/SessionStatement.php b/src/Models/Statements/SessionStatement.php index aa77214b..d3a8417a 100644 --- a/src/Models/Statements/SessionStatement.php +++ b/src/Models/Statements/SessionStatement.php @@ -2,6 +2,8 @@ namespace Blueprint\Models\Statements; +use Illuminate\Support\Str; + class SessionStatement { private string $operation; @@ -24,13 +26,27 @@ public function reference(): string return $this->reference; } - public function output(): string + public function output(array $properties = [], bool $livewire = false): string { - $code = '$request->session()->' . $this->operation() . '('; - $code .= "'" . $this->reference() . "', "; - $code .= '$' . str_replace('.', '->', $this->reference()); - $code .= ');'; + $template = "%ssession()->%s('%s', %s);"; + + return sprintf( + $template, + $livewire ? '' : '$request->', + $this->operation(), + $this->reference(), + $this->buildValue($properties) + ); + } + + private function buildValue(array $properties): string + { + $variable = str_replace('.', '->', $this->reference()); + + if (in_array(Str::before($this->reference(), '.'), $properties)) { + $variable = 'this->' . $variable; + } - return $code; + return '$' . $variable; } } diff --git a/tests/fixtures/components/properties-statements.php b/tests/fixtures/components/properties-statements.php index d787012b..bede9fa0 100644 --- a/tests/fixtures/components/properties-statements.php +++ b/tests/fixtures/components/properties-statements.php @@ -32,9 +32,9 @@ public function update() ProfileUpdated::dispatch($this->user); - $request->session()->flash('user.name', $user->name); + session()->flash('user.name', $this->user->name); - $user->notify(new ReviewProfile($this->user)); + $this->user->notify(new ReviewProfile($this->user)); return redirect()->route('user.show', [$this->user]); @@ -44,8 +44,8 @@ public function update() return $user; - Mail::to($user->email)->send(new ReviewProfile($this->user)); + Mail::to($this->user->email)->send(new ReviewProfile($this->user)); - $request->session()->store('user.id', $user->id); + session()->store('user.id', $this->user->id); } } diff --git a/tests/fixtures/drafts/livewire-properties-statements.yaml b/tests/fixtures/drafts/livewire-properties-statements.yaml index 4b8ee90e..a7c13b39 100644 --- a/tests/fixtures/drafts/livewire-properties-statements.yaml +++ b/tests/fixtures/drafts/livewire-properties-statements.yaml @@ -2,6 +2,7 @@ components: UpdateProfile: mount: user update: +# validate: user dispatch: UpdateProfile with:user # find: user fire: ProfileUpdated with:user