Skip to content

Commit

Permalink
feat: allow register own middleware's services #2559
Browse files Browse the repository at this point in the history
  • Loading branch information
sashaaro authored and joostfaassen committed Aug 14, 2019
1 parent b9d1f1a commit cbfc3e2
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Radvance;

use Nyholm\Psr7\Factory\Psr17Factory;
use OpenAPIValidation\Schema\Validator;
use Silex\Application;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
Expand All @@ -14,32 +15,42 @@

class Runner
{
public const MIDDLEWARE_SERVICES_LIST_ID = 'middleware_services';

/**
* @param HttpKernelInterface|Application $app
* @param Request|null $request
*/
static function run(HttpKernelInterface $app, Request $request = null)
static function run(HttpKernelInterface $application, Request $request = null)
{
$flexMiddlewaresYaml = null;
$stack = $application->getStack();
$app = $stack->resolve($application);

if ($app instanceof Application) {
$flexMiddlewaresYaml = isset($app['flex_middlewares.config']) ?
$app['flex_middlewares.config'] :
realpath('../') . DIRECTORY_SEPARATOR . 'middlewares.yaml';
}

$stack = $app->getStack();
$app = $stack->resolve($app);

$request = $request ?: Request::createFromGlobals();
$middlewarePipe = new \Zend\Stratigility\MiddlewarePipe();

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

if (is_file($flexMiddlewaresYaml)) {
$middlewarePipe->pipe(FlexMiddlewareFactory::fromConfig($flexMiddlewaresYaml));
if ($application instanceof Application) {
$flexMiddlewaresYaml = isset($application['flex_middlewares.config']) ?
$application['flex_middlewares.config'] :
realpath('../') . DIRECTORY_SEPARATOR . 'middlewares.yaml';

if (is_file($flexMiddlewaresYaml)) {
$middlewarePipe->pipe(FlexMiddlewareFactory::fromConfig($flexMiddlewaresYaml));
}

if (isset($application[self::MIDDLEWARE_SERVICES_LIST_ID])) {
foreach ($application[self::MIDDLEWARE_SERVICES_LIST_ID] as $middlewareServiceID) {
if (!isset($application[$middlewareServiceID])) {
throw new \InvalidArgumentException("No $middlewareServiceID middleware service");
}

$middlewarePipe->pipe($application[$middlewareServiceID]);
}
}
}

$middlewarePipe->pipe(new HttpKernelMiddleware($app));
Expand Down

0 comments on commit cbfc3e2

Please sign in to comment.