Skip to content

Commit 8634045

Browse files
authored
Merge pull request #372 from bowphp/refactor/code-base
Refactoring magic method definition
2 parents 88c7a42 + 1e349ba commit 8634045

2 files changed

Lines changed: 39 additions & 71 deletions

File tree

src/Application/Application.php

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Bow\Application\Exception\ApplicationException;
88
use Bow\Configuration\Loader;
99
use Bow\Container\Capsule;
10-
use Bow\Container\Compass;
1110
use Bow\Contracts\ResponseInterface;
1211
use Bow\Http\Exception\BadRequestException;
1312
use Bow\Http\Exception\HttpException;
@@ -86,15 +85,15 @@ public function __construct(Request $request, Response $response)
8685
$this->request = $request;
8786
$this->response = $response;
8887

88+
$this->request->capture();
89+
8990
$this->router = Router::configure($request->get('_method'));
9091
$this->capsule = Capsule::getInstance();
9192

9293
$this->capsule->instance('response', $response);
9394
$this->capsule->instance('request', $request);
9495
$this->capsule->instance('router', $this->router);
9596
$this->capsule->instance('app', $this);
96-
97-
$this->request->capture();
9897
}
9998

10099
/**
@@ -149,13 +148,6 @@ public function run(): bool
149148

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

152-
// We verify the existence of a special method DELETE, PUT
153-
if ($method == 'POST') {
154-
if ($this->router->hasSpecialMethod()) {
155-
$method = $this->router->getSpecialMethod();
156-
}
157-
}
158-
159151
// We verify the existence of the method of the request in
160152
// the routing collection
161153
$routes = $this->router->getRoutes();
@@ -186,23 +178,16 @@ public function run(): bool
186178

187179
// Error management
188180
if ($resolved) {
189-
$this->sendResponse($response);
181+
$this->send($response);
190182
return true;
191183
}
192184

193185
// We apply the 404 error code
194186
$this->response->status(404);
195-
$error_code = $this->router->getErrorCodes();
196-
197-
if (!array_key_exists(404, $error_code)) {
198-
throw new RouterException(
199-
sprintf('Route "%s" not found', $this->request->path())
200-
);
201-
}
202-
203-
$response = Compass::getInstance()->execute($this->router->getErrorCodes(), []);
204187

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

207192
return false;
208193
}
@@ -214,7 +199,7 @@ public function run(): bool
214199
* @param int $code
215200
* @return void
216201
*/
217-
private function sendResponse(mixed $response, int $code = 200): void
202+
private function send(mixed $response, int $code = 200): void
218203
{
219204
if ($response instanceof ResponseInterface) {
220205
$response->sendContent();
@@ -423,16 +408,6 @@ public function __invoke(...$params): mixed
423408
return $this->capsule->bind($params[0], $params[1]);
424409
}
425410

426-
/**
427-
* Send the application response
428-
*
429-
* @return void
430-
*/
431-
public function send(): void
432-
{
433-
$this->run();
434-
}
435-
436411
/**
437412
* Delegate method calls to the router
438413
*

src/Router/Router.php

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Router
2121
*
2222
* @var array
2323
*/
24-
protected array $error_code = [];
24+
protected array $error_codes = [];
2525

2626
/**
2727
* Define the global middleware
@@ -37,11 +37,6 @@ class Router
3737
*/
3838
protected string $prefix = '';
3939

40-
/**
41-
* @var ?string
42-
*/
43-
protected ?string $special_method = null;
44-
4540
/**
4641
* Define the domain constraint for routes
4742
*
@@ -247,7 +242,7 @@ public function route(array $definition): void
247242
unset($cb['controller']);
248243
}
249244

250-
$route = $this->pushHttpVerb($method, $path, $cb);
245+
$route = $this->pushMany($method, $path, $cb);
251246

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

263258
/**
264-
* Add other HTTP verbs [PUT, DELETE, UPDATE, HEAD, PATCH]
259+
* Add other HTTP verbs [PUT, DELETE, OPTIONS, HEAD, PATCH]
265260
*
266261
* @param string|array $methods
267262
* @param string $path
268263
* @param callable|array|string $cb
269264
* @return Route
270265
*/
271-
private function pushHttpVerb(string|array $methods, string $path, callable|string|array $cb): Route
266+
private function pushMany(string|array $methods, string $path, callable|string|array $cb): Route
272267
{
273268
$methods = (array) $methods;
274269

275-
if (!$this->magic_method) {
276-
return $this->routeLoader($methods, $path, $cb);
277-
}
278-
279270
foreach ($methods as $key => $method) {
280-
if ($this->magic_method === $method) {
281-
$methods[$key] = $this->magic_method;
271+
if (in_array($this->magic_method, ['PUT', 'DELETE', 'PATCH']) && in_array($method, ['PUT', 'DELETE', 'PATCH'])) {
272+
$methods[$key] = 'POST';
282273
}
283274
}
284275

285-
return $this->routeLoader($methods, $path, $cb);
276+
return $this->push($methods, $path, $cb);
286277
}
287278

288279
/**
@@ -293,7 +284,7 @@ private function pushHttpVerb(string|array $methods, string $path, callable|stri
293284
* @param callable|string|array $cb
294285
* @return Route
295286
*/
296-
private function routeLoader(string|array $methods, string $path, callable|string|array $cb): Route
287+
private function push(string|array $methods, string $path, callable|string|array $cb): Route
297288
{
298289
$methods = (array) $methods;
299290

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

364-
return $this->pushHttpVerb($methods, $path, $cb);
355+
return $this->pushMany($methods, $path, $cb);
365356
}
366357

367358
/**
@@ -373,7 +364,7 @@ public function any(string $path, callable|string|array $cb): Route
373364
*/
374365
public function get(string $path, callable|string|array $cb): Route
375366
{
376-
return $this->routeLoader('GET', $path, $cb);
367+
return $this->push('GET', $path, $cb);
377368
}
378369

379370
/**
@@ -385,17 +376,7 @@ public function get(string $path, callable|string|array $cb): Route
385376
*/
386377
public function post(string $path, callable|string|array $cb): Route
387378
{
388-
if (!$this->magic_method) {
389-
return $this->routeLoader('POST', $path, $cb);
390-
}
391-
392-
$method = strtoupper($this->magic_method);
393-
394-
if (in_array($method, ['DELETE', 'PUT'])) {
395-
$this->special_method = $method;
396-
}
397-
398-
return $this->pushHttpVerb($method, $path, $cb);
379+
return $this->push('POST', $path, $cb);
399380
}
400381

401382
/**
@@ -407,7 +388,11 @@ public function post(string $path, callable|string|array $cb): Route
407388
*/
408389
public function delete(string $path, callable|string|array $cb): Route
409390
{
410-
return $this->pushHttpVerb('DELETE', $path, $cb);
391+
if ($this->magic_method && strtoupper($this->magic_method) === 'DELETE') {
392+
return $this->post($path, $cb);
393+
}
394+
395+
return $this->push('DELETE', $path, $cb);
411396
}
412397

413398
/**
@@ -419,7 +404,11 @@ public function delete(string $path, callable|string|array $cb): Route
419404
*/
420405
public function put(string $path, callable|string|array $cb): Route
421406
{
422-
return $this->pushHttpVerb('PUT', $path, $cb);
407+
if ($this->magic_method && strtoupper($this->magic_method) === 'PUT') {
408+
return $this->post($path, $cb);
409+
}
410+
411+
return $this->push('PUT', $path, $cb);
423412
}
424413

425414
/**
@@ -431,7 +420,11 @@ public function put(string $path, callable|string|array $cb): Route
431420
*/
432421
public function patch(string $path, callable|string|array $cb): Route
433422
{
434-
return $this->pushHttpVerb('PATCH', $path, $cb);
423+
if ($this->magic_method && strtoupper($this->magic_method) === 'PATCH') {
424+
return $this->post($path, $cb);
425+
}
426+
427+
return $this->push('PATCH', $path, $cb);
435428
}
436429

437430
/**
@@ -443,7 +436,7 @@ public function patch(string $path, callable|string|array $cb): Route
443436
*/
444437
public function options(string $path, callable|string|array $cb): Route
445438
{
446-
return $this->pushHttpVerb('OPTIONS', $path, $cb);
439+
return $this->push('OPTIONS', $path, $cb);
447440
}
448441

449442
/**
@@ -456,7 +449,7 @@ public function options(string $path, callable|string|array $cb): Route
456449
*/
457450
public function code(int $code, callable|array|string $cb): Router
458451
{
459-
$this->error_code[$code] = $cb;
452+
$this->error_codes[$code] = $cb;
460453

461454
return $this;
462455
}
@@ -468,7 +461,7 @@ public function code(int $code, callable|array|string $cb): Router
468461
*/
469462
public function getErrorCodes(): array
470463
{
471-
return $this->error_code;
464+
return $this->error_codes;
472465
}
473466

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

486-
return $this->pushHttpVerb($methods, $path, $cb);
479+
return $this->pushMany($methods, $path, $cb);
487480
}
488481

489482
/**
@@ -503,7 +496,7 @@ public function getRoutes(): array
503496
*/
504497
public function getSpecialMethod(): string
505498
{
506-
return $this->special_method;
499+
return $this->magic_method;
507500
}
508501

509502
/**
@@ -513,7 +506,7 @@ public function getSpecialMethod(): string
513506
*/
514507
public function hasSpecialMethod(): bool
515508
{
516-
return !is_null($this->special_method);
509+
return !is_null($this->magic_method);
517510
}
518511

519512
/**

0 commit comments

Comments
 (0)