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

Url rewrites #373

Open
wants to merge 16 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
1 change: 1 addition & 0 deletions code/site/components/com_pages/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected function _initialize(KObjectConfig $config)

'collections' => array(),
'redirects' => array(),
'rewrites' => array(),
'page' => array(),
'sites' => array('[*]' => JPATH_ROOT.'/joomlatools-pages'),
'headers' => array(),
Expand Down
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 @@ -91,7 +91,7 @@ public function getRoute()
$path = trim(str_replace(array($base, '/index.php'), '', $url), '/');
$query = $this->getRequest()->getUrl()->getQuery(true);

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

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;
}
}
104 changes: 68 additions & 36 deletions code/site/components/com_pages/dispatcher/router/resolver/regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,20 @@ protected function _initialize(KObjectConfig $config)
$config->append(array(
'routes' => array(),
'types' => [
'email' => '\S+@\S+',
'month' => '(0?[1-9]|1[012])',
'year' => '(19|20)\d{2}',
'digit' => '[0-9]++',
'email' => '\S+@\S+',
'month' => '(0?[1-9]|1[012])',
'year' => '(19|20)\d{2}',
'digit' => '[0-9]++',
'*digit' => '[0-9]+(,[0-9]+)*',
'alnum' => '[0-9A-Za-z]++',
'alnum' => '[0-9A-Za-z]++',
'*alnum' => '[0-9A-Za-z]+(,[0-9A-Za-z]+)*',
'alpha' => '[A-Za-z]++',
'alpha' => '[A-Za-z]++',
'*alpha' => '[A-Za-z]+(,[A-Za-z]+)*',
'*' => '.+?',
'**' => '.++',
'' => '[^/\.]++',
'id' => '[0-9]++[-]++[\S]++',
'slug' => '(?![0-9]++-)[^-][\S]++',
'*' => '.+?',
'**' => '.++',
'' => '[^/\.]++',
],
));

Expand All @@ -90,18 +92,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 +120,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 +170,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 +196,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']))
{
//Generate the dynamic route
if($this->_buildRoute($regex, $route)) {
//Parse the route to match it
if($this->_parseRoute($regex, $route) && (bool) call_user_func($target['generate'], $route) == true) {
$generated = true; break;
}
}
else
{
//Parse the route to match it
if($this->_parseRoute($regex, $route) && $this->_buildRoute($target, $route)) {
$generated = true; break;
}
}
Expand All @@ -208,12 +219,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 @@ -321,11 +343,21 @@ protected function _buildRoute($regex, ComPagesDispatcherRouterRouteInterface $r
if(isset($route->query[$param]))
{
if(is_array($route->query[$param])) {
$value= implode(',', $route->query[$param]);
$value = implode(',', $route->query[$param]);
} else {
$value = $route->query[$param];
}

if ($type && isset($this->_match_types[$type]))
{
$type = $this->_match_types[$type];

//Get first capturing group if it exists, if not use full match
if(preg_match('/'.$type.'/', $value, $matches)) {
$value = $matches[0] ?? $value;
}
}

//Part is found, replace for param value
$regex = str_replace($block, $value, $regex);

Expand All @@ -338,7 +370,7 @@ protected function _buildRoute($regex, ComPagesDispatcherRouterRouteInterface $r
if($optional) {
$regex = str_replace($pre . $block, '', $regex);
} else {
$result = false; break;
$result = false; break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,21 @@ public function isAbsolute()
{
return (bool) ($this->scheme && $this->host);
}

/**
* Generate debug info
*
* @return array
*/
public function __debugInfo()
{
$result = [
'route' => $this->toString(),
'state' => $this->getState(),
'format' => $this->getFormat(),
'status' => $this->isResolved() ? 'resolved' : $this->isGenerated() ? 'generated' : ''
];

return $result;
}
}
13 changes: 13 additions & 0 deletions code/site/components/com_pages/dispatcher/router/route/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ public function setGenerated()

return $this;
}

/**
* Generate debug info
*
* @return array
*/
public function __debugInfo()
{
$result = parent::__debugInfo();
$result['page'] = $this->_page_path;

return $result;
}
}
Loading