Skip to content

Commit 4921cef

Browse files
authored
Merge pull request #90 from phenixphp/feature/register-events
Register events in service provider
2 parents b073ca2 + 65bdffb commit 4921cef

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/Events/EventServiceProvider.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Phenix\Events\Console\MakeEvent;
88
use Phenix\Events\Console\MakeListener;
99
use Phenix\Events\Contracts\EventEmitter as EventEmitterContract;
10+
use Phenix\Facades\File;
1011
use Phenix\Providers\ServiceProvider;
1112

1213
class EventServiceProvider extends ServiceProvider
@@ -21,17 +22,27 @@ public function provides(string $id): bool
2122
return in_array($id, $this->provides);
2223
}
2324

24-
public function register(): void
25+
public function boot(): void
2526
{
2627
$this->getContainer()->addShared(EventEmitter::class, EventEmitter::class);
2728
$this->getContainer()->add(EventEmitterContract::class, EventEmitter::class);
28-
}
2929

30-
public function boot(): void
31-
{
3230
$this->commands([
3331
MakeEvent::class,
3432
MakeListener::class,
3533
]);
34+
35+
$this->loadEvents();
36+
}
37+
38+
private function loadEvents(): void
39+
{
40+
$eventPath = base_path('events');
41+
42+
if (File::exists($eventPath)) {
43+
foreach (File::listFilesRecursively($eventPath, '.php') as $file) {
44+
require $file;
45+
}
46+
}
3647
}
3748
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phenix\Events\Contracts\Event as EventContract;
6+
use Phenix\Facades\Event;
7+
8+
Event::on('user.registered', function (EventContract $event): void {
9+
// Handle the user registered event
10+
});

0 commit comments

Comments
 (0)