|
@@ -6893,6 +6893,17 @@ namespace Tqdev\PhpCrudApi\Middleware\Base {
|
6893
|
6893
|
$this->properties = $properties;
|
6894
|
6894
|
}
|
6895
|
6895
|
|
|
6896
|
+ /**
|
|
6897
|
+ * allows to load middlewares in a specific order
|
|
6898
|
+ * The higher the priority, the earlier the middleware will be called
|
|
6899
|
+ *
|
|
6900
|
+ * @return int
|
|
6901
|
+ */
|
|
6902
|
+ public function getPriority() /* : int */
|
|
6903
|
+ {
|
|
6904
|
+ return 1;
|
|
6905
|
+ }
|
|
6906
|
+
|
6896
|
6907
|
protected function getArrayProperty(string $key, string $default): array
|
6897
|
6908
|
{
|
6898
|
6909
|
return array_filter(array_map('trim', explode(',', $this->getProperty($key, $default))));
|
|
@@ -7056,6 +7067,11 @@ namespace Tqdev\PhpCrudApi\Middleware\Router {
|
7056
|
7067
|
$data = gzcompress(json_encode($this->routes, JSON_UNESCAPED_UNICODE));
|
7057
|
7068
|
$this->cache->set('PathTree', $data, $this->ttl);
|
7058
|
7069
|
}
|
|
7070
|
+
|
|
7071
|
+ uasort($this->middlewares, function (Middleware $a, Middleware $b) {
|
|
7072
|
+ return $a->getPriority() > $b->getPriority() ? 1 : ($a->getPriority() === $b->getPriority() ? 0 : -1);
|
|
7073
|
+ });
|
|
7074
|
+
|
7059
|
7075
|
return $this->handle($request);
|
7060
|
7076
|
}
|
7061
|
7077
|
|
|
@@ -7376,6 +7392,55 @@ namespace Tqdev\PhpCrudApi\Middleware {
|
7376
|
7392
|
}
|
7377
|
7393
|
}
|
7378
|
7394
|
|
|
7395
|
+// file: src/Tqdev/PhpCrudApi/Middleware/CatchErrorsMiddleware.php
|
|
7396
|
+namespace Tqdev\PhpCrudApi\Middleware {
|
|
7397
|
+
|
|
7398
|
+ use Psr\Http\Message\ResponseInterface;
|
|
7399
|
+ use Psr\Http\Message\ServerRequestInterface;
|
|
7400
|
+ use Psr\Http\Server\RequestHandlerInterface;
|
|
7401
|
+ use Tqdev\PhpCrudApi\Controller\Responder;
|
|
7402
|
+ use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
|
|
7403
|
+ use Tqdev\PhpCrudApi\Middleware\Router\Router;
|
|
7404
|
+ use Tqdev\PhpCrudApi\Record\ErrorCode;
|
|
7405
|
+ use Tqdev\PhpCrudApi\ResponseUtils;
|
|
7406
|
+
|
|
7407
|
+ class CatchErrorsMiddleware extends Middleware
|
|
7408
|
+ {
|
|
7409
|
+ private $debug;
|
|
7410
|
+
|
|
7411
|
+ public function __construct(Router $router, Responder $responder, array $properties, bool $debug)
|
|
7412
|
+ {
|
|
7413
|
+ parent::__construct($router, $responder, $properties);
|
|
7414
|
+ $this->debug = $debug;
|
|
7415
|
+ }
|
|
7416
|
+
|
|
7417
|
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
|
|
7418
|
+ {
|
|
7419
|
+ $response = null;
|
|
7420
|
+ try {
|
|
7421
|
+ $response = $next->handle($request);
|
|
7422
|
+ } catch (\Throwable $e) {
|
|
7423
|
+ $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
|
|
7424
|
+ if ($this->debug) {
|
|
7425
|
+ $response = ResponseUtils::addExceptionHeaders($response, $e);
|
|
7426
|
+ }
|
|
7427
|
+ }
|
|
7428
|
+ return $response;
|
|
7429
|
+ }
|
|
7430
|
+
|
|
7431
|
+ /**
|
|
7432
|
+ * High priority, should always be one of the very first middlewares to be loaded
|
|
7433
|
+ * Only cors middleware should be loaded earlier
|
|
7434
|
+ *
|
|
7435
|
+ * @return int
|
|
7436
|
+ */
|
|
7437
|
+ public function getPriority()
|
|
7438
|
+ {
|
|
7439
|
+ return 998;
|
|
7440
|
+ }
|
|
7441
|
+ }
|
|
7442
|
+}
|
|
7443
|
+
|
7379
|
7444
|
// file: src/Tqdev/PhpCrudApi/Middleware/CorsMiddleware.php
|
7380
|
7445
|
namespace Tqdev\PhpCrudApi\Middleware {
|
7381
|
7446
|
|
|
@@ -7443,6 +7508,16 @@ namespace Tqdev\PhpCrudApi\Middleware {
|
7443
|
7508
|
}
|
7444
|
7509
|
return $response;
|
7445
|
7510
|
}
|
|
7511
|
+
|
|
7512
|
+ /**
|
|
7513
|
+ * load early in the routing stack. should be loaded before catc herrors middleware,
|
|
7514
|
+ * otherwise cors headers will be missing
|
|
7515
|
+ * @return int
|
|
7516
|
+ */
|
|
7517
|
+ public function getPriority()
|
|
7518
|
+ {
|
|
7519
|
+ return 999;
|
|
7520
|
+ }
|
7446
|
7521
|
}
|
7447
|
7522
|
}
|
7448
|
7523
|
|
|
@@ -10641,6 +10716,7 @@ namespace Tqdev\PhpCrudApi {
|
10641
|
10716
|
use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
|
10642
|
10717
|
use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
|
10643
|
10718
|
use Tqdev\PhpCrudApi\Middleware\BasicAuthMiddleware;
|
|
10719
|
+ use Tqdev\PhpCrudApi\Middleware\CatchErrorsMiddleware;
|
10644
|
10720
|
use Tqdev\PhpCrudApi\Middleware\CorsMiddleware;
|
10645
|
10721
|
use Tqdev\PhpCrudApi\Middleware\CustomizationMiddleware;
|
10646
|
10722
|
use Tqdev\PhpCrudApi\Middleware\DbAuthMiddleware;
|
|
@@ -10684,6 +10760,7 @@ namespace Tqdev\PhpCrudApi {
|
10684
|
10760
|
$reflection = new ReflectionService($db, $cache, $config->getCacheTime());
|
10685
|
10761
|
$responder = new JsonResponder();
|
10686
|
10762
|
$router = new SimpleRouter($config->getBasePath(), $responder, $cache, $config->getCacheTime(), $config->getDebug());
|
|
10763
|
+ new CatchErrorsMiddleware($router, $responder, [], $config->getDebug());
|
10687
|
10764
|
foreach ($config->getMiddlewares() as $middleware => $properties) {
|
10688
|
10765
|
switch ($middleware) {
|
10689
|
10766
|
case 'sslRedirect':
|
|
@@ -10737,6 +10814,9 @@ namespace Tqdev\PhpCrudApi {
|
10737
|
10814
|
case 'xml':
|
10738
|
10815
|
new XmlMiddleware($router, $responder, $properties, $reflection);
|
10739
|
10816
|
break;
|
|
10817
|
+ case 'errors':
|
|
10818
|
+ new CatchErrorsMiddleware($router, $responder, [], $config->getDebug());
|
|
10819
|
+ break;
|
10740
|
10820
|
}
|
10741
|
10821
|
}
|
10742
|
10822
|
foreach ($config->getControllers() as $controller) {
|
|
@@ -10833,16 +10913,7 @@ namespace Tqdev\PhpCrudApi {
|
10833
|
10913
|
|
10834
|
10914
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
10835
|
10915
|
{
|
10836
|
|
- $response = null;
|
10837
|
|
- try {
|
10838
|
|
- $response = $this->router->route($this->addParsedBody($request));
|
10839
|
|
- } catch (\Throwable $e) {
|
10840
|
|
- $response = $this->responder->error(ErrorCode::ERROR_NOT_FOUND, $e->getMessage());
|
10841
|
|
- if ($this->debug) {
|
10842
|
|
- $response = ResponseUtils::addExceptionHeaders($response, $e);
|
10843
|
|
- }
|
10844
|
|
- }
|
10845
|
|
- return $response;
|
|
10916
|
+ return $this->router->route($this->addParsedBody($request));
|
10846
|
10917
|
}
|
10847
|
10918
|
}
|
10848
|
10919
|
}
|
|
@@ -10860,7 +10931,7 @@ namespace Tqdev\PhpCrudApi {
|
10860
|
10931
|
'password' => null,
|
10861
|
10932
|
'database' => null,
|
10862
|
10933
|
'tables' => '',
|
10863
|
|
- 'middlewares' => 'cors',
|
|
10934
|
+ 'middlewares' => 'cors,errors',
|
10864
|
10935
|
'controllers' => 'records,geojson,openapi',
|
10865
|
10936
|
'customControllers' => '',
|
10866
|
10937
|
'customOpenApiBuilders' => '',
|