Skip to content
Merged
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
39 changes: 7 additions & 32 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Bow\Application\Exception\ApplicationException;
use Bow\Configuration\Loader;
use Bow\Container\Capsule;
use Bow\Container\Compass;
use Bow\Contracts\ResponseInterface;
use Bow\Http\Exception\BadRequestException;
use Bow\Http\Exception\HttpException;
Expand Down Expand Up @@ -86,15 +85,15 @@ public function __construct(Request $request, Response $response)
$this->request = $request;
$this->response = $response;

$this->request->capture();

$this->router = Router::configure($request->get('_method'));
$this->capsule = Capsule::getInstance();

$this->capsule->instance('response', $response);
$this->capsule->instance('request', $request);
$this->capsule->instance('router', $this->router);
$this->capsule->instance('app', $this);

$this->request->capture();
}

/**
Expand Down Expand Up @@ -149,13 +148,6 @@ public function run(): bool

$method = $this->request->method();

// We verify the existence of a special method DELETE, PUT
if ($method == 'POST') {
if ($this->router->hasSpecialMethod()) {
$method = $this->router->getSpecialMethod();
}
}

// We verify the existence of the method of the request in
// the routing collection
$routes = $this->router->getRoutes();
Expand Down Expand Up @@ -186,23 +178,16 @@ public function run(): bool

// Error management
if ($resolved) {
$this->sendResponse($response);
$this->send($response);
return true;
}

// We apply the 404 error code
$this->response->status(404);
$error_code = $this->router->getErrorCodes();

if (!array_key_exists(404, $error_code)) {
throw new RouterException(
sprintf('Route "%s" not found', $this->request->path())
);
}

$response = Compass::getInstance()->execute($this->router->getErrorCodes(), []);

$this->sendResponse($response, 404);
throw new RouterException(
sprintf('Route "%s" not found', $this->request->path())
);

return false;
}
Expand All @@ -214,7 +199,7 @@ public function run(): bool
* @param int $code
* @return void
*/
private function sendResponse(mixed $response, int $code = 200): void
private function send(mixed $response, int $code = 200): void
{
if ($response instanceof ResponseInterface) {
$response->sendContent();
Expand Down Expand Up @@ -423,16 +408,6 @@ public function __invoke(...$params): mixed
return $this->capsule->bind($params[0], $params[1]);
}

/**
* Send the application response
*
* @return void
*/
public function send(): void
{
$this->run();
}

/**
* Delegate method calls to the router
*
Expand Down
71 changes: 32 additions & 39 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Router
*
* @var array
*/
protected array $error_code = [];
protected array $error_codes = [];

/**
* Define the global middleware
Expand All @@ -37,11 +37,6 @@ class Router
*/
protected string $prefix = '';

/**
* @var ?string
*/
protected ?string $special_method = null;

/**
* Define the domain constraint for routes
*
Expand Down Expand Up @@ -247,7 +242,7 @@ public function route(array $definition): void
unset($cb['controller']);
}

$route = $this->pushHttpVerb($method, $path, $cb);
$route = $this->pushMany($method, $path, $cb);

if (isset($definition['middleware'])) {
$route->middleware($definition['middleware']);
Expand All @@ -261,28 +256,24 @@ public function route(array $definition): void
}

/**
* Add other HTTP verbs [PUT, DELETE, UPDATE, HEAD, PATCH]
* Add other HTTP verbs [PUT, DELETE, OPTIONS, HEAD, PATCH]
*
* @param string|array $methods
* @param string $path
* @param callable|array|string $cb
* @return Route
*/
private function pushHttpVerb(string|array $methods, string $path, callable|string|array $cb): Route
private function pushMany(string|array $methods, string $path, callable|string|array $cb): Route
{
$methods = (array) $methods;

if (!$this->magic_method) {
return $this->routeLoader($methods, $path, $cb);
}

foreach ($methods as $key => $method) {
if ($this->magic_method === $method) {
$methods[$key] = $this->magic_method;
if (in_array($this->magic_method, ['PUT', 'DELETE', 'PATCH']) && in_array($method, ['PUT', 'DELETE', 'PATCH'])) {
$methods[$key] = 'POST';
}
}

return $this->routeLoader($methods, $path, $cb);
return $this->push($methods, $path, $cb);
}

/**
Expand All @@ -293,7 +284,7 @@ private function pushHttpVerb(string|array $methods, string $path, callable|stri
* @param callable|string|array $cb
* @return Route
*/
private function routeLoader(string|array $methods, string $path, callable|string|array $cb): Route
private function push(string|array $methods, string $path, callable|string|array $cb): Route
{
$methods = (array) $methods;

Expand Down Expand Up @@ -361,7 +352,7 @@ public function any(string $path, callable|string|array $cb): Route
{
$methods = array_map('strtoupper', ['options', 'patch', 'post', 'delete', 'put', 'get']);

return $this->pushHttpVerb($methods, $path, $cb);
return $this->pushMany($methods, $path, $cb);
}

/**
Expand All @@ -373,7 +364,7 @@ public function any(string $path, callable|string|array $cb): Route
*/
public function get(string $path, callable|string|array $cb): Route
{
return $this->routeLoader('GET', $path, $cb);
return $this->push('GET', $path, $cb);
}

/**
Expand All @@ -385,17 +376,7 @@ public function get(string $path, callable|string|array $cb): Route
*/
public function post(string $path, callable|string|array $cb): Route
{
if (!$this->magic_method) {
return $this->routeLoader('POST', $path, $cb);
}

$method = strtoupper($this->magic_method);

if (in_array($method, ['DELETE', 'PUT'])) {
$this->special_method = $method;
}

return $this->pushHttpVerb($method, $path, $cb);
return $this->push('POST', $path, $cb);
}

/**
Expand All @@ -407,7 +388,11 @@ public function post(string $path, callable|string|array $cb): Route
*/
public function delete(string $path, callable|string|array $cb): Route
{
return $this->pushHttpVerb('DELETE', $path, $cb);
if ($this->magic_method && strtoupper($this->magic_method) === 'DELETE') {
return $this->post($path, $cb);
}

return $this->push('DELETE', $path, $cb);
}

/**
Expand All @@ -419,7 +404,11 @@ public function delete(string $path, callable|string|array $cb): Route
*/
public function put(string $path, callable|string|array $cb): Route
{
return $this->pushHttpVerb('PUT', $path, $cb);
if ($this->magic_method && strtoupper($this->magic_method) === 'PUT') {
return $this->post($path, $cb);
}

return $this->push('PUT', $path, $cb);
}

/**
Expand All @@ -431,7 +420,11 @@ public function put(string $path, callable|string|array $cb): Route
*/
public function patch(string $path, callable|string|array $cb): Route
{
return $this->pushHttpVerb('PATCH', $path, $cb);
if ($this->magic_method && strtoupper($this->magic_method) === 'PATCH') {
return $this->post($path, $cb);
}

return $this->push('PATCH', $path, $cb);
}

/**
Expand All @@ -443,7 +436,7 @@ public function patch(string $path, callable|string|array $cb): Route
*/
public function options(string $path, callable|string|array $cb): Route
{
return $this->pushHttpVerb('OPTIONS', $path, $cb);
return $this->push('OPTIONS', $path, $cb);
}

/**
Expand All @@ -456,7 +449,7 @@ public function options(string $path, callable|string|array $cb): Route
*/
public function code(int $code, callable|array|string $cb): Router
{
$this->error_code[$code] = $cb;
$this->error_codes[$code] = $cb;

return $this;
}
Expand All @@ -468,7 +461,7 @@ public function code(int $code, callable|array|string $cb): Router
*/
public function getErrorCodes(): array
{
return $this->error_code;
return $this->error_codes;
}

/**
Expand All @@ -483,7 +476,7 @@ public function match(array $methods, string $path, callable|string|array $cb):
{
$methods = array_map('strtoupper', $methods);

return $this->pushHttpVerb($methods, $path, $cb);
return $this->pushMany($methods, $path, $cb);
}

/**
Expand All @@ -503,7 +496,7 @@ public function getRoutes(): array
*/
public function getSpecialMethod(): string
{
return $this->special_method;
return $this->magic_method;
}

/**
Expand All @@ -513,7 +506,7 @@ public function getSpecialMethod(): string
*/
public function hasSpecialMethod(): bool
{
return !is_null($this->special_method);
return !is_null($this->magic_method);
}

/**
Expand Down
Loading