As for the Container, I use Auryn. Here’s the setup:
// container.php
$shares = [
\Zend\Expressive\Router\RouterInterface::class,
\Zend\Expressive\Middleware\DispatchMiddleware::class,
\Zend\Expressive\Middleware\ImplicitHeadMiddleware::class,
\Zend\Expressive\Middleware\ImplicitOptionsMiddleware::class,
\Zend\Expressive\Middleware\RouteMiddleware::class,
\Zend\Expressive\ApplicationPipeline::class,
\Zend\HttpHandlerRunner\RequestHandlerRunner::class,
\Zend\HttpHandlerRunner\Emitter\EmitterInterface::class,
\Zend\Expressive\Response\ServerRequestErrorResponseGenerator::class,
\Psr\Http\Message\ServerRequestInterface::class,
\Psr\Http\Message\ResponseInterface::class,
];
$aliases = [
\Zend\Expressive\Router\RouterInterface::class => \Zend\Expressive\Router\FastRouteRouter::class,
\Zend\Expressive\Middleware\DispatchMiddleware::class => \Zend\Expressive\Router\Middleware\DispatchMiddleware::class,
\Zend\Expressive\Middleware\ImplicitHeadMiddleware::class => \Zend\Expressive\Router\Middleware\ImplicitHeadMiddleware::class,
\Zend\Expressive\Middleware\ImplicitOptionsMiddleware::class => \Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware::class,
\Zend\Expressive\Middleware\RouteMiddleware::class => \Zend\Expressive\Router\Middleware\RouteMiddleware::class,
];
$factories = [
\Zend\Expressive\ApplicationPipeline::class => \Zend\Expressive\Container\ApplicationPipelineFactory::class,
\Zend\HttpHandlerRunner\RequestHandlerRunner::class => \Zend\Expressive\Container\RequestHandlerRunnerFactory::class,
\Zend\HttpHandlerRunner\Emitter\EmitterInterface::class => \Zend\Expressive\Container\EmitterFactory::class,
\Zend\Expressive\Response\ServerRequestErrorResponseGenerator::class => \Zend\Expressive\Container\ServerRequestErrorResponseGeneratorFactory::class,
\Psr\Http\Message\ServerRequestInterface::class => \Zend\Expressive\Container\ServerRequestFactoryFactory::class,
\Psr\Http\Message\ResponseInterface::class => \Zend\Expressive\Container\ResponseFactoryFactory::class,
];
foreach ($aliases as $k => $v) {
$injector->alias($k, $v);
if (in_array($k, $shares, true)) {
$injector->share($k, $v);
}
}
foreach ($factories as $k => $v) {
$injector->delegate($k, $v);
if (in_array($k, $shares, true)) {
$injector->share($k, $v);
}
}
And the app factory:
public static function create(
\Psr\Container\ContainerInterface $injector,
\Zend\Expressive\Router\RouterInterface $router = null
) {
$injector->alias(\Psr\Container\ContainerInterface::class, \Eco\Core\Injector::class);
$container = require \FW_BOOTSTRAP_PATH.'/container.php'; // code above
$middlewareFactory = $container->make(MiddlewareFactory::class);
$applicationPipeline = $container->make(ApplicationPipeline::class);
$routeCollector = $container->make(RouteCollector::class);
$requestHandlerRunner = $container->make(RequestHandlerRunner::class);
$app = new \Zend\Expressive\Application(
$middlewareFactory,
$applicationPipeline,
$routeCollector,
$requestHandlerRunner
);
return $app;
}
Yes, it’s inefficient in many ways, just trying to get it working first.