Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Dec 11, 2024
1 parent 15f0eea commit f06785d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Generators/ComponentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Statements/ResourceStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
18 changes: 14 additions & 4 deletions src/Models/Statements/SendStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() . '(';
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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;
}
}
28 changes: 22 additions & 6 deletions src/Models/Statements/SessionStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Blueprint\Models\Statements;

use Illuminate\Support\Str;

class SessionStatement
{
private string $operation;
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/fixtures/components/properties-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/fixtures/drafts/livewire-properties-statements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ components:
UpdateProfile:
mount: user
update:
# validate: user
dispatch: UpdateProfile with:user
# find: user
fire: ProfileUpdated with:user
Expand Down

0 comments on commit f06785d

Please sign in to comment.