Skip to content

Commit c6a8566

Browse files
committed
Fixed some bugs related with the paths.
1 parent 24a7ec7 commit c6a8566

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

src/Router.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Router
4040
/**
4141
* Router Version
4242
*/
43-
const VERSION = '2.3.0';
43+
const VERSION = '2.3.4';
4444

4545
/**
4646
* @var string $baseFolder Pattern definitions for parameters of Route
@@ -133,8 +133,8 @@ class Router
133133
/**
134134
* Router constructor method.
135135
*
136-
* @param array $params
137-
* @param Request|null $request
136+
* @param array $params
137+
* @param Request|null $request
138138
* @param Response|null $response
139139
*/
140140
public function __construct(array $params = [], Request $request = null, Response $response = null)
@@ -211,10 +211,10 @@ public function __call($method, $params)
211211
/**
212212
* Add new route method one or more http methods.
213213
*
214-
* @param string $methods
215-
* @param string $route
214+
* @param string $methods
215+
* @param string $route
216216
* @param string|closure $callback
217-
* @param array $options
217+
* @param array $options
218218
*
219219
* @return bool
220220
*/
@@ -241,12 +241,12 @@ public function add(string $methods, string $route, $callback, array $options =
241241
* Add new route rules pattern; String or Array
242242
*
243243
* @param string|array $pattern
244-
* @param null|string $attr
244+
* @param string|null $attr
245245
*
246246
* @return mixed
247247
* @throws
248248
*/
249-
public function pattern($pattern, $attr = null)
249+
public function pattern($pattern, string $attr = null)
250250
{
251251
if (is_array($pattern)) {
252252
foreach ($pattern as $key => $value) {
@@ -340,9 +340,9 @@ public function run(): void
340340
/**
341341
* Routes Group
342342
*
343-
* @param string $prefix
344-
* @param closure $callback
345-
* @param array $options
343+
* @param string $prefix
344+
* @param Closure $callback
345+
* @param array $options
346346
*
347347
* @return bool
348348
*/
@@ -359,9 +359,7 @@ public function group(string $prefix, Closure $callback, array $options = []): b
359359

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

362-
if (is_object($callback)) {
363-
call_user_func_array($callback, [$this]);
364-
}
362+
call_user_func_array($callback, [$this]);
365363

366364
$this->endGroup();
367365

@@ -373,7 +371,7 @@ public function group(string $prefix, Closure $callback, array $options = []): b
373371
*
374372
* @param string $route
375373
* @param string $controller
376-
* @param array $options
374+
* @param array $options
377375
*
378376
* @return mixed
379377
* @throws
@@ -543,7 +541,7 @@ public function getMiddlewares(): array
543541
/**
544542
* Detect Routes Middleware; before or after
545543
*
546-
* @param array $middleware
544+
* @param array $middleware
547545
* @param string $type
548546
*
549547
* @return void
@@ -573,12 +571,12 @@ protected function response(): Response
573571
* Throw new Exception for Router Error
574572
*
575573
* @param string $message
576-
* @param int $statusCode
574+
* @param int $statusCode
577575
*
578576
* @return RouterException
579577
* @throws Exception
580578
*/
581-
protected function exception($message = '', int $statusCode = 500): RouterException
579+
protected function exception(string $message = '', int $statusCode = 500): RouterException
582580
{
583581
return new RouterException($message, $statusCode);
584582
}
@@ -612,21 +610,21 @@ protected function setPaths(array $params): void
612610

613611
if (isset($params['paths']) && $paths = $params['paths']) {
614612
$this->paths['controllers'] = isset($paths['controllers'])
615-
? trim($paths['controllers'], '/')
613+
? rtrim($paths['controllers'], '/')
616614
: $this->paths['controllers'];
617615

618616
$this->paths['middlewares'] = isset($paths['middlewares'])
619-
? trim($paths['middlewares'], '/')
617+
? rtrim($paths['middlewares'], '/')
620618
: $this->paths['middlewares'];
621619
}
622620

623621
if (isset($params['namespaces']) && $namespaces = $params['namespaces']) {
624622
$this->namespaces['controllers'] = isset($namespaces['controllers'])
625-
? trim($namespaces['controllers'], '\\') . '\\'
623+
? rtrim($namespaces['controllers'], '\\') . '\\'
626624
: '';
627625

628626
$this->namespaces['middlewares'] = isset($namespaces['middlewares'])
629-
? trim($namespaces['middlewares'], '\\') . '\\'
627+
? rtrim($namespaces['middlewares'], '\\') . '\\'
630628
: '';
631629
}
632630

@@ -638,7 +636,7 @@ protected function setPaths(array $params): void
638636
$this->mainMethod = $params['main_method'];
639637
}
640638

641-
$this->cacheFile = isset($params['cache']) ? $params['cache'] : realpath(__DIR__ . '/../cache.php');
639+
$this->cacheFile = $params['cache'] ?? realpath(__DIR__ . '/../cache.php');
642640
}
643641

644642
/**
@@ -694,11 +692,11 @@ protected function loadCache(): bool
694692
* @param string $uri
695693
* @param string $method
696694
* @param $callback
697-
* @param array $options
695+
* @param array|null $options
698696
*
699697
* @return void
700698
*/
701-
protected function addRoute(string $uri, string $method, $callback, $options = [])
699+
protected function addRoute(string $uri, string $method, $callback, ?array $options = null)
702700
{
703701
$groupUri = '';
704702
$groupStack = [];
@@ -752,11 +750,11 @@ protected function calculateMiddleware($middleware): array
752750
* Run Route Command; Controller or Closure
753751
*
754752
* @param $command
755-
* @param $params
753+
* @param array $params
756754
*
757755
* @return void
758756
*/
759-
protected function runRouteCommand($command, $params = [])
757+
protected function runRouteCommand($command, array $params = [])
760758
{
761759
$this->routerCommand()->runRoute($command, $params);
762760
}
@@ -787,7 +785,7 @@ protected function clearRouteName(string $route = ''): string
787785
*/
788786
protected function getRequestUri(): string
789787
{
790-
$script = $this->request()->server->get('SCRIPT_NAME');
788+
$script = $this->request()->server->get('SCRIPT_FILENAME') ?? $this->request()->server->get('SCRIPT_NAME');
791789
$dirname = dirname($script);
792790
$dirname = $dirname === '/' ? '' : $dirname;
793791
$basename = basename($script);

src/RouterCommand.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function __construct(
9292
public function getMiddlewareInfo(): array
9393
{
9494
return [
95-
'path' => "{$this->baseFolder}/{$this->paths['middlewares']}",
95+
'path' => "{$this->paths['middlewares']}",
9696
'namespace' => $this->namespaces['middlewares'],
9797
];
9898
}
@@ -103,7 +103,7 @@ public function getMiddlewareInfo(): array
103103
public function getControllerInfo(): array
104104
{
105105
return [
106-
'path' => "{$this->baseFolder}/{$this->paths['controllers']}",
106+
'path' => "{$this->paths['controllers']}",
107107
'namespace' => $this->namespaces['controllers'],
108108
];
109109
}
@@ -125,7 +125,8 @@ public static function getInstance(
125125
Request $request,
126126
Response $response,
127127
array $middlewares
128-
) {
128+
): ?RouterCommand
129+
{
129130
if (null === self::$instance) {
130131
self::$instance = new static(
131132
$baseFolder, $paths, $namespaces,
@@ -313,10 +314,10 @@ protected function runMiddleware(string $command, string $middleware, array $par
313314
$middlewareMethod = 'handle'; // For now, it's constant.
314315
$controller = $this->resolveClass($middleware, $info['path'], $info['namespace']);
315316

316-
if (in_array($className = get_class($controller), $this->markedMiddlewares)) {
317+
if (in_array($command, $this->markedMiddlewares)) {
317318
return true;
318319
}
319-
array_push($this->markedMiddlewares, $className);
320+
array_push($this->markedMiddlewares, $command);
320321

321322
if (!method_exists($controller, $middlewareMethod)) {
322323
return $this->exception("{$middlewareMethod}() method is not found in {$middleware} class.");
@@ -359,7 +360,7 @@ protected function resolveMiddleware(string $middleware)
359360
*/
360361
protected function sendResponse($response)
361362
{
362-
if (is_array($response)) {
363+
if (is_array($response) || strpos($this->request->headers->get('Accept'), 'application/json') !== false) {
363364
$this->response->headers->set('Content-Type', 'application/json');
364365
return $this->response
365366
->setContent(json_encode($response))
@@ -377,12 +378,12 @@ protected function sendResponse($response)
377378
* Throw new Exception for Router Error
378379
*
379380
* @param string $message
380-
* @param int $statusCode
381+
* @param int $statusCode
381382
*
382383
* @return RouterException
383384
* @throws Exception
384385
*/
385-
protected function exception($message = '', $statusCode = 500)
386+
protected function exception(string $message = '', int $statusCode = 500)
386387
{
387388
return new RouterException($message, $statusCode);
388389
}

0 commit comments

Comments
 (0)