Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group specific error pages and prefix error pages #55

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 64 additions & 12 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class Router
*/
protected $groups = [];

/**
* @var array List of error group routes
*/
protected $errorGroupNames = [];

/**
* @var string Error group of requested page
*/
protected $currentErrorGroupName = '/';

/**
* @var array $patterns Pattern definitions for parameters of Route
*/
Expand Down Expand Up @@ -316,15 +326,7 @@ public function run(): void
}

if ($foundRoute === false) {
if (!$this->errorCallback) {
$this->errorCallback = function () {
$this->response()
->setStatusCode(Response::HTTP_NOT_FOUND)
->sendHeaders();
return $this->exception('Looks like page not found or something went wrong. Please try again.');
};
}
call_user_func($this->errorCallback);
$this->notFoundRoute();
}
}

Expand All @@ -350,6 +352,8 @@ public function group(string $prefix, Closure $callback, array $options = []): b

array_push($this->groups, $group);

$this->getErrorGroupNames();

if (is_object($callback)) {
call_user_func_array($callback, [$this]);
}
Expand Down Expand Up @@ -428,13 +432,23 @@ public function controller(string $route, string $controller, array $options = [
/**
* Routes error function.
*
* @param Closure $callback
* @param $callback
* @param array $options
*
* @return void
*/
public function error(Closure $callback): void
public function error($callback,array $options = []): void
{
$this->errorCallback = $callback;
if (isset($options['prefix'])) {
$prefix = $options['prefix'];
$this->errorGroupNames[] = $prefix.'/';
} else {
$prefix = '';
foreach ($this->groups as $item) {
$prefix .= $item['route'];
}
}
$this->errorCallback[$prefix.'/'] = $callback;
}

/**
Expand Down Expand Up @@ -531,6 +545,44 @@ public function getMiddlewares(): array
];
}

/**
* Gets errors generated under group
*
* @return void
*/
protected function getErrorGroupNames()
{
$group = '';
foreach ($this->groups as $item) {
$group .= $item['route'];
}
$this->errorGroupNames[] = $group.'/';
}

protected function notFoundRoute()
{
foreach ($this->errorGroupNames as $key => $item) {
if (str_starts_with($this->getRequestUri(), $item))
$groupKey = $key;
}
if (isset($groupKey))
$this->currentErrorGroupName = $this->errorGroupNames[$groupKey];

if (!isset($this->errorCallback[$this->currentErrorGroupName])) {
if (!isset($this->errorCallback['/']))
$this->errorCallback['/'] = function () {
$this->response()
->setStatusCode(Response::HTTP_NOT_FOUND)
->sendHeaders();
return $this->exception('Looks like page not found or something went wrong. Please try again.');
};
$this->currentErrorGroupName = '/';
}

$this->response()->setStatusCode(Response::HTTP_NOT_FOUND)->sendHeaders();
$this->routerCommand()->runRoute($this->errorCallback[$this->currentErrorGroupName]);
}

/**
* Detect Routes Middleware; before or after
*
Expand Down
42 changes: 42 additions & 0 deletions tests/error-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

$router->group('/test', function ($router) {
$router->get('/',function () use ($router) {
return 'text page';
});
$router->get('/blue',function () {
echo 'blue page';
});
$router->group('/testing', function ($rou) {
$rou->get('/',function () {
echo 'testing page';
});
$rou->error(function () {
echo 'testing error page';
});
});


$router->error(function () {
echo 'test error page';
});
});

$router->group('/test2', function ($router) {
$router->get('/',function () {
echo 'test2 page';
});
$router->get('/yellow',function () {
echo 'yellow page';
});
$router->error(function () {
die('test2 error page');
});
});


$router->error(function () {
echo 'main error page';
});

$router->error('home@notfound',['prefix' => '/test3']);