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

Serve static files #346

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
60 changes: 60 additions & 0 deletions code/site/components/com_pages/dispatcher/router/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Joomlatools Pages
*
* @copyright Copyright (C) 2018 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository
*/

class ComPagesDispatcherRouterFile extends ComPagesDispatcherRouterAbstract
{
protected function _initialize(KObjectConfig $config)
{
$config->append([
'route' => 'file',
'routes' => []
])->append([
'resolvers' => [
'regex' => ['routes' => $config->routes]
]
]);

parent::_initialize($config);
}

public function resolve($route = null, array $parameters = array())
{
$result = false;
if (count($this->getConfig()->routes))
{
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), '/');
}

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

return $result;
}

public function qualify(ComPagesDispatcherRouterRouteInterface $route, $replace = false)
{
$url = clone $route;

if(!$url->isAbsolute())
{
$base = $this->getRequest()->getBasePath(true);
$path = trim($url->getPath(), '/');

$url->setPath($base . '/' . $path);
}

return $url;
}
}
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;
}
}
78 changes: 49 additions & 29 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']))
{
//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 +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 All @@ -353,10 +373,10 @@ protected function _buildRoute($regex, ComPagesDispatcherRouterRouteInterface $r
}

if(strpos($regex, '://') === false) {
$route->setPath('/'.ltrim($regex, '/'));
} else {
$route->setUrl($regex);
$regex = '/' . ltrim($regex, '/');
}

$route->setUrl($regex);
}

return $result;
Expand Down
22 changes: 22 additions & 0 deletions code/site/components/com_pages/dispatcher/router/route/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Joomlatools Pages
*
* @copyright Copyright (C) 2018 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository
*/

/**
* Router File Route
*
* @author Johan Janssens <https://github.com/johanjanssens>
* @package Koowa\Library\Dispatcher\Router\Route
*/
class ComPagesDispatcherRouterRouteFile extends ComPagesDispatcherRouterRouteAbstract
{
public function isAbsolute()
{
return file_exists($this->getPath());
}
}
Loading