diff --git a/core/components/fastrouter/elements/plugins/plugin.fastrouter.php b/core/components/fastrouter/elements/plugins/plugin.fastrouter.php index bfb0c92..55edd57 100644 --- a/core/components/fastrouter/elements/plugins/plugin.fastrouter.php +++ b/core/components/fastrouter/elements/plugins/plugin.fastrouter.php @@ -1,10 +1,11 @@ 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(); } diff --git a/core/components/fastrouter/elements/snippets/snippet.fastrouter.php b/core/components/fastrouter/elements/snippets/snippet.fastrouter.php index 3020521..9d4ec93 100644 --- a/core/components/fastrouter/elements/snippets/snippet.fastrouter.php +++ b/core/components/fastrouter/elements/snippets/snippet.fastrouter.php @@ -1,4 +1,5 @@ getOption('fastrouter.paramsKey', null, 'fastrouter'); $params = $modx->getOption($key, $scriptProperties, isset($_REQUEST[$key]) ? $_REQUEST[$key] : array()); diff --git a/core/components/fastrouter/fastrouter.class.php b/core/components/fastrouter/fastrouter.class.php index b81f2c3..156ea68 100644 --- a/core/components/fastrouter/fastrouter.class.php +++ b/core/components/fastrouter/fastrouter.class.php @@ -2,6 +2,9 @@ require_once __DIR__ . '/vendor/nikic/fast-route/src/bootstrap.php'; +/** + * Class FastRouter + */ class FastRouter { /** * Path to routes cache file @@ -9,39 +12,43 @@ class FastRouter { * @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() { @@ -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'); @@ -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() { @@ -105,6 +134,8 @@ public function dispatch() { } /** + * Handle route + * * @param mixed $routeHandler * @param array $data * @@ -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; } @@ -127,6 +158,8 @@ protected function handle($routeHandler, array $data) { } /** + * Send error page + * * @return null */ protected function error() { @@ -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, '
The page you requested was not found.
') + 'error_message' => $this->modx->getOption('error_page_message', null, 'The page you requested was not found.
'), ); $this->modx->sendForward($this->modx->getOption('error_page', $options, '404'), $options); @@ -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(); + } }