Skip to content

Commit

Permalink
#343 - Add support for callback route targets
Browse files Browse the repository at this point in the history
The callback is defined is as follows: 'function($route, $generate = false)'

- $route: a ComPagesDispatcherRouteRouteInterface object
- $generate: are we generating a url or resolving (default false)

Callbacks are both supported for static and dynamic routes, in case of a
dynamic route the callback is called only if the route could be succesfully
resolved.
  • Loading branch information
johanjanssens committed May 15, 2020
1 parent 93a1074 commit 13e4f5f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
70 changes: 44 additions & 26 deletions code/site/components/com_pages/dispatcher/router/resolver/regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,21 @@ protected function _initialize(KObjectConfig $config)
* Add a route for matching
*
* @param string $regex The route regex You can use multiple pre-set regex filters, like [digit:id]
* @param string $path The path this route should point to.
* @param string|callable $target The target this route points to
* @return ComPagesDispatcherRouterResolverInterface
*/
public function addRoute($regex, $path)
public function addRoute($regex, $target)
{
$regex = trim($regex, '/');
$path = rtrim($path, '/');

if(is_string($target)) {
$path = rtrim($target, '/');
}

if(strpos($regex, '[') !== false) {
$this->__dynamic_routes[$regex] = $path;
$this->__dynamic_routes[$regex] = $target;
} else {
$this->__static_routes[$regex] = $path;
$this->__static_routes[$regex] = $target;
}

return $this;
Expand All @@ -115,16 +118,8 @@ public function addRoute($regex, $path)
*/
public function addRoutes($routes)
{
foreach((array)KObjectConfig::unbox($routes) as $path => $routes)
{
foreach((array) $routes as $regex)
{
if (is_numeric($path)) {
$this->addRoute($regex, $regex);
} else {
$this->addRoute($regex, $path);
}
}
foreach((array)KObjectConfig::unbox($routes) as $regex => $target) {
$this->addRoute($regex, $target);
}

return $this;
Expand Down Expand Up @@ -173,8 +168,13 @@ public function resolve(ComPagesDispatcherRouterRouteInterface $route)
$this->__static_routes = array($path => $result) + $this->__static_routes;
}

if($result !== false) {
$this->_buildRoute($result, $route);
if($result !== false)
{
if(is_callable($result)) {
$result = (bool) call_user_func($result, $route, false);
} else {
$result = $this->_buildRoute($result, $route);
}
}

return $result !== false ? parent::resolve($route) : false;
Expand All @@ -194,12 +194,20 @@ public function generate(ComPagesDispatcherRouterRouteInterface $route)
$path = ltrim($route->getPath(), '/');

//Dynamic routes
if($routes = array_keys($this->__dynamic_routes, $path))
$routes = $this->__dynamic_routes;

foreach($routes as $regex => $target)
{
foreach($routes as $regex)
if(is_callable($target))
{
//Parse the route to match it
if($this->_parseRoute($regex, $route) && (bool) call_user_func($target, $route, false) == true) {
$generated = true; break;
}
}
else
{
//Generate the dynamic route
if($this->_buildRoute($regex, $route)) {
if($target == $path && $this->_buildRoute($regex, $route)) {
$generated = true; break;
}
}
Expand All @@ -208,12 +216,22 @@ public function generate(ComPagesDispatcherRouterRouteInterface $route)
//Static routes
if(!$generated)
{
$routes = array_flip(array_reverse($this->__static_routes, true));
$routes = array_reverse($this->__static_routes, true);

if(isset($routes[$path]))
foreach($routes as $regex => $target)
{
if($this->_buildRoute($routes[$path], $route)) {
$generated = true;
if(is_callable($target))
{
//Compare the path to match it
if($regex == $path && (bool) call_user_func($target, $route, false) == true) {
$generated = true; break;
}
}
else
{
if($target == $path && $this->_buildRoute($regex, $route)) {
$generated = true; break;
}
}
}
}
Expand Down Expand Up @@ -338,7 +356,7 @@ protected function _buildRoute($regex, ComPagesDispatcherRouterRouteInterface $r
if($optional) {
$regex = str_replace($pre . $block, '', $regex);
} else {
$result = false; break;
$result = false; break;
}
}
}
Expand Down
29 changes: 24 additions & 5 deletions code/site/components/com_pages/page/registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,29 @@ public function getPageContent($path, $render = false)

public function getRoutes($path = null)
{
if(!is_null($path)) {
$result = $this->__data['routes'][$path];
} else {
$result = $this->__data['routes'];
$result = array();
if(is_null($path))
{
foreach( $this->__data['routes'] as $path => $routes)
{
foreach((array) $routes as $regex)
{
if (is_numeric($path)) {
$result[$regex] = $regex;
} else {
$result[$regex] = $path;
}
}
}
}
else
{
if(isset($this->__data['routes'][$path]))
{
foreach((array) $this->__data['routes'][$path] as $regex) {
$result[$regex] = $path;
}
}
}

return $result;
Expand Down Expand Up @@ -458,7 +477,7 @@ public function loadCache($basedir, $refresh = true)
$result['pages'] = $pages;
$result['routes'] = $routes;
$result['collections'] = $collections;
$result['redirects'] = array_flip($redirects);
$result['redirects'] = $redirects;

//Generate a checksum
$result['hash'] = hash('crc32b', serialize($result));
Expand Down
2 changes: 1 addition & 1 deletion code/site/components/com_pages/resources/config/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'cache_path' => $config['page_cache_path'],
'cache_validation' => $config['page_cache_validation'],
'collections' => $config['collections'],
'redirects' => array_flip($config['redirects']),
'redirects' => $config['redirects'],
'properties' => $config['page'],
],
'data.registry' => [
Expand Down

0 comments on commit 13e4f5f

Please sign in to comment.