Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve redirect routing #348

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function _beforeRender(KControllerContextInterface $context)
{
$segments[] = $segment;

if($route = $router->generate('pages:'.implode('/', $segments)))
if($route = $router->generate('page:'.implode('/', $segments)))
{
$page = $route->getPage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@ protected function _initialize(KObjectConfig $config)
parent::_initialize($config);
}

protected function _beforeDispatch(KDispatcherContextInterface $context)
{
$router = $this->getObject('com://site/pages.dispatcher.router.redirect', ['request' => $context->request]);

if(false !== $route = $router->resolve())
{
if($route->toString(KHttpUrl::AUTHORITY))
{
//External redierct: 301 permanent
$status = KHttpResponse::MOVED_PERMANENTLY;
}
else
{
//Internal redirect: 307 temporary
$status = KHttpResponse::TEMPORARY_REDIRECT;
}

//Qualify the route
$url = $router->qualify($route);

//Set the location header
$context->getResponse()->getHeaders()->set('Location', $url);
$context->getResponse()->setStatus($status);

$context->getSubject()->send();
}
}

protected function _beforeSend(KDispatcherContextInterface $context)
{
$response = $context->response;
Expand Down
2 changes: 1 addition & 1 deletion code/site/components/com_pages/dispatcher/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getRoute()
$url = urldecode($this->getRequest()->getUrl()->getPath());
$path = trim(str_replace(array($base, '/index.php'), '', $url), '/');

$this->__route = $this->getRouter()->resolve('pages:'.$path, $this->getRequest()->query->toArray());
$this->__route = $this->getRouter()->resolve('page:'.$path, $this->getRequest()->query->toArray());
}

if(is_object($this->__route)) {
Expand Down
2 changes: 1 addition & 1 deletion code/site/components/com_pages/dispatcher/router/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function _initialize(KObjectConfig $config)
public function getRoute($route, array $parameters = array())
{
if($route instanceof ComPagesModelEntityPage) {
$route = 'pages:'.$route->path;
$route = 'page:'.$route->path;
}

return parent::getRoute($route, $parameters);
Expand Down
17 changes: 12 additions & 5 deletions code/site/components/com_pages/dispatcher/router/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ protected function _initialize(KObjectConfig $config)

public function resolve($route = null, array $parameters = array())
{
if(!$route)
$result = false;
if(count($this->getConfig()->routes))
{
$base = $this->getRequest()->getBasePath();
$url = urldecode( $this->getRequest()->getUrl()->getPath());
if(!$route)
{
$base = $this->getRequest()->getBasePath();
$url = urldecode( $this->getRequest()->getUrl()->getPath());
$parameters = $this->getRequest()->getUrl()->getQuery(true);

$route = trim(str_replace(array($base, '/index.php'), '', $url), '/');
$route = trim(str_replace(array($base, '/index.php'), '', $url), '/');
}

$result = parent::resolve($route, $parameters);
}

return parent::resolve($route, $parameters);
return $result;
}
}
72 changes: 46 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(isset($result['resolve']) && is_callable($result['resolve'])) {
$result = (bool) call_user_func($result['resolve'], $route);
} else {
$result = $this->_buildRoute($result, $route);
}
}

return $result !== false ? parent::resolve($route) : false;
Expand All @@ -194,12 +194,21 @@ 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(isset($target['generate']) && is_callable($target['generate']))
{
//Parse the route to match it
if($this->_parseRoute($regex, $route) && (bool) call_user_func($target['generate'], $route) == true) {
$generated = true; break;
}
}
else
{
//Generate the dynamic route
if($this->_buildRoute($regex, $route)) {
//Parse the route to match it
if($this->_parseRoute($regex, $route) && $this->_buildRoute($target, $route)) {
$generated = true; break;
}
}
Expand All @@ -208,12 +217,23 @@ 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(isset($target['generate']) && is_callable($target['generate']))
{
//Compare the path to match it
if($regex == $path && (bool) call_user_func($target['generate'], $route) == true) {
$generated = true; break;
}
}
else
{
//Compare the path to match it
if($target == $path && $this->_buildRoute($regex, $route)) {
$generated = true; break;
}
}
}
}
Expand Down Expand Up @@ -338,7 +358,7 @@ protected function _buildRoute($regex, ComPagesDispatcherRouterRouteInterface $r
if($optional) {
$regex = str_replace($pre . $block, '', $regex);
} else {
$result = false; break;
$result = false; break;
}
}
}
Expand Down
91 changes: 71 additions & 20 deletions code/site/components/com_pages/dispatcher/router/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public function __construct(KObjectConfig $config)

//Add a global object alias
$this->getObject('manager')->registerAlias($this->getIdentifier(), 'router');

$this->__routers = KObjectConfig::unbox($config->routers);
}

/**
* Initializes the options for the object
*
* Called from {@link __construct()} as a first step of object instantiation.
*
* @param KObjectConfig $config An optional ObjectConfig object with configuration options.
* @return void
*/
protected function _initialize(KObjectConfig $config)
{
$config->append(array(
'routers' => [
'page' => 'com://site/pages.dispatcher.router.pages',
'site' => 'com://site/pages.dispatcher.router.site',
'redirect' => 'com://site/pages.dispatcher.router.redirect',
],
));

parent::_initialize($config);
}

/**
Expand All @@ -43,24 +66,38 @@ public function __construct(KObjectConfig $config)
*/
public function resolve($route, array $parameters = array())
{
$result = false;
$identifier = null;

//Find router package
//Find router identifier
if($route instanceof KObjectInterface)
{
if($route instanceof ComPagesDispatcherRouterRouteInterface) {
if($route instanceof ComPagesDispatcherRouterRouteInterface)
{
$package = $route->getScheme();
} else {
$package = $route->getIdentifier()->getPackage();

if(isset($this->__routers[$package])) {
$identifier = $this->__routers[$package];
}
}
else $package = $route->getIdentifier()->getPackage();
}
else
{
$package = parse_url($route, PHP_URL_SCHEME);

if(isset($this->__routers[$package])) {
$identifier = $this->__routers[$package];
}
}
else $package = parse_url($route, PHP_URL_SCHEME);

//Identifier Fallback
if(!$identifier) {
$identifier = 'com://site/' . $package . '.dispatcher.router.' . $package;
}

//Get router instance
if(!isset($this->__routers[$package]))
if(is_string($identifier))
{
$identifier = 'com://site/'.$package.'.dispatcher.router.'.$package;

$config = [
'request' => $this->getRequest(),
'resolvers' => $this->getResolvers()
Expand All @@ -70,7 +107,7 @@ public function resolve($route, array $parameters = array())

$this->__routers[$package] = $router;
}
else $router = $this->__routers[$package];
else $router = $identifier;

return $router->resolve($route, $parameters);
}
Expand All @@ -86,24 +123,38 @@ public function resolve($route, array $parameters = array())
*/
public function generate($route, array $parameters = array())
{
$result = false;
$identifier = null;

//Find router package
//Find router identifier
if($route instanceof KObjectInterface)
{
if($route instanceof ComPagesDispatcherRouterRouteInterface) {
if($route instanceof ComPagesDispatcherRouterRouteInterface)
{
$package = $route->getScheme();
} else {
$package = $route->getIdentifier()->getPackage();

if(isset($this->__routers[$package])) {
$identifier = $this->__routers[$package];
}
}
else $package = $route->getIdentifier()->getPackage();
}
else
{
$package = parse_url($route, PHP_URL_SCHEME);

if(isset($this->__routers[$package])) {
$identifier = $this->__routers[$package];
}
}
else $package = parse_url($route, PHP_URL_SCHEME);

//Identifier Fallback
if(!$identifier) {
$identifier = 'com://site/' . $package . '.dispatcher.router.' . $package;
}

//Get router instance
if(!isset($this->__routers[$package]))
if(is_string($identifier))
{
$identifier = 'com://site/'.$package.'.dispatcher.router.'.$package;

$config = [
'request' => $this->getRequest(),
'resolvers' => $this->getResolvers()
Expand All @@ -113,7 +164,7 @@ public function generate($route, array $parameters = array())

$this->__routers[$package] = $router;
}
else $router = $this->__routers[$package];
else $router = $identifier;

return $router->generate($route, $parameters);
}
Expand Down
Loading