-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Core\Events; | ||
|
||
use Core\Facades\App; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Psr\EventDispatcher\ListenerProviderInterface; | ||
use Psr\EventDispatcher\StoppableEventInterface; | ||
|
||
/** | ||
* Kerjakan eventnya. | ||
* | ||
* @class Dispatch | ||
* @package \Core\Events | ||
*/ | ||
class Dispatch implements EventDispatcherInterface | ||
{ | ||
/** | ||
* Listener object. | ||
* | ||
* @var ListenerProviderInterface $listenerProvider | ||
*/ | ||
private $listenerProvider; | ||
|
||
/** | ||
* Init object. | ||
* | ||
* @param ListenerProviderInterface $listenerProvider | ||
* @return void | ||
*/ | ||
public function __construct(ListenerProviderInterface $listenerProvider) | ||
{ | ||
$this->listenerProvider = $listenerProvider; | ||
} | ||
|
||
/** | ||
* Kerjakan. | ||
* | ||
* @param object $event | ||
* @return object | ||
*/ | ||
public function dispatch(object $event): object | ||
{ | ||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { | ||
return $event; | ||
} | ||
|
||
foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) { | ||
$listener = App::get()->singleton($listener); | ||
$listener($event); | ||
} | ||
|
||
return $event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Core\Events; | ||
|
||
/** | ||
* Fungsi tambahan yang mempermudah. | ||
* | ||
* Trait Dispatchable | ||
* @package \Core\Events | ||
*/ | ||
trait Dispatchable | ||
{ | ||
/** | ||
* Mengirimkan event dengan argumen yang diberikan. | ||
* | ||
* @param mixed ...$arguments | ||
* @return object | ||
*/ | ||
public static function dispatch(mixed ...$arguments): object | ||
{ | ||
return event(new static(...$arguments)); | ||
} | ||
|
||
/** | ||
* Kirimkan event dengan argumen yang diberikan jika betul. | ||
* | ||
* @param bool $boolean | ||
* @param mixed ...$arguments | ||
* @return object|null | ||
*/ | ||
public static function dispatchIf(bool $boolean, mixed ...$arguments): object|null | ||
{ | ||
if ($boolean) { | ||
return static::dispatch(...$arguments); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Kirimkan event dengan argumen yang diberikan jika salah. | ||
* | ||
* @param bool $boolean | ||
* @param mixed ...$arguments | ||
* @return object|null | ||
*/ | ||
public static function dispatchUnless(bool $boolean, mixed ...$arguments): object|null | ||
{ | ||
return static::dispatchIf(!$boolean, ...$arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Core\Events; | ||
|
||
use Psr\EventDispatcher\StoppableEventInterface; | ||
|
||
/** | ||
* Base class dari semua event. | ||
* | ||
* @class Event | ||
* @package \Core\Events | ||
*/ | ||
class Event implements StoppableEventInterface | ||
{ | ||
/** | ||
* Apakah tidak ada pemroses event lebih lanjut?. | ||
* | ||
* @var bool $propagationStopped | ||
*/ | ||
private $propagationStopped = false; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function isPropagationStopped(): bool | ||
{ | ||
return $this->propagationStopped; | ||
} | ||
|
||
/** | ||
* Menghentikan penyebaran event ke listener event selanjutnya. | ||
* | ||
* Jika beberapa listener event terhubung ke event yang sama | ||
* Tidak ada pemroses event selanjutnya akan terjadi setelah meggunakan perintah stop ini | ||
* | ||
* @return void | ||
*/ | ||
public function stopPropagation(): void | ||
{ | ||
$this->propagationStopped = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Core\Events; | ||
|
||
use Psr\EventDispatcher\ListenerProviderInterface; | ||
|
||
/** | ||
* Pendengar event. | ||
* | ||
* @class Listener | ||
* @package \Core\Events | ||
*/ | ||
class Listener implements ListenerProviderInterface | ||
{ | ||
/** | ||
* Listeners from Event Service Provider. | ||
* | ||
* @var array $listeners | ||
*/ | ||
private $listeners = []; | ||
|
||
/** | ||
* Init object. | ||
* | ||
* @param array<string, array<int, string>> $listeners | ||
* @return void | ||
*/ | ||
public function __construct(array $listeners) | ||
{ | ||
$this->listeners = $listeners; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getListenersForEvent(object $event): iterable | ||
{ | ||
$eventType = get_class($event); | ||
if (array_key_exists($eventType, $this->listeners)) { | ||
return $this->listeners[$eventType]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Clear event by type. | ||
* | ||
* @param string $eventType | ||
* @return void | ||
*/ | ||
public function clearListeners(string $eventType): void | ||
{ | ||
if (array_key_exists($eventType, $this->listeners)) { | ||
unset($this->listeners[$eventType]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters