Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vanchelo committed Oct 24, 2015
1 parent 4df69ba commit 0ea8bee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

require_once MODX_CORE_PATH . 'components/fastrouter/fastrouter.class.php';

$router = new FastRouter($modx);

if ($modx->event->name == 'OnPageNotFound' && !isset($modx->event->params['stop'])) {
if ($router->needDispath()) {
$router->dispatch();
} else if ($modx->event->name == 'OnChunkSave' && $chunk->name == 'fastrouter') {
} elseif ($router->isRoutesChunkUpdated($chunk->name)) {
$router->clearCache();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

$key = $modx->getOption('fastrouter.paramsKey', null, 'fastrouter');
$params = $modx->getOption($key, $scriptProperties, isset($_REQUEST[$key]) ? $_REQUEST[$key] : array());

Expand Down
90 changes: 75 additions & 15 deletions core/components/fastrouter/fastrouter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,53 @@

require_once __DIR__ . '/vendor/nikic/fast-route/src/bootstrap.php';

/**
* Class FastRouter
*/
class FastRouter {
/**
* Path to routes cache file
*
* @var string
*/
protected $cacheFile;

/**
* @var modX
*/
protected $modx;

/**
* @var FastRoute\Dispatcher\GroupCountBased
*/
protected $dispatcher;

/**
* @var string
*/
protected $paramsKey;

/**
* FastRouter constructor.
*
* @param modX $modx
*/
function __construct(modX $modx) {
$this->modx = $modx;
$this->cacheFile = $modx->getOption(xPDO::OPT_CACHE_PATH) . 'fastrouter.cache.php';
$this->paramsKey = $modx->getOption('fastrouter.paramsKey', null, 'fastrouter');
$this->registerDispatcher();
}

/**
* @return mixed
* Get request method
*
* @return string
*/
protected function getMethod() {
return $_SERVER['REQUEST_METHOD'];
return strtoupper($_SERVER['REQUEST_METHOD']);
}

/**
* Get request URI
*
* @return string
*/
protected function getUri() {
Expand All @@ -53,23 +60,21 @@ protected function getUri() {
}

/**
* Get routes dispatcher
*
* @return FastRoute\Dispatcher|FastRoute\Dispatcher\GroupCountBased
*/
protected function getDispatcher() {
if (!isset($this->dispatcher)) {
$this->dispatcher = FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $router) {
$this->getRoutes($router);
}, array('cacheFile' => $this->cacheFile));
}

return $this->dispatcher;
}

/**
* Register routes
*
* @param FastRoute\RouteCollector $router
*/
protected function getRoutes(FastRoute\RouteCollector $router) {
$routes = json_decode($this->modx->getChunk('fastrouter'), true);
$routes = json_decode($this->getRoutesChunk(), true);

if (!$routes) {
throw new InvalidArgumentException('Invalid routes');
Expand All @@ -83,6 +88,30 @@ protected function getRoutes(FastRoute\RouteCollector $router) {
}

/**
* Get routes chunk content
*
* @return string
*/
protected function getRoutesChunk() {
$chunk = $this->modx->getChunk($this->chunkName());

return $chunk;
}

/**
* Get routes chunk name
*
* @return string
*/
public function chunkName() {
$name = $this->modx->getOption('fastrouter.chunkName', null, 'fastrouter');

return $name;
}

/**
* Dispatch request
*
* @return null
*/
public function dispatch() {
Expand All @@ -105,6 +134,8 @@ public function dispatch() {
}

/**
* Handle route
*
* @param mixed $routeHandler
* @param array $data
*
Expand All @@ -118,7 +149,7 @@ protected function handle($routeHandler, array $data) {
} else {
// Call snippet
echo $this->modx->runSnippet($routeHandler, array(
$this->paramsKey => $data
$this->paramsKey => $data,
));
die;
}
Expand All @@ -127,6 +158,8 @@ protected function handle($routeHandler, array $data) {
}

/**
* Send error page
*
* @return null
*/
protected function error() {
Expand All @@ -135,7 +168,7 @@ protected function error() {
'error_type' => '404',
'error_header' => $this->modx->getOption('error_page_header', null, 'HTTP/1.1 404 Not Found'),
'error_pagetitle' => $this->modx->getOption('error_page_pagetitle', null, 'Error 404: Page not found'),
'error_message' => $this->modx->getOption('error_page_message', null, '<h1>Page not found</h1><p>The page you requested was not found.</p>')
'error_message' => $this->modx->getOption('error_page_message', null, '<h1>Page not found</h1><p>The page you requested was not found.</p>'),
);

$this->modx->sendForward($this->modx->getOption('error_page', $options, '404'), $options);
Expand All @@ -144,11 +177,38 @@ protected function error() {
}

/**
* Remove routes cache file
* Remove routes cache
*/
public function clearCache() {
if (file_exists($this->cacheFile)) {
unlink($this->cacheFile);
}
}

/**
* Register router dispatcher
*/
protected function registerDispatcher() {
$this->dispatcher = FastRoute\cachedDispatcher(function (FastRoute\RouteCollector $router) {
$this->getRoutes($router);
}, array(
'cacheFile' => $this->cacheFile,
));
}

/**
* @return bool
*/
public function needDispath() {
return $this->modx->event->name === 'OnPageNotFound' && !isset($this->modx->event->params['stop']);
}

/**
* Check if chunk with routes updated
*
* @return bool
*/
public function isRoutesChunkUpdated($chunkName) {
return $this->modx->event->name == 'OnChunkSave' && $chunkName == $this->chunkName();
}
}

0 comments on commit 0ea8bee

Please sign in to comment.