-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error handling for event system.
- Loading branch information
Showing
14 changed files
with
521 additions
and
24 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 |
---|---|---|
|
@@ -9,7 +9,7 @@ class Config | |
{ | ||
/** | ||
* config.php. | ||
*q | ||
* | ||
* @var array | ||
*/ | ||
private $configFile; | ||
|
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,85 @@ | ||
<?php | ||
|
||
namespace Limelight\Events; | ||
|
||
use Limelight\Exceptions\EventErrorException; | ||
|
||
class Dispatcher | ||
{ | ||
/** | ||
* Registered listeners. | ||
* | ||
* @var array | ||
*/ | ||
protected $registeredListeners = []; | ||
|
||
/** | ||
* Construct. | ||
* | ||
* @param array $configListeners | ||
*/ | ||
public function __construct(array $configListeners) | ||
{ | ||
$this->addAllListeners($configListeners); | ||
} | ||
|
||
/** | ||
* Add array of listeners from config. | ||
* | ||
* @param array $configListeners | ||
*/ | ||
public function addAllListeners(array $configListeners) | ||
{ | ||
array_walk($configListeners, [$this, 'addListeners']); | ||
} | ||
|
||
/** | ||
* Add a single listener. | ||
* | ||
* @param LimelightListener|array $listeners | ||
* @param string $eventName | ||
*/ | ||
public function addListeners($listeners, $eventName) | ||
{ | ||
$listeners = (is_array($listeners) ? $listeners : [$listeners]); | ||
|
||
array_walk($listeners, function ($listener) use ($eventName) { | ||
if (is_string($listener) && !class_exists($listener)) { | ||
throw new EventErrorException("Class {$listener} does not exist."); | ||
} | ||
|
||
$this->registeredListeners[$eventName][] = new $listener(); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all registered listeners. | ||
* | ||
* @return array | ||
*/ | ||
public function getListeners() | ||
{ | ||
return $this->registeredListeners; | ||
} | ||
|
||
/** | ||
* Call handle method on all listeners for event. | ||
* | ||
* @param string $eventName | ||
* @param mixed $payload | ||
* | ||
* @return mixed | ||
*/ | ||
public function fire($eventName, $payload = null) | ||
{ | ||
if (isset($this->registeredListeners[$eventName])) { | ||
$listeners = $this->registeredListeners[$eventName]; | ||
|
||
return array_map(function (LimelightListener $listener) use ($payload) { | ||
return $listener->handle($payload); | ||
}, $listeners); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Limelight\Events; | ||
|
||
interface LimelightListener | ||
{ | ||
/** | ||
* Respond to event. | ||
* | ||
* @param object $payload | ||
*/ | ||
public function handle($payload); | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Limelight\Exceptions; | ||
|
||
class EventErrorException extends LimelightException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $message; | ||
|
||
/** | ||
* Construct. | ||
* | ||
* @param string $message | ||
*/ | ||
public function __construct($message = 'Event error.') | ||
{ | ||
$this->message = $message; | ||
|
||
parent::__construct($message); | ||
} | ||
|
||
/** | ||
* How to display error. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->handle(); | ||
} | ||
} |
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
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
Oops, something went wrong.