Skip to content

Commit

Permalink
Added routes caching feature. So, Routes can now be cached.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Jan 10, 2019
1 parent 298537b commit cc75420
Showing 1 changed file with 87 additions and 12 deletions.
99 changes: 87 additions & 12 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Closure;
use ReflectionMethod;
use Exception;
use Buki\Router\RouterRequest;
use Buki\Router\RouterCommand;
use Buki\Router\RouterException;
Expand Down Expand Up @@ -68,6 +69,16 @@ class Router
*/
protected $mainMethod = 'main';

/**
* @var string $mainMethod Cache file
*/
protected $cacheFile = null;

/**
* @var bool $cacheLoaded Cache is loaded?
*/
protected $cacheLoaded = false;

/**
* @var Closure $errorCallback Route error callback function
*/
Expand All @@ -93,6 +104,7 @@ function __construct(array $params = [])
}

$this->setPaths($params);
$this->loadCache();
}

/**
Expand Down Expand Up @@ -135,6 +147,12 @@ protected function setPaths($params)
if (isset($params['main_method'])) {
$this->mainMethod = $params['main_method'];
}

if (isset($params['cache'])) {
$this->cacheFile = $params['cache'];
} else {
$this->cacheFile = realpath(__DIR__ . '/../cache.php');
}
}

/**
Expand All @@ -144,13 +162,17 @@ protected function setPaths($params)
* @param $method
* @param $params
*
* @return void
* @return mixed
* @throws
*/
public function __call($method, $params)
{
if($this->cacheLoaded) {
return true;
}

if (is_null($params)) {
return;
return false;
}

if (! in_array(strtoupper($method), explode('|', RouterRequest::$validMethods)) ) {
Expand Down Expand Up @@ -190,7 +212,8 @@ public function __call($method, $params)
} else {
$this->addRoute($route, $method, $callback, $settings);
}
return;

return true;
}

/**
Expand All @@ -201,10 +224,14 @@ public function __call($method, $params)
* @param array|string|closure $settings
* @param string|closure $callback
*
* @return void
* @return bool
*/
public function add($methods, $route, $settings, $callback = null)
{
if($this->cacheLoaded) {
return true;
}

if (is_null($callback)) {
$callback = $settings;
$settings = null;
Expand All @@ -220,7 +247,7 @@ public function add($methods, $route, $settings, $callback = null)
call_user_func_array([$this, strtolower($methods)], [$route, $settings, $callback]);
}

return;
return true;
}

/**
Expand All @@ -229,7 +256,7 @@ public function add($methods, $route, $settings, $callback = null)
* @param string|array $pattern
* @param null|string $attr
*
* @return void
* @return mixed
* @throws
*/
public function pattern($pattern, $attr = null)
Expand All @@ -250,7 +277,7 @@ public function pattern($pattern, $attr = null)
}
}

return;
return true;
}

/**
Expand Down Expand Up @@ -354,10 +381,14 @@ public function run()
* @param closure|array $settings
* @param null|closure $callback
*
* @return void
* @return bool
*/
public function group($name, $settings = null, $callback = null)
{
if($this->cacheLoaded) {
return true;
}

$groupName = trim($name, '/');
$group = [];
$group['route'] = '/' . $groupName;
Expand Down Expand Up @@ -406,6 +437,8 @@ public function group($name, $settings = null, $callback = null)
}

$this->endGroup();

return true;
}

/**
Expand All @@ -415,11 +448,15 @@ public function group($name, $settings = null, $callback = null)
* @param string|array $settings
* @param null|string $controller
*
* @return void
* @return mixed
* @throws
*/
public function controller($route, $settings, $controller = null)
{
if($this->cacheLoaded) {
return true;
}

if (is_null($controller)) {
$controller = $settings;
$settings = [];
Expand All @@ -435,7 +472,7 @@ public function controller($route, $settings, $controller = null)
}

if (! class_exists($controller)) {
require($controllerFile);
require $controllerFile;
}

$controller = str_replace('/', '\\', $controller);
Expand Down Expand Up @@ -467,7 +504,7 @@ public function controller($route, $settings, $controller = null)
unset($r);
}

return;
return true;
}

/**
Expand Down Expand Up @@ -606,7 +643,7 @@ public function getList()
echo '<pre style="font-size:15px;">';
var_dump($this->getRoutes());
echo '</pre>';
die();
die;
}

/**
Expand Down Expand Up @@ -641,4 +678,42 @@ public function routerCommand()
{
return RouterCommand::getInstance();
}

/**
* Cache all routes
*
* @return bool
* @throws Exception
*/
public function cache()
{
foreach($this->getRoutes() as $key => $r) {
if (!is_string($r['callback'])) {
throw new Exception(sprintf('Routes cannot contain a Closure/Function callback while caching.'));
}
}

$cacheContent = '<?php return '.var_export($this->getRoutes(), true).';' . PHP_EOL;
if (false === file_put_contents($this->cacheFile, $cacheContent)) {
throw new Exception(sprintf('Routes cache file could not be written.'));
}

return true;
}

/**
* Load Cache file
*
* @return bool
*/
protected function loadCache()
{
if (file_exists($this->cacheFile)) {
$this->routes = require $this->cacheFile;
$this->cacheLoaded = true;
return true;
}

return false;
}
}

0 comments on commit cc75420

Please sign in to comment.