Skip to content

Commit 4447a88

Browse files
committed
Merge pull request #40 from skipperbent/development
[FEATURE] Features
2 parents 4d45e34 + 8efec07 commit 4447a88

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

README.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use Pecee\SimpleRouter\SimpleRouter;
7272

7373
SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() {
7474

75-
SimpleRouter::group(['prefix' => 'services'], function() {
75+
SimpleRouter::group(['prefix' => '/services', 'exceptionHandler' => '\MyProject\Handler\CustomExceptionHandler'], function() {
7676

7777
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')->where(['id' => '[0-9]+');
7878

@@ -131,47 +131,57 @@ The framework has it's own ```Router``` class which inherits from the ```SimpleR
131131
namespace MyProject;
132132

133133
use Pecee\Handler\ExceptionHandler;
134+
use Pecee\SimpleRouter\RouterBase;
134135
use Pecee\SimpleRouter\SimpleRouter;
135136

136137
class Router extends SimpleRouter {
137138

138-
protected static $exceptionHandlers = array();
139-
140-
public static function start() {
139+
protected static $defaultExceptionHandler;
141140

142-
Debug::getInstance()->add('Router initialised.');
141+
public static function start($defaultNamespace = null) {
143142

144143
// Load routes.php
145-
$file = $_ENV['basePath'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
144+
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
146145
if(file_exists($file)) {
147146
require_once $file;
148147
}
149148

150-
// Init locale settings
151-
Locale::getInstance();
152-
153-
// Set default namespace for routes
154-
$defaultNamespace = '\\'.Registry::getInstance()->get('AppName') . '\\Controller';
155-
156-
// Add custom csrf verifier (must extend BaseCsrfVerifier)
157-
parent::csrfVerifier('MyProject\Middleware\CustomCsrfVerifier');
149+
// Set default namespace
150+
$defaultNamespace = '\\'.$_ENV['app_name'] . '\\Controller';
158151

159152
// Handle exceptions
160153
try {
161154
parent::start($defaultNamespace);
162155
} catch(\Exception $e) {
163-
/* @var $handler ExceptionHandler */
164-
foreach(self::$exceptionHandlers as $handler) {
165-
$class = new $handler();
166-
$class->handleError($e);
156+
157+
$route = RouterBase::getInstance()->getLoadedRoute();
158+
159+
$exceptionHandler = null;
160+
161+
// Load and use exception-handler defined on group
162+
163+
if($route && $route->getGroup()) {
164+
$exceptionHandler = $route->getGroup()->getExceptionHandler();
165+
166+
if($exceptionHandler !== null) {
167+
$class = new $exceptionHandler();
168+
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
169+
}
170+
}
171+
172+
// Otherwise use the fallback default exceptions handler
173+
174+
if(self::$defaultExceptionHandler !== null) {
175+
$class = new self::$defaultExceptionHandler();
176+
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
167177
}
168178

169179
throw $e;
170180
}
171181
}
172182

173-
public static function addExceptionHandler($handler) {
174-
self::$exceptionHandlers[] = $handler;
183+
public static function setDefaultExceptionHandler($handler) {
184+
self::$defaultExceptionHandler = $handler;
175185
}
176186

177187
}

src/Pecee/SimpleRouter/RouterBase.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ public function addRoute(RouterEntry $route) {
3939
}
4040
}
4141

42-
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false) {
42+
protected function processRoutes(array $routes, array $settings = array(), array $prefixes = array(), $backstack = false, $group = null) {
4343
// Loop through each route-request
4444

45+
$activeGroup = null;
46+
4547
/* @var $route RouterEntry */
4648
foreach($routes as $route) {
4749

50+
$route->setGroup($group);
51+
4852
if($this->defaultNamespace && !$route->getNamespace()) {
4953
$namespace = null;
5054
if ($route->getNamespace()) {
@@ -75,6 +79,7 @@ protected function processRoutes(array $routes, array $settings = array(), array
7579
$this->currentRoute = $route;
7680
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
7781
$route->renderRoute($this->request);
82+
$activeGroup = $route;
7883
}
7984
$this->currentRoute = null;
8085

@@ -83,7 +88,7 @@ protected function processRoutes(array $routes, array $settings = array(), array
8388
$this->backstack = array();
8489

8590
// Route any routes added to the backstack
86-
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true);
91+
$this->processRoutes($backstack, $mergedSettings, $newPrefixes, true, $activeGroup);
8792
}
8893
}
8994
}

src/Pecee/SimpleRouter/RouterEntry.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ public function getRequestMethods() {
317317
return $this->settings['requestMethods'];
318318
}
319319

320+
public function getGroup() {
321+
return $this->group;
322+
}
323+
324+
public function setGroup($group) {
325+
$this->group = $group;
326+
return $this;
327+
}
328+
320329
abstract function matchRoute(Request $request);
321330

322331
}

src/Pecee/SimpleRouter/RouterGroup.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ public function matchRoute(Request $request) {
3333
return null;
3434
}
3535

36+
public function setExceptionHandler($class) {
37+
$this->exceptionHandler = $class;
38+
return $this;
39+
}
40+
41+
public function getExceptionHandler() {
42+
return $this->exceptionHandler;
43+
}
44+
3645
}

src/Pecee/SimpleRouter/RouterRoute.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class RouterRoute extends RouterEntry {
88

9-
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?\?{0,1})}';
9+
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)\?{0,1}}';
1010

1111
protected $url;
1212

@@ -60,12 +60,12 @@ public function matchRoute(Request $request) {
6060

6161
$isParameter = true;
6262
} elseif($isParameter && $character === '}') {
63-
$required = false;
63+
$required = true;
6464
// Check for optional parameter
6565
if($lastCharacter === '?') {
6666
$parameter = substr($parameter, 0, strlen($parameter)-1);
6767
$regex .= '(?:(?:\/{0,1}(?P<'.$parameter.'>[a-z0-9]*?)){0,1}\\/)';
68-
$required = true;
68+
$required = false;
6969
} else {
7070
// Use custom parameter regex if it exists
7171
$parameterRegex = '[a-z0-9]*?';
@@ -130,12 +130,13 @@ public function getUrl() {
130130
public function setUrl($url) {
131131

132132
$parameters = array();
133+
$matches = array();
133134

134-
preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $parameters);
135-
136-
$parameters = $parameters[1];
135+
if(preg_match_all('/'.self::PARAMETERS_REGEX_MATCH.'/is', $url, $matches)) {
136+
$parameters = $matches[1];
137+
}
137138

138-
if($parameters !== null) {
139+
if(count($parameters)) {
139140
foreach($parameters as $param) {
140141
$this->parameters[$param] = '';
141142
}

0 commit comments

Comments
 (0)