Skip to content

Commit cbfc3e2

Browse files
sashaarojoostfaassen
authored andcommitted
feat: allow register own middleware's services #2559
1 parent b9d1f1a commit cbfc3e2

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

src/Runner.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Radvance;
44

55
use Nyholm\Psr7\Factory\Psr17Factory;
6+
use OpenAPIValidation\Schema\Validator;
67
use Silex\Application;
78
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
89
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
@@ -14,32 +15,42 @@
1415

1516
class Runner
1617
{
18+
public const MIDDLEWARE_SERVICES_LIST_ID = 'middleware_services';
19+
1720
/**
1821
* @param HttpKernelInterface|Application $app
1922
* @param Request|null $request
2023
*/
21-
static function run(HttpKernelInterface $app, Request $request = null)
24+
static function run(HttpKernelInterface $application, Request $request = null)
2225
{
23-
$flexMiddlewaresYaml = null;
26+
$stack = $application->getStack();
27+
$app = $stack->resolve($application);
2428

25-
if ($app instanceof Application) {
26-
$flexMiddlewaresYaml = isset($app['flex_middlewares.config']) ?
27-
$app['flex_middlewares.config'] :
28-
realpath('../') . DIRECTORY_SEPARATOR . 'middlewares.yaml';
29-
}
30-
31-
$stack = $app->getStack();
32-
$app = $stack->resolve($app);
33-
3429
$request = $request ?: Request::createFromGlobals();
3530
$middlewarePipe = new \Zend\Stratigility\MiddlewarePipe();
3631

3732
$psr17Factory = new Psr17Factory();
3833
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
3934
$psrRequest = $psrHttpFactory->createRequest($request);
4035

41-
if (is_file($flexMiddlewaresYaml)) {
42-
$middlewarePipe->pipe(FlexMiddlewareFactory::fromConfig($flexMiddlewaresYaml));
36+
if ($application instanceof Application) {
37+
$flexMiddlewaresYaml = isset($application['flex_middlewares.config']) ?
38+
$application['flex_middlewares.config'] :
39+
realpath('../') . DIRECTORY_SEPARATOR . 'middlewares.yaml';
40+
41+
if (is_file($flexMiddlewaresYaml)) {
42+
$middlewarePipe->pipe(FlexMiddlewareFactory::fromConfig($flexMiddlewaresYaml));
43+
}
44+
45+
if (isset($application[self::MIDDLEWARE_SERVICES_LIST_ID])) {
46+
foreach ($application[self::MIDDLEWARE_SERVICES_LIST_ID] as $middlewareServiceID) {
47+
if (!isset($application[$middlewareServiceID])) {
48+
throw new \InvalidArgumentException("No $middlewareServiceID middleware service");
49+
}
50+
51+
$middlewarePipe->pipe($application[$middlewareServiceID]);
52+
}
53+
}
4354
}
4455

4556
$middlewarePipe->pipe(new HttpKernelMiddleware($app));

0 commit comments

Comments
 (0)