Skip to content

Commit

Permalink
now routes can be organized by priorities, up versioned but bacwards …
Browse files Browse the repository at this point in the history
…compatible still
  • Loading branch information
Christian Blanquera committed Jan 14, 2019
1 parent e35bdfc commit 480fdef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
72 changes: 41 additions & 31 deletions src/Event/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ class EventHandler implements EventInterface
*/
const STATUS_INCOMPLETE = 308;

/**
/**
* @var array $observers cache of event handlers
*/
protected $observers = [];

/**
/**
* @var array $regexp listeners with regexp
*/
protected $regex = [];

/**
/**
* @var array|bool $meta The meta data for the current event
*/
protected $meta = true;
Expand Down Expand Up @@ -83,15 +83,15 @@ public function match(string $event): array
$matches[$event] = array(
'event' => $event,
'pattern' => $event,
'variables' => array()
'variables' => []
);
}

//deal with regexp
foreach ($this->regex as $pattern) {
//if it matches
if (preg_match_all($pattern, $event, $match)) {
$variables = array();
$variables = [];

if (is_array($match) && !empty($match)) {
//flatten
Expand Down Expand Up @@ -192,7 +192,10 @@ public function on($event, callable $callback, int $priority = 0): EventInterfac
$this->regex[] = $event;
}

$this->observers[$event][$priority][] = $observer;
$this->observers[$event][] = [
'priority' => $priority,
'observer' => $observer
];

return $this;
}
Expand All @@ -217,35 +220,45 @@ public function trigger(string $event, ...$args): EventInterface
return $this;
}

$observers = [];
foreach ($matches as $match) {
//add on to match
$match['args'] = $args;
$event = $match['pattern'];

//if no direct observers
if (!isset($this->observers[$event])) {
continue;
}

//sort it out
krsort($this->observers[$event]);
$observers = call_user_func_array('array_merge', $this->observers[$event]);
//add args on to match
$match['args'] = $args;

//for each observer
foreach ($observers as $observer) {
//then loop the observers
foreach ($this->observers[$event] as $observer) {
//get the callback
$callback = $observer->getCallback();
$callback = $observer['observer']->getCallback();
$priority = $observer['priority'];
//add on to match
$match['callback'] = $callback;
//set the current
$this->meta = $match;

//if this is the same event, call the method, if the method returns false
if (call_user_func_array($callback, $args) === false) {
//report a 308
$this->meta = self::STATUS_INCOMPLETE;
return $this;
}
$observers[$priority][] = $match;
}
}

//then sort by priority
krsort($observers);
if (!empty($observers)) {
$observers = call_user_func_array('array_merge', $observers);
}

//call the callbacks
foreach ($observers as $match) {
//set the current
$this->meta = $match;

//if this is the same event, call the method, if the method returns false
if (call_user_func_array($match['callback'], $args) === false) {
//report a 308
$this->meta = self::STATUS_INCOMPLETE;
return $this;
}
}

Expand All @@ -265,7 +278,7 @@ public function trigger(string $event, ...$args): EventInterface
protected function removeObserversByCallback(callable $callback): EventInterface
{
//find the callback
foreach ($this->observers as $event => $priorities) {
foreach ($this->observers as $event => $observer) {
$this->removeObserversByEvent($event, $callback);
}

Expand All @@ -289,13 +302,10 @@ protected function removeObserversByEvent(string $event, callable $callback): Ev
}

//'foobar' => array(
foreach ($this->observers[$event] as $priority => $observers) {
//0 => array(
foreach ($observers as $i => $observer) {
//0 => callback
if ($observer->assertEquals($callback)) {
unset($this->observers[$event][$priority][$i]);
}
foreach ($this->observers[$event] as $i => $observer) {
//0 => callback
if ($observer['observer']->assertEquals($callback)) {
unset($this->observers[$event][$i]);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public function route(
string $pattern,
callable $callback,
int $priority = 0
): RouterInterface
{
): RouterInterface {
//hard requirement
if (!is_callable($callback)) {
throw HttpException::forInvalidRouteCallback();
Expand Down

0 comments on commit 480fdef

Please sign in to comment.