Skip to content

Commit

Permalink
feat: support event
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Dec 23, 2023
1 parent f58e4bb commit 11aa0f8
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": "^8.1",
"psr/clock": "^1.0",
"psr/container": "^2.0",
"psr/event-dispatcher": "^1.0",
"ext-openssl": "*"
},
"require-dev": {
Expand Down
55 changes: 55 additions & 0 deletions src/Core/Events/Dispatch.php
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;
}
}
51 changes: 51 additions & 0 deletions src/Core/Events/Dispatchable.php
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);
}
}
42 changes: 42 additions & 0 deletions src/Core/Events/Event.php
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;
}
}
58 changes: 58 additions & 0 deletions src/Core/Events/Listener.php
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]);
}
}
}
3 changes: 2 additions & 1 deletion src/Core/Http/Respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public function redirect(string $uri, bool $force = false): void

$this->setCode($force ? Respond::HTTP_MOVED_PERMANENTLY : Respond::HTTP_MOVED_TEMPORARILY);
$this->headers->set('Location', $uri);
$this->headers->unset('Content-Type');
}

/**
Expand Down Expand Up @@ -512,7 +513,7 @@ public function transform(mixed $respond): Respond
return $this;
}

if (App::get()->singleton(Respond::class) !== $respond && $respond instanceof Respond) {
if ($respond instanceof Respond) {
$this->setCode($respond->getCode());
$this->content = $respond->getContent();
$this->headers = new Header([...$this->headers->all(), ...$respond->headers->all()]);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/errors/trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<pre style="margin: 0;white-space: nowrap;overflow: hidden; text-overflow: ellipsis; color:black !important;" class="black">[0] <?= ltrim($file[$error->getLine() - 1], '&nbsp;') ?></pre>
</div>
<?php foreach ($error->getTrace() as $id => $value) : ?>
<div class="item" <?= empty($value['file']) ? 'style="cursor: not-allowed;"' : 'onclick="document.getElementById(\'file\').innerHTML = document.getElementById(' . ($id + 1) . ').innerHTML"' ?>>
<div class="item" <?= empty($value['file']) ? 'style="cursor: auto;"' : 'onclick="document.getElementById(\'file\').innerHTML = document.getElementById(' . ($id + 1) . ').innerHTML"' ?>>
<pre style="margin: 0; white-space: nowrap;overflow: hidden; text-overflow: ellipsis;">[<?= $id + 1 ?>] <?= htmlspecialchars(isset($value['class']) ? $value['class'] . $value['type'] . $value['function'] : $value['function']) ?></pre>

<?php
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ function &translate(): \Core\Valid\Trans
}
}

if (!function_exists('event')) {
/**
* Helper method untuk menjalankan objek event.
*
* @return object
*/
function event(object $event): object
{
return \Core\Facades\App::get()->singleton(\Core\Events\Dispatch::class)->dispatch($event);
}
}

if (!function_exists('render')) {
/**
* Baca dari file .kita serta masih bentuk object.
Expand Down

0 comments on commit 11aa0f8

Please sign in to comment.