Skip to content

Instantly share code, notes, and snippets.

@sergiors
Created March 20, 2018 20:05
Show Gist options
  • Save sergiors/69fe58aca5eb36799755fbc3fb5f8500 to your computer and use it in GitHub Desktop.
Save sergiors/69fe58aca5eb36799755fbc3fb5f8500 to your computer and use it in GitHub Desktop.
<?php
abstract class Container extends \Xtreamwayz\Pimple\Container
{
public function __construct(array $values = [])
{
parent::__construct($values));
$this->initializeProviders();
}
private function initializeProviders(): void
{
$providers = $this->registerProviders();
foreach ($providers as $provider) {
$this->register($provider);
}
}
abstract public function registerProviders(): array;
}
<?php
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Zend\Expressive\Helper\ServerUrlHelper;
use Zend\Expressive\Helper\ServerUrlMiddleware;
use Zend\Expressive\Helper\UrlHelper;
use Zend\Expressive\Helper\UrlHelperMiddleware;
use Zend\Expressive\Helper\ServerUrlMiddlewareFactory;
use Zend\Expressive\Helper\UrlHelperFactory;
use Zend\Expressive\Helper\UrlHelperMiddlewareFactory;
final class ExpressiveHelperServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container[ServerUrlHelper::class] = function () {
return new ServerUrlHelper;
};
$container[ServerUrlMiddleware::class] = function (Container $container) {
return (new ServerUrlMiddlewareFactory)($container);
};
$container[UrlHelper::class] = function (Container $container) {
return (new UrlHelperFactory)($container);
};
$container[UrlHelperMiddleware::class] = function (Container $container) {
return (new UrlHelperMiddlewareFactory)($container);
};
}
}
<?php
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Zend\Expressive\Router\RouteCollector;
use Zend\Expressive\Router\RouteCollectorFactory;
use Zend\Expressive\Router\Middleware\DispatchMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitHeadMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware;
use Zend\Expressive\Router\Middleware\RouteMiddleware;
use Zend\Expressive\Router\Middleware\DispatchMiddlewareFactory;
use Zend\Expressive\Router\Middleware\ImplicitHeadMiddlewareFactory;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddlewareFactory;
use Zend\Expressive\Router\Middleware\MethodNotAllowedMiddlewareFactory;
use Zend\Expressive\Router\Middleware\RouteMiddlewareFactory;
final class ExpressiveRouterServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container[DispatchMiddleware::class] = function (Container $container) {
return (new DispatchMiddlewareFactory)($container);
};
$container[ImplicitHeadMiddleware::class] = function (Container $container) {
return (new ImplicitHeadMiddlewareFactory)($container);
};
$container[ImplicitOptionsMiddleware::class] = function (Container $container) {
return (new ImplicitOptionsMiddlewareFactory)($container);
};
$container[MethodNotAllowedMiddleware::class] = function (Container $container) {
return (new MethodNotAllowedMiddlewareFactory)($container);
};
$container[RouteMiddleware::class] = function (Container $container) {
return (new RouteMiddlewareFactory)($container);
};
$container[RouteCollector::class] = function (Container $container) {
return (new RouteCollectorFactory)($container);
};
}
}
<?php
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Expressive\Application;
use Zend\Expressive\MiddlewareContainer;
use Zend\Expressive\MiddlewareFactory;
use Zend\Expressive\Middleware\ErrorResponseGenerator;
use Zend\Expressive\Response\ServerRequestErrorResponseGenerator;
use Zend\Expressive\Container\ApplicationFactory;
use Zend\Expressive\Container\ApplicationPipelineFactory;
use Zend\Expressive\Container\EmitterFactory;
use Zend\Expressive\Container\ErrorHandlerFactory;
use Zend\Expressive\Container\NotFoundHandlerFactory;
use Zend\Expressive\Container\MiddlewareContainerFactory;
use Zend\Expressive\Container\MiddlewareFactoryFactory;
use Zend\Expressive\Container\ErrorResponseGeneratorFactory;
use Zend\Expressive\Container\RequestHandlerRunnerFactory;
use Zend\Expressive\Container\ResponseFactoryFactory;
use Zend\Expressive\Container\ServerRequestErrorResponseGeneratorFactory;
use Zend\Expressive\Container\ServerRequestFactoryFactory;
use Zend\Expressive\Container\StreamFactoryFactory;
use Zend\Expressive\Handler\NotFoundHandler;
use Zend\HttpHandlerRunner\RequestHandlerRunner;
use Zend\HttpHandlerRunner\Emitter\EmitterInterface;
use Zend\Stratigility\Middleware\ErrorHandler;
final class ExpressiveServiceProvider implements ServiceProviderInterface
{
public function register(Container $container)
{
$container[Application::class] = function (Container $container) {
return (new ApplicationFactory)($container);
};
$container['Zend\Expressive\ApplicationPipeline'] = function (Container $container) {
return (new ApplicationPipelineFactory)($container);
};
$container[EmitterInterface::class] = function (Container $container) {
return (new EmitterFactory)($container);
};
$container[ErrorHandler::class] = function (Container $container) {
return (new ErrorHandlerFactory)($container);
};
$container[NotFoundHandler::class] = function (Container $container) {
return (new NotFoundHandlerFactory)($container);
};
$container[MiddlewareContainer::class] = function (Container $container) {
return (new MiddlewareContainerFactory)($container);
};
$container[MiddlewareFactory::class] = function (Container $container) {
return (new MiddlewareFactoryFactory)($container);
};
$container[ErrorResponseGenerator::class] = function (Container $container) {
return (new ErrorResponseGeneratorFactory)($container);
};
$container[RequestHandlerRunner::class] = function (Container $container) {
return (new RequestHandlerRunnerFactory)($container);
};
$container[ResponseInterface::class] = function (Container $container) {
return (new ResponseFactoryFactory)($container);
};
$container[ServerRequestErrorResponseGenerator::class] = function (Container $container) {
return (new ServerRequestErrorResponseGeneratorFactory)($container);
};
$container[ServerRequestInterface::class] = function (Container $container) {
return (new ServerRequestFactoryFactory)($container);
};
$container[StreamInterface::class] = function (Container $container) {
return (new StreamFactoryFactory)($container);
};
}
}
<?php
$container = new class extends Container {
public function registerProviders(): array
{
return [
new ExpressiveServiceProvider,
new ExpressiveRouterServiceProvider,
new ExpressiveHelperServiceProvider,
new FastRouteServiceProvider,
new WhoopsHandlerServiceProvider,
];
}
};
$app = $container->get(Application::class);
$app->pipe(ErrorHandler::class);
$app->pipe(ServerUrlMiddleware::class);
$app->pipe(RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(DispatchMiddleware::class);
$app->pipe(NotFoundHandler::class);
$app->run();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment