Development

- Added new event when adding route.
- Added `prependUrl` method to `LoadableRoute` class.
- Added unit-test for add-route event.
- Updated documentation to reflect new changes.
This commit is contained in:
Simon Sessingø
2018-04-02 14:53:36 +02:00
parent 17a7b28e82
commit 30a2ddeed9
7 changed files with 245 additions and 160 deletions
@@ -22,6 +22,11 @@ class EventHandler implements IEventHandler
*/
public const EVENT_LOAD = 'onLoad';
/**
* Fires when route is added to the router
*/
public const EVENT_ADD_ROUTE = 'onAddRoute';
/**
* Fires when a url-rewrite is and just before the routes are re-initialized.
*/
@@ -95,6 +100,7 @@ class EventHandler implements IEventHandler
self::EVENT_ALL,
self::EVENT_INIT,
self::EVENT_LOAD,
self::EVENT_ADD_ROUTE,
self::EVENT_REWRITE,
self::EVENT_BOOT,
self::EVENT_RENDER_BOOTMANAGER,
@@ -39,6 +39,13 @@ interface ILoadableRoute extends IRoute
*/
public function setUrl(string $url): self;
/**
* Prepend url
* @param string $url
* @return ILoadableRoute
*/
public function prependUrl(string $url): self;
/**
* Returns the provided name for the router.
*
+12 -1
View File
@@ -85,6 +85,17 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
return $this;
}
/**
* Prepend url
*
* @param string $url
* @return ILoadableRoute
*/
public function prependUrl(string $url): ILoadableRoute
{
return $this->setUrl(rtrim($url, '/') . $this->url);
}
public function getUrl(): string
{
return $this->url;
@@ -240,7 +251,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
}
if (isset($values['prefix']) === true) {
$this->setUrl($values['prefix'] . $this->getUrl());
$this->prependUrl($values['prefix']);
}
parent::setSettings($values, $merge);
+6 -4
View File
@@ -151,18 +151,20 @@ class Router
*/
public function addRoute(IRoute $route): IRoute
{
$this->fireEvents(EventHandler::EVENT_ADD_ROUTE, [
'route' => $route,
]);
/*
* If a route is currently being processed, that means that the route being added are rendered from the parent
* routes callback, so we add them to the stack instead.
*/
if ($this->isProcessingRoute === true) {
$this->routeStack[] = $route;
return $route;
} else {
$this->routes[] = $route;
}
$this->routes[] = $route;
return $route;
}