Skip to content

Commit 5106c17

Browse files
author
Brian Faust
authored
Use the Class::dispatch syntax for class-based events (#620)
1 parent dd3d1af commit 5106c17

File tree

6 files changed

+14
-19
lines changed

6 files changed

+14
-19
lines changed

src/Models/Statements/FireStatement.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,21 @@ public function isNamedEvent(): bool
3737

3838
public function output()
3939
{
40-
$code = 'event(';
41-
4240
if ($this->isNamedEvent()) {
43-
$code .= "'" . $this->event() . "'";
44-
4541
if ($this->data()) {
46-
$code .= ', [' . $this->buildParameters($this->data()) . ']';
42+
$template = "event('%s', [%s]);";
43+
} else {
44+
$template = "event('%s');";
4745
}
4846
} else {
49-
$code .= 'new ' . $this->event() . '(';
50-
if ($this->data()) {
51-
$code .= $this->buildParameters($this->data());
52-
}
53-
54-
$code .= ')';
47+
$template = '%s::dispatch(%s);';
5548
}
5649

57-
$code .= ');';
58-
59-
return $code;
50+
return sprintf(
51+
$template,
52+
$this->event(),
53+
$this->data() ? $this->buildParameters($this->data()) : ''
54+
);
6055
}
6156

6257
private function buildParameters(array $data)

tests/fixtures/controllers/invokable-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ReportController extends Controller
1010
{
1111
public function __invoke(Request $request): View
1212
{
13-
event(new ReportGenerated());
13+
ReportGenerated::dispatch();
1414

1515
return view('report');
1616
}

tests/fixtures/controllers/nested-components.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function store(UserStoreRequest $request): RedirectResponse
3030

3131
BuildAccount::dispatch($user);
3232

33-
event(new NewUser($user));
33+
NewUser::dispatch($user);
3434

3535
$request->session()->flash('user.name', $user->name);
3636

tests/fixtures/controllers/readme-example-notification-facade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function store(PostStoreRequest $request): RedirectResponse
2929

3030
SyncMedia::dispatch($post);
3131

32-
event(new NewPost($post));
32+
NewPost::dispatch($post);
3333

3434
$request->session()->flash('post.title', $post->title);
3535

tests/fixtures/controllers/readme-example-notification-model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function store(PostStoreRequest $request): RedirectResponse
2727

2828
SyncMedia::dispatch($post);
2929

30-
event(new NewPost($post));
30+
NewPost::dispatch($post);
3131

3232
$request->session()->flash('post.title', $post->title);
3333

tests/fixtures/controllers/readme-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function store(PostStoreRequest $request): RedirectResponse
2929

3030
SyncMedia::dispatch($post);
3131

32-
event(new NewPost($post));
32+
NewPost::dispatch($post);
3333

3434
$request->session()->flash('post.title', $post->title);
3535

0 commit comments

Comments
 (0)