From 3c4145d85e4143f5e250239b4631f89b40ed0e25 Mon Sep 17 00:00:00 2001 From: Mehmet Korkmaz Date: Thu, 20 Dec 2018 03:02:39 +0300 Subject: [PATCH] Optional dependencies moved to Wither Method --- src/Dispatcher.php | 6 +++--- src/Router.php | 20 +++++++++++++++----- test/RouterTest.php | 21 +++++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 0a27172..72734c3 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -4,7 +4,7 @@ namespace Selami\Router; use FastRoute; -use RuntimeException; +use Selami\Router\Exceptions\InvalidCacheFileException; class Dispatcher { @@ -78,7 +78,7 @@ private function createCachedRoute($routeCollector) : void * @var FastRoute\RouteCollector $routeCollector */ $dispatchData = $routeCollector->getData(); - file_put_contents($this->cachedFile, 'cachedFile, 'cachedFile; if (!is_array($dispatchData)) { - throw new RuntimeException('Invalid cache file "' . $this->cachedFile . '"'); + throw new InvalidCacheFileException('Invalid cache file "' . $this->cachedFile . '"'); } return new FastRoute\Dispatcher\GroupCountBased($dispatchData); } diff --git a/src/Router.php b/src/Router.php index 4c84a6b..4365c4f 100644 --- a/src/Router.php +++ b/src/Router.php @@ -107,18 +107,28 @@ final class Router public function __construct( int $defaultReturnType, string $method, - string $requestedPath, - string $folder = '', - ?string $cachedFile = null + string $requestedPath ) { if (!in_array($method, self::$validRequestMethods, true)) { $message = sprintf('%s is not valid Http request method.', $method); throw new InvalidRequestMethodException($message); } $this->method = $method; - $this->requestedPath = $this->extractFolder($requestedPath, $folder); + $this->requestedPath = $requestedPath; $this->defaultReturnType = ($defaultReturnType >=1 && $defaultReturnType <=7) ? $defaultReturnType : self::HTML; - $this->cachedFile = $cachedFile; + } + + public function withSubFolder(string $folder) : self + { + $new = clone $this; + $new->requestedPath = $this->extractFolder($this->requestedPath, $folder); + return $new; + } + public function withCacheFile(string $fileName) : self + { + $new = clone $this; + $new->cachedFile = $fileName; + return $new; } /* diff --git a/test/RouterTest.php b/test/RouterTest.php index f01e168..ca5636a 100644 --- a/test/RouterTest.php +++ b/test/RouterTest.php @@ -22,7 +22,7 @@ public function setUp() : void $basedir = dirname(__DIR__) . '/app'; $this->config['base_dir'] = $basedir; $this->config['app_dir'] = $basedir; - $this->config['cache_file'] = '/tmp/fastroute.cache'; + $this->config['cache_file'] = tempnam(sys_get_temp_dir(), 'FRT'); $_SERVER = []; $_FILES = []; $_GET = []; @@ -87,10 +87,11 @@ public function shouldCacheRoutesSuccessfully() : void $router = new Router( $this->config['default_return_type'], $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'], - $this->config['cache_file'] + $this->request->getUri()->getPath() ); + $router = $router->withSubFolder($this->config['folder']) + ->withSubFolder($this->config['cache_file']); + $router->add(Router::GET, '/', 'app/main', Router::HTML, 'home'); $router->getRoute(); $this->assertFileExists( @@ -100,7 +101,7 @@ public function shouldCacheRoutesSuccessfully() : void } /** * @test - * @expectedException \RuntimeException + * @expectedException \Selami\Router\Exceptions\InvalidCacheFileException */ public function shouldThrowExceptionForInvalidCachedFile() : void { @@ -108,10 +109,10 @@ public function shouldThrowExceptionForInvalidCachedFile() : void $router = new Router( $this->config['default_return_type'], $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'], - '/tmp/failed.cache' + $this->request->getUri()->getPath() ); + $router = $router->withSubFolder($this->config['folder']) + ->withCacheFile('/tmp/failed.cache'); $router->add(Router::GET, '/', 'app/main', Router::HTML, 'home'); $router->getRoute(); $this->assertFileExists( @@ -161,9 +162,9 @@ public function shouldCorrectlyInstantiateRouter() : void $router = new Router( $this->config['default_return_type'], $this->request->getMethod(), - $this->request->getUri()->getPath(), - $this->config['folder'] + $this->request->getUri()->getPath() ); + $router = $router->withSubFolder($this->config['folder']); $router->add(Router::GET, '/', 'app/main', null, 'home'); $router->add(Router::GET, '/json', 'app/json', Router::JSON); $router->add(Router::POST, '/json', 'app/redirect', Router::REDIRECT);