Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Optional dependencies moved to Wither Method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Dec 20, 2018
1 parent 62ecff0 commit 3c4145d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Selami\Router;

use FastRoute;
use RuntimeException;
use Selami\Router\Exceptions\InvalidCacheFileException;

class Dispatcher
{
Expand Down Expand Up @@ -78,7 +78,7 @@ private function createCachedRoute($routeCollector) : void
* @var FastRoute\RouteCollector $routeCollector
*/
$dispatchData = $routeCollector->getData();
file_put_contents($this->cachedFile, '<?php return ' . var_export($dispatchData, true) . ';');
file_put_contents($this->cachedFile, '<?php return ' . var_export($dispatchData, true) . ';', LOCK_EX);
}
}

Expand All @@ -87,7 +87,7 @@ private function cachedDispatcher() : FastRoute\Dispatcher\GroupCountBased
{
$dispatchData = include $this->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);
}
Expand Down
20 changes: 15 additions & 5 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down
21 changes: 11 additions & 10 deletions test/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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(
Expand All @@ -100,18 +101,18 @@ public function shouldCacheRoutesSuccessfully() : void
}
/**
* @test
* @expectedException \RuntimeException
* @expectedException \Selami\Router\Exceptions\InvalidCacheFileException
*/
public function shouldThrowExceptionForInvalidCachedFile() : void
{
file_put_contents('/tmp/failed.cache', '');
$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(
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3c4145d

Please sign in to comment.