From be8479b48c0f7f6d63742a086b52c79c40104fd9 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 10 Jan 2017 16:07:10 +0100 Subject: [PATCH 01/19] Initial commit --- .gitignore | 3 ++ composer.json | 16 +++++++++ config/autoload/dependencies.global.php | 33 +++++++++++++++++++ config/autoload/errorhandler.local.php | 32 ++++++++++++++++++ .../autoload/middleware-pipeline.global.php | 14 ++++++++ 5 files changed, 98 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 config/autoload/dependencies.global.php create mode 100644 config/autoload/errorhandler.local.php create mode 100644 config/autoload/middleware-pipeline.global.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8925195 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +vendor +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..04f40f9 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "php-di/zend-expressive-bridge", + "description": "PHP-DI integration in Zend Expressive", + "license": "MIT", + "type": "library", + "require": { + "php": "~5.5|~7.0", + "php-di/php-di": "^5.2.0", + "php-di/invoker": "^1.2.0", + "zendframework/zend-expressive": "^1.0", + "zendframework/zend-expressive-helpers": "^2.2", + "zendframework/zend-expressive-fastroute": "^1.3", + "zendframework/zend-expressive-zendviewrenderer": "^1.1", + "mtymek/expressive-config-manager": "^0.4.0" + } +} \ No newline at end of file diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php new file mode 100644 index 0000000..b777d3a --- /dev/null +++ b/config/autoload/dependencies.global.php @@ -0,0 +1,33 @@ + function (\Interop\Container\ContainerInterface $container) { + return $container; + }, + \Zend\Expressive\Application::class => function (\Interop\Container\ContainerInterface $container) { + $applicationFactory = $container->get(\Zend\Expressive\Container\ApplicationFactory::class); + return $applicationFactory($container); + }, + \Zend\Expressive\Router\RouterInterface::class => get(\Zend\Expressive\Router\FastRouteRouter::class), + \Zend\Expressive\Router\FastRouteRouter::class => object()->lazy(), + \Zend\Expressive\Container\ApplicationFactory::class => object()->lazy(), + /** + * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: + * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in + * + * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() + */ + \Zend\View\HelperPluginManager::class => function (\Interop\Container\ContainerInterface $container) { + $factory = new \Zend\Expressive\ZendView\HelperPluginManagerFactory(); + return $factory($container); + }, + \Zend\Expressive\Helper\ServerUrlHelper::class => object()->lazy(), + \Zend\Expressive\Helper\UrlHelper::class => function (\Interop\Container\ContainerInterface $container) { + $factory = new \Zend\Expressive\Helper\UrlHelperFactory(); + return $factory($container); + }, +); \ No newline at end of file diff --git a/config/autoload/errorhandler.local.php b/config/autoload/errorhandler.local.php new file mode 100644 index 0000000..ca04a25 --- /dev/null +++ b/config/autoload/errorhandler.local.php @@ -0,0 +1,32 @@ + object()->lazy(), + \Zend\Expressive\Container\WhoopsFactory::class => object()->lazy(), + \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => object()->lazy(), + \Zend\Expressive\Template\TemplateRendererInterface::class => function (\Interop\Container\ContainerInterface $container) { + $factory = new \Zend\Expressive\ZendView\ZendViewRendererFactory(); + return $factory($container); + }, + 'Zend\Expressive\Whoops' => function (\Interop\Container\ContainerInterface $container) { + $factory = $container->get(\Zend\Expressive\Container\WhoopsFactory::class); + return $factory($container); + }, + 'Zend\Expressive\WhoopsPageHandler' => function (\Interop\Container\ContainerInterface $container) { + $factory = $container->get(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class); + return $factory($container); + }, + 'Zend\Expressive\FinalHandler' => function (\Interop\Container\ContainerInterface $container) { + $factory = $container->get(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class); + return $factory($container); + }, + /* in production + 'Zend\Expressive\FinalHandler' => function (\Interop\Container\ContainerInterface $container) { + $templatedErrorHandlerFactory = $container->get(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class); + return $templatedErrorHandlerFactory($container); + }, + */ +); \ No newline at end of file diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php new file mode 100644 index 0000000..9f22627 --- /dev/null +++ b/config/autoload/middleware-pipeline.global.php @@ -0,0 +1,14 @@ + function (\Interop\Container\ContainerInterface $container) { + $factory = new \Zend\Expressive\Helper\ServerUrlMiddlewareFactory(); + return $factory($container); + }, + \Zend\Expressive\Helper\UrlHelperMiddleware::class => function (\Interop\Container\ContainerInterface $container) { + $factory = new \Zend\Expressive\Helper\UrlHelperMiddlewareFactory(); + return $factory($container); + }, +); \ No newline at end of file From 5d8657d786cacdfc51318f249a4fe9abd2338e53 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 10 Jan 2017 16:24:18 +0100 Subject: [PATCH 02/19] Add configuration of the container --- README.md | 36 +++++++++++++++++++++++++++++++++++- composer.json | 3 +-- config/container.php | 12 ++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 config/container.php diff --git a/README.md b/README.md index 2ca25a9..2b4bfbf 100644 --- a/README.md +++ b/README.md @@ -1 +1,35 @@ -# Zend-Expressive-Bridge \ No newline at end of file +# Zend-Expressive-Bridge + +The easiest way to use PHP-DI with Zend Expressive, seems to: + + - assemble the Dependency Injection Container using some default definition files + - use the DIC to get the Zend Expressive Application + - run the Application + +Index.php + +``` + +set('config', $config); + +/** @var \Zend\Expressive\Application $app */ +$app = $container->get(\Zend\Expressive\Application::class); +$app->run(); + +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 04f40f9..7626741 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,6 @@ "zendframework/zend-expressive": "^1.0", "zendframework/zend-expressive-helpers": "^2.2", "zendframework/zend-expressive-fastroute": "^1.3", - "zendframework/zend-expressive-zendviewrenderer": "^1.1", - "mtymek/expressive-config-manager": "^0.4.0" + "zendframework/zend-expressive-zendviewrenderer": "^1.1" } } \ No newline at end of file diff --git a/config/container.php b/config/container.php new file mode 100644 index 0000000..b19cff8 --- /dev/null +++ b/config/container.php @@ -0,0 +1,12 @@ +addDefinitions(__DIR__ . '/autoload/dependencies.global.php'); +$containerBuilder->addDefinitions(__DIR__ . '/autoload/errorhandler.local.php'); +$containerBuilder->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.global.php'); +$this->configureContainer($containerBuilder); + +return $containerBuilder->build(); From bdfbe80376930a4ee2c7d0fda8e3d5091a58db06 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 10 Jan 2017 16:25:54 +0100 Subject: [PATCH 03/19] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b4bfbf..ac85d5d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ The easiest way to use PHP-DI with Zend Expressive, seems to: Index.php -``` +```php set('config', $config); $app = $container->get(\Zend\Expressive\Application::class); $app->run(); -``` \ No newline at end of file +``` From 0ef0f372c740a115e8a092b65b3f6dfed421e4e3 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 10 Jan 2017 16:33:40 +0100 Subject: [PATCH 04/19] Do not use lazy loading by default, this depends on the proxymanager --- config/autoload/dependencies.global.php | 6 +++--- config/autoload/errorhandler.local.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index b777d3a..f7caa80 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -13,8 +13,8 @@ return $applicationFactory($container); }, \Zend\Expressive\Router\RouterInterface::class => get(\Zend\Expressive\Router\FastRouteRouter::class), - \Zend\Expressive\Router\FastRouteRouter::class => object()->lazy(), - \Zend\Expressive\Container\ApplicationFactory::class => object()->lazy(), + \Zend\Expressive\Router\FastRouteRouter::class => object(), + \Zend\Expressive\Container\ApplicationFactory::class => object(), /** * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in @@ -25,7 +25,7 @@ $factory = new \Zend\Expressive\ZendView\HelperPluginManagerFactory(); return $factory($container); }, - \Zend\Expressive\Helper\ServerUrlHelper::class => object()->lazy(), + \Zend\Expressive\Helper\ServerUrlHelper::class => object(), \Zend\Expressive\Helper\UrlHelper::class => function (\Interop\Container\ContainerInterface $container) { $factory = new \Zend\Expressive\Helper\UrlHelperFactory(); return $factory($container); diff --git a/config/autoload/errorhandler.local.php b/config/autoload/errorhandler.local.php index ca04a25..09990e9 100644 --- a/config/autoload/errorhandler.local.php +++ b/config/autoload/errorhandler.local.php @@ -4,9 +4,9 @@ use function DI\object; return array( - \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => object()->lazy(), - \Zend\Expressive\Container\WhoopsFactory::class => object()->lazy(), - \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => object()->lazy(), + \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => object(), + \Zend\Expressive\Container\WhoopsFactory::class => object(), + \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => object(), \Zend\Expressive\Template\TemplateRendererInterface::class => function (\Interop\Container\ContainerInterface $container) { $factory = new \Zend\Expressive\ZendView\ZendViewRendererFactory(); return $factory($container); From 0d674fa8771914c6b3556b43611149c314415b2f Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Thu, 19 Jan 2017 16:08:29 +0100 Subject: [PATCH 05/19] Use DI\factory() --- config/autoload/dependencies.global.php | 26 ++++---------- config/autoload/errorhandler.local.php | 36 +++++-------------- .../autoload/middleware-pipeline.global.php | 10 ++---- config/container.php | 5 ++- 4 files changed, 20 insertions(+), 57 deletions(-) diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index f7caa80..4e94148 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -1,33 +1,21 @@ function (\Interop\Container\ContainerInterface $container) { return $container; }, - \Zend\Expressive\Application::class => function (\Interop\Container\ContainerInterface $container) { - $applicationFactory = $container->get(\Zend\Expressive\Container\ApplicationFactory::class); - return $applicationFactory($container); - }, - \Zend\Expressive\Router\RouterInterface::class => get(\Zend\Expressive\Router\FastRouteRouter::class), - \Zend\Expressive\Router\FastRouteRouter::class => object(), - \Zend\Expressive\Container\ApplicationFactory::class => object(), + \Zend\Expressive\Application::class => \DI\factory(\Zend\Expressive\Container\ApplicationFactory::class), + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), + \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), + \Zend\Expressive\Container\ApplicationFactory::class => \DI\object(), /** * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in * * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() */ - \Zend\View\HelperPluginManager::class => function (\Interop\Container\ContainerInterface $container) { - $factory = new \Zend\Expressive\ZendView\HelperPluginManagerFactory(); - return $factory($container); - }, - \Zend\Expressive\Helper\ServerUrlHelper::class => object(), - \Zend\Expressive\Helper\UrlHelper::class => function (\Interop\Container\ContainerInterface $container) { - $factory = new \Zend\Expressive\Helper\UrlHelperFactory(); - return $factory($container); - }, + \Zend\View\HelperPluginManager::class => \DI\factory(\Zend\Expressive\ZendView\HelperPluginManagerFactory::class), + \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\object(), + \Zend\Expressive\Helper\UrlHelper::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperFactory::class), ); \ No newline at end of file diff --git a/config/autoload/errorhandler.local.php b/config/autoload/errorhandler.local.php index 09990e9..32be2dd 100644 --- a/config/autoload/errorhandler.local.php +++ b/config/autoload/errorhandler.local.php @@ -1,32 +1,14 @@ object(), - \Zend\Expressive\Container\WhoopsFactory::class => object(), - \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => object(), - \Zend\Expressive\Template\TemplateRendererInterface::class => function (\Interop\Container\ContainerInterface $container) { - $factory = new \Zend\Expressive\ZendView\ZendViewRendererFactory(); - return $factory($container); - }, - 'Zend\Expressive\Whoops' => function (\Interop\Container\ContainerInterface $container) { - $factory = $container->get(\Zend\Expressive\Container\WhoopsFactory::class); - return $factory($container); - }, - 'Zend\Expressive\WhoopsPageHandler' => function (\Interop\Container\ContainerInterface $container) { - $factory = $container->get(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class); - return $factory($container); - }, - 'Zend\Expressive\FinalHandler' => function (\Interop\Container\ContainerInterface $container) { - $factory = $container->get(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class); - return $factory($container); - }, - /* in production - 'Zend\Expressive\FinalHandler' => function (\Interop\Container\ContainerInterface $container) { - $templatedErrorHandlerFactory = $container->get(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class); - return $templatedErrorHandlerFactory($container); - }, - */ + \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Container\WhoopsFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(Zend\Expressive\ZendView\ZendViewRendererFactory::class), + 'Zend\Expressive\Whoops' => \DI\factory(\Zend\Expressive\Container\WhoopsFactory::class), + 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), + 'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), + //'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class), //In production + ); \ No newline at end of file diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index 9f22627..dd43e9c 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -3,12 +3,6 @@ declare(strict_types = 1); return array( - \Zend\Expressive\Helper\ServerUrlMiddleware::class => function (\Interop\Container\ContainerInterface $container) { - $factory = new \Zend\Expressive\Helper\ServerUrlMiddlewareFactory(); - return $factory($container); - }, - \Zend\Expressive\Helper\UrlHelperMiddleware::class => function (\Interop\Container\ContainerInterface $container) { - $factory = new \Zend\Expressive\Helper\UrlHelperMiddlewareFactory(); - return $factory($container); - }, + \Zend\Expressive\Helper\ServerUrlMiddleware::class => \DI\factory(\Zend\Expressive\Helper\ServerUrlMiddlewareFactory::class), + \Zend\Expressive\Helper\UrlHelperMiddleware::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperMiddlewareFactory::class), ); \ No newline at end of file diff --git a/config/container.php b/config/container.php index b19cff8..6023e4c 100644 --- a/config/container.php +++ b/config/container.php @@ -1,9 +1,8 @@ addDefinitions(__DIR__ . '/autoload/dependencies.global.php'); $containerBuilder->addDefinitions(__DIR__ . '/autoload/errorhandler.local.php'); $containerBuilder->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.global.php'); From 9d34e434c30356ffb888305300d07c5fe7e7e81d Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 18:57:38 +0100 Subject: [PATCH 06/19] Remove local / global --- .../autoload/{dependencies.global.php => dependencies.php} | 0 .../autoload/{errorhandler.local.php => errorHandler.php} | 1 - ...dleware-pipeline.global.php => middleware-pipeline.php} | 0 config/container.php | 7 +++---- 4 files changed, 3 insertions(+), 5 deletions(-) rename config/autoload/{dependencies.global.php => dependencies.php} (100%) rename config/autoload/{errorhandler.local.php => errorHandler.php} (99%) rename config/autoload/{middleware-pipeline.global.php => middleware-pipeline.php} (100%) diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.php similarity index 100% rename from config/autoload/dependencies.global.php rename to config/autoload/dependencies.php diff --git a/config/autoload/errorhandler.local.php b/config/autoload/errorHandler.php similarity index 99% rename from config/autoload/errorhandler.local.php rename to config/autoload/errorHandler.php index 32be2dd..73a00d1 100644 --- a/config/autoload/errorhandler.local.php +++ b/config/autoload/errorHandler.php @@ -10,5 +10,4 @@ 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), 'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), //'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class), //In production - ); \ No newline at end of file diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.php similarity index 100% rename from config/autoload/middleware-pipeline.global.php rename to config/autoload/middleware-pipeline.php diff --git a/config/container.php b/config/container.php index 6023e4c..84eab2c 100644 --- a/config/container.php +++ b/config/container.php @@ -1,11 +1,10 @@ addDefinitions(__DIR__ . '/autoload/dependencies.global.php'); -$containerBuilder->addDefinitions(__DIR__ . '/autoload/errorhandler.local.php'); -$containerBuilder->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.global.php'); +$containerBuilder->addDefinitions(__DIR__ . '/autoload/dependencies.php'); +$containerBuilder->addDefinitions(__DIR__ . '/autoload/errorHandler.php'); +$containerBuilder->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.php'); $this->configureContainer($containerBuilder); return $containerBuilder->build(); From af2e3390f0d3bed0914ddf9d049a728047c6916f Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 19:03:30 +0100 Subject: [PATCH 07/19] Use proper type to allow $container->set() --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac85d5d..c3f3a22 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ if (php_sapi_name() === 'cli-server' require __DIR__ . '/../vendor/autoload.php'; -/** @var \Interop\Container\ContainerInterface $container */ +/** @var \DI\Container $container */ $container = require __DIR__ . '/../vendor/php-di/zend-expressive-bridge/config/container.php'; //Assign configuration to container From 58b53c15497dbe4c877e687e927bda9b20e8c778 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 19:16:20 +0100 Subject: [PATCH 08/19] Return ContainerBuilder --- README.md | 18 +++++++++++++++--- composer.json | 3 +++ config/containerBuilder.php | 7 +++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 config/containerBuilder.php diff --git a/README.md b/README.md index c3f3a22..57efbd7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Index.php writeProxiesToFile($inProduction, __DIR__ . '/../data/cache'); //You probably want to use caching in production + +//Add your own application-specific Dependency Definitions to the Container Builder +$pathToDependencyDefinitions = __DIR__ . '/../config/dependencies/{{,*.}global,{,*.}local}.php'; +$phpFileProvider = new \Zend\Expressive\ConfigManager\PhpFileProvider($pathToDependencyDefinitions); +$dependencyDefinitions = $phpFileProvider(); +foreach ($dependencyDefinitions as $definitions) { + $containerBuilder->addDefinitions($definitions); +} + +$container = $containerBuilder->build(); //Assign configuration to container $config = require __DIR__ . '/../config.php'; diff --git a/composer.json b/composer.json index 7626741..ff7484d 100644 --- a/composer.json +++ b/composer.json @@ -11,5 +11,8 @@ "zendframework/zend-expressive-helpers": "^2.2", "zendframework/zend-expressive-fastroute": "^1.3", "zendframework/zend-expressive-zendviewrenderer": "^1.1" + }, + "suggest": { + "mtymek/expressive-config-manager": "To easily add application specific Dependency Definitions to the ContainerBuilder" } } \ No newline at end of file diff --git a/config/containerBuilder.php b/config/containerBuilder.php new file mode 100644 index 0000000..d5b6141 --- /dev/null +++ b/config/containerBuilder.php @@ -0,0 +1,7 @@ +addDefinitions(__DIR__ . '/autoload/dependencies.php') + ->addDefinitions(__DIR__ . '/autoload/errorHandler.php') + ->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.php'); From 9d4a8317b5ec11d20b97bf4b73c25faf8313b4c1 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 19:16:36 +0100 Subject: [PATCH 09/19] Return ContainerBuilder --- config/container.php | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 config/container.php diff --git a/config/container.php b/config/container.php deleted file mode 100644 index 84eab2c..0000000 --- a/config/container.php +++ /dev/null @@ -1,10 +0,0 @@ -addDefinitions(__DIR__ . '/autoload/dependencies.php'); -$containerBuilder->addDefinitions(__DIR__ . '/autoload/errorHandler.php'); -$containerBuilder->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.php'); -$this->configureContainer($containerBuilder); - -return $containerBuilder->build(); From 809452b5bb7c9f35e3c4139150a1812bae4293a4 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 19:30:08 +0100 Subject: [PATCH 10/19] Suggest use of ./config/container.php --- README.md | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 57efbd7..3df852b 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,16 @@ The easiest way to use PHP-DI with Zend Expressive, seems to: - - assemble the Dependency Injection Container using some default definition files - - use the DIC to get the Zend Expressive Application + - use the Dependency Injection Container Builder provided by this library using some default definition files + - add your own Dependency Definitions to the Container Builder + - use the Dependency Injection Container to fetch the Zend Expressive Application - run the Application -Index.php +## In your ```./public/index.php``` ```php get(\Zend\Expressive\Application::class); +$app->run(); + +``` + +## In your ```./config/container.php``` + + +```php +writeProxiesToFile($inProduction, __DIR__ . '/../data/cache'); //You probably want to use caching in production @@ -40,8 +54,5 @@ $container = $containerBuilder->build(); $config = require __DIR__ . '/../config.php'; $container->set('config', $config); -/** @var \Zend\Expressive\Application $app */ -$app = $container->get(\Zend\Expressive\Application::class); -$app->run(); - +return $container; ``` From 5c1e295b566e84d9ca605048b7c494d2471e6648 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 20:17:28 +0100 Subject: [PATCH 11/19] Do not rely on one specific routing / templating approach --- composer.json | 16 +++++++----- config/autoload/dependencies.php | 9 ------- config/autoload/dependencies.router.php | 22 +++++++++++++++++ config/autoload/dependencies.templating.php | 27 +++++++++++++++++++++ config/autoload/errorHandler.php | 1 - 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 config/autoload/dependencies.router.php create mode 100644 config/autoload/dependencies.templating.php diff --git a/composer.json b/composer.json index ff7484d..4280fb5 100644 --- a/composer.json +++ b/composer.json @@ -5,14 +5,18 @@ "type": "library", "require": { "php": "~5.5|~7.0", - "php-di/php-di": "^5.2.0", - "php-di/invoker": "^1.2.0", + "php-di/php-di": "^5.4.0", + "php-di/invoker": "^1.3.0", "zendframework/zend-expressive": "^1.0", - "zendframework/zend-expressive-helpers": "^2.2", - "zendframework/zend-expressive-fastroute": "^1.3", - "zendframework/zend-expressive-zendviewrenderer": "^1.1" + "zendframework/zend-expressive-helpers": "^2.2" }, "suggest": { - "mtymek/expressive-config-manager": "To easily add application specific Dependency Definitions to the ContainerBuilder" + "mtymek/expressive-config-manager": "To easily add application specific Dependency Definitions to the ContainerBuilder", + "zendframework/zend-expressive-aurarouter": "To route requests using Aura.Router", + "zendframework/zend-expressive-fastroute": "To route requests using FastRoute", + "zendframework/zend-expressive-zendrouter": "To route requests using zend-mvc Router", + "zendframework/zend-expressive-platesrenderer": "To use Plates as templating engine", + "zendframework/zend-expressive-twigrenderer": "To use Twig as templating engine", + "zendframework/zend-expressive-zendviewrenderer": "To use zend-view PhpRenderer as templating engine" } } \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index 4e94148..dd7e09b 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -6,16 +6,7 @@ return $container; }, \Zend\Expressive\Application::class => \DI\factory(\Zend\Expressive\Container\ApplicationFactory::class), - \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), - \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), \Zend\Expressive\Container\ApplicationFactory::class => \DI\object(), - /** - * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: - * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in - * - * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() - */ - \Zend\View\HelperPluginManager::class => \DI\factory(\Zend\Expressive\ZendView\HelperPluginManagerFactory::class), \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\object(), \Zend\Expressive\Helper\UrlHelper::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperFactory::class), ); \ No newline at end of file diff --git a/config/autoload/dependencies.router.php b/config/autoload/dependencies.router.php new file mode 100644 index 0000000..0d73c0b --- /dev/null +++ b/config/autoload/dependencies.router.php @@ -0,0 +1,22 @@ + \DI\get(\Zend\Expressive\Router\AuraRouter::class), + \Zend\Expressive\Router\AuraRouter::class => \DI\object(), + ); +} +if(class_exists('Zend\Expressive\Router\FastRouteRouter')){ + return array( + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), + \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), + ); +} +if(class_exists('Zend\Expressive\Router\Zf2Router')){ + return array( + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\Zf2Router::class), + \Zend\Expressive\Router\Zf2Router::class => \DI\object(), + ); +} +throw new RuntimeException('No Router available to use in Dependency Injection Container'); diff --git a/config/autoload/dependencies.templating.php b/config/autoload/dependencies.templating.php new file mode 100644 index 0000000..0fdc7c1 --- /dev/null +++ b/config/autoload/dependencies.templating.php @@ -0,0 +1,27 @@ + \DI\factory(\Zend\Expressive\Plates\PlatesRendererFactory::class), + ); +} +if(class_exists('Zend\Expressive\Twig\TwigRendererFactory')){ + return array( + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Twig\TwigRendererFactory::class), + ); +} +if(class_exists('Zend\Expressive\ZendView\ZendViewRendererFactory')){ + return array( + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\ZendView\ZendViewRendererFactory::class), + /** + * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: + * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in + * + * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() + */ + \Zend\View\HelperPluginManager::class => \DI\factory(\Zend\Expressive\ZendView\HelperPluginManagerFactory::class), + ); +} + +return array(); //Template engine is not mandatory diff --git a/config/autoload/errorHandler.php b/config/autoload/errorHandler.php index 73a00d1..aaaf758 100644 --- a/config/autoload/errorHandler.php +++ b/config/autoload/errorHandler.php @@ -5,7 +5,6 @@ \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => \DI\object()->lazy(), \Zend\Expressive\Container\WhoopsFactory::class => \DI\object()->lazy(), \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\object()->lazy(), - \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(Zend\Expressive\ZendView\ZendViewRendererFactory::class), 'Zend\Expressive\Whoops' => \DI\factory(\Zend\Expressive\Container\WhoopsFactory::class), 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), 'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), From 3706c4d9798ef8dc70fad3779dacfe99514c33c6 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 20:18:06 +0100 Subject: [PATCH 12/19] Do not rely on one specific routing / templating approach --- config/containerBuilder.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/containerBuilder.php b/config/containerBuilder.php index d5b6141..a9566df 100644 --- a/config/containerBuilder.php +++ b/config/containerBuilder.php @@ -3,5 +3,7 @@ return (new \DI\ContainerBuilder()) ->addDefinitions(__DIR__ . '/autoload/dependencies.php') + ->addDefinitions(__DIR__ . '/autoload/dependencies.router.php') + ->addDefinitions(__DIR__ . '/autoload/dependencies.templating.php') ->addDefinitions(__DIR__ . '/autoload/errorHandler.php') ->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.php'); From f79fac9c992c053796fe6c4ac84ed2a98ee863e0 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 21 Jan 2017 20:27:19 +0100 Subject: [PATCH 13/19] Use proper / new name for Zend Router --- config/autoload/dependencies.router.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/autoload/dependencies.router.php b/config/autoload/dependencies.router.php index 0d73c0b..ba1cdae 100644 --- a/config/autoload/dependencies.router.php +++ b/config/autoload/dependencies.router.php @@ -13,10 +13,10 @@ \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), ); } -if(class_exists('Zend\Expressive\Router\Zf2Router')){ +if(class_exists('Zend\Expressive\Router\ZendRouter')){ return array( - \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\Zf2Router::class), - \Zend\Expressive\Router\Zf2Router::class => \DI\object(), + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\ZendRouter::class), + \Zend\Expressive\Router\ZendRouter::class => \DI\object(), ); } throw new RuntimeException('No Router available to use in Dependency Injection Container'); From faa7ae18d307908de166cdd273694c490afae3f8 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Tue, 7 Mar 2017 20:56:18 +0100 Subject: [PATCH 14/19] Relax constraints to stable versions --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 4280fb5..9d868d5 100644 --- a/composer.json +++ b/composer.json @@ -4,11 +4,11 @@ "license": "MIT", "type": "library", "require": { - "php": "~5.5|~7.0", - "php-di/php-di": "^5.4.0", - "php-di/invoker": "^1.3.0", - "zendframework/zend-expressive": "^1.0", - "zendframework/zend-expressive-helpers": "^2.2" + "php": "@stable", + "php-di/php-di": "@stable", + "php-di/invoker": "@stable", + "zendframework/zend-expressive": "@stable", + "zendframework/zend-expressive-helpers": "@stable" }, "suggest": { "mtymek/expressive-config-manager": "To easily add application specific Dependency Definitions to the ContainerBuilder", From 2c24eaa7bed60c508342209a0dad790b2586e42e Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Wed, 8 Mar 2017 10:16:44 +0100 Subject: [PATCH 15/19] Use new Zend ConfigAggregator --- README.md | 7 +++++-- composer.json | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3df852b..15670e3 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,12 @@ $containerBuilder = require __DIR__ . '/../vendor/php-di/zend-expressive-bridge/ $inProduction = false; //You probably want to use an environment variable for this... $containerBuilder->writeProxiesToFile($inProduction, __DIR__ . '/../data/cache'); //You probably want to use caching in production -//Add your own application-specific Dependency Definitions to the Container Builder +/** + * Add your own, application-specific, Dependency Definitions to the Container Builder + * @link https://zend-expressive.readthedocs.io/en/latest/features/modular-applications/ + */ $pathToDependencyDefinitions = __DIR__ . '/../config/dependencies/{{,*.}global,{,*.}local}.php'; -$phpFileProvider = new \Zend\Expressive\ConfigManager\PhpFileProvider($pathToDependencyDefinitions); +$phpFileProvider = new \Zend\ConfigAggregator\PhpFileProvider($pathToDependencyDefinitions); $dependencyDefinitions = $phpFileProvider(); foreach ($dependencyDefinitions as $definitions) { $containerBuilder->addDefinitions($definitions); diff --git a/composer.json b/composer.json index 9d868d5..a958dd3 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "zendframework/zend-expressive-helpers": "@stable" }, "suggest": { - "mtymek/expressive-config-manager": "To easily add application specific Dependency Definitions to the ContainerBuilder", + "zendframework/zend-config-aggregator": "To easily add application specific Dependency Definitions to the ContainerBuilder", "zendframework/zend-expressive-aurarouter": "To route requests using Aura.Router", "zendframework/zend-expressive-fastroute": "To route requests using FastRoute", "zendframework/zend-expressive-zendrouter": "To route requests using zend-mvc Router", From a1cea8285214690d818c3cb640ad90bafcc52926 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sun, 12 Mar 2017 13:27:42 +0100 Subject: [PATCH 16/19] Use single quotes for inline code blocks --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 15670e3..7662e3a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ The easiest way to use PHP-DI with Zend Expressive, seems to: - use the Dependency Injection Container to fetch the Zend Expressive Application - run the Application -## In your ```./public/index.php``` +## In your `./public/index.php` ```php @@ -30,7 +30,7 @@ $app->run(); ``` -## In your ```./config/container.php``` +## In your `./config/container.php` ```php From 2de17046408dbda156aaf9e310f9218a433e7c83 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 8 Apr 2017 21:07:49 +0200 Subject: [PATCH 17/19] Use ExpressiveContainerBuilder instead of separate configuration files --- README.md | 11 +- composer.json | 13 ++- config/autoload/dependencies.php | 12 --- config/autoload/dependencies.router.php | 22 ---- config/autoload/dependencies.templating.php | 27 ----- config/autoload/errorHandler.php | 12 --- config/autoload/middleware-pipeline.php | 8 -- config/containerBuilder.php | 9 -- src/DI/ExpressiveContainerBuilder.php | 106 ++++++++++++++++++++ 9 files changed, 120 insertions(+), 100 deletions(-) delete mode 100644 config/autoload/dependencies.php delete mode 100644 config/autoload/dependencies.router.php delete mode 100644 config/autoload/dependencies.templating.php delete mode 100644 config/autoload/errorHandler.php delete mode 100644 config/autoload/middleware-pipeline.php delete mode 100644 config/containerBuilder.php create mode 100644 src/DI/ExpressiveContainerBuilder.php diff --git a/README.md b/README.md index 7662e3a..b63f9e1 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ The easiest way to use PHP-DI with Zend Expressive, seems to: - add your own Dependency Definitions to the Container Builder - use the Dependency Injection Container to fetch the Zend Expressive Application - run the Application + - for example using the PHP internal WebServer: `php -S 0.0.0.0:8080 -t public/` ## In your `./public/index.php` @@ -13,9 +14,7 @@ The easiest way to use PHP-DI with Zend Expressive, seems to: get(\Zend\Expressive\Application::class); $app->run(); ``` - ## In your `./config/container.php` ```php registerXYZRouter(); //Choose your preferred Router +$containerBuilder->registerXYZRenderer(); //Choose your preferred template Renderer $containerBuilder->writeProxiesToFile($inProduction, __DIR__ . '/../data/cache'); //You probably want to use caching in production /** diff --git a/composer.json b/composer.json index a958dd3..89dcdf1 100644 --- a/composer.json +++ b/composer.json @@ -5,10 +5,15 @@ "type": "library", "require": { "php": "@stable", - "php-di/php-di": "@stable", - "php-di/invoker": "@stable", - "zendframework/zend-expressive": "@stable", - "zendframework/zend-expressive-helpers": "@stable" + "php-di/php-di": "^5.4", + "php-di/invoker": "^1.3", + "zendframework/zend-expressive": "^2.0", + "zendframework/zend-expressive-helpers": "^4.0" + }, + "autoload": { + "psr-4": { + "DI\\": "src/DI" + } }, "suggest": { "zendframework/zend-config-aggregator": "To easily add application specific Dependency Definitions to the ContainerBuilder", diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php deleted file mode 100644 index dd7e09b..0000000 --- a/config/autoload/dependencies.php +++ /dev/null @@ -1,12 +0,0 @@ - function (\Interop\Container\ContainerInterface $container) { - return $container; - }, - \Zend\Expressive\Application::class => \DI\factory(\Zend\Expressive\Container\ApplicationFactory::class), - \Zend\Expressive\Container\ApplicationFactory::class => \DI\object(), - \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\object(), - \Zend\Expressive\Helper\UrlHelper::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperFactory::class), -); \ No newline at end of file diff --git a/config/autoload/dependencies.router.php b/config/autoload/dependencies.router.php deleted file mode 100644 index ba1cdae..0000000 --- a/config/autoload/dependencies.router.php +++ /dev/null @@ -1,22 +0,0 @@ - \DI\get(\Zend\Expressive\Router\AuraRouter::class), - \Zend\Expressive\Router\AuraRouter::class => \DI\object(), - ); -} -if(class_exists('Zend\Expressive\Router\FastRouteRouter')){ - return array( - \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), - \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), - ); -} -if(class_exists('Zend\Expressive\Router\ZendRouter')){ - return array( - \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\ZendRouter::class), - \Zend\Expressive\Router\ZendRouter::class => \DI\object(), - ); -} -throw new RuntimeException('No Router available to use in Dependency Injection Container'); diff --git a/config/autoload/dependencies.templating.php b/config/autoload/dependencies.templating.php deleted file mode 100644 index 0fdc7c1..0000000 --- a/config/autoload/dependencies.templating.php +++ /dev/null @@ -1,27 +0,0 @@ - \DI\factory(\Zend\Expressive\Plates\PlatesRendererFactory::class), - ); -} -if(class_exists('Zend\Expressive\Twig\TwigRendererFactory')){ - return array( - \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Twig\TwigRendererFactory::class), - ); -} -if(class_exists('Zend\Expressive\ZendView\ZendViewRendererFactory')){ - return array( - \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\ZendView\ZendViewRendererFactory::class), - /** - * injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: - * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in - * - * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() - */ - \Zend\View\HelperPluginManager::class => \DI\factory(\Zend\Expressive\ZendView\HelperPluginManagerFactory::class), - ); -} - -return array(); //Template engine is not mandatory diff --git a/config/autoload/errorHandler.php b/config/autoload/errorHandler.php deleted file mode 100644 index aaaf758..0000000 --- a/config/autoload/errorHandler.php +++ /dev/null @@ -1,12 +0,0 @@ - \DI\object()->lazy(), - \Zend\Expressive\Container\WhoopsFactory::class => \DI\object()->lazy(), - \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\object()->lazy(), - 'Zend\Expressive\Whoops' => \DI\factory(\Zend\Expressive\Container\WhoopsFactory::class), - 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), - 'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), - //'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class), //In production -); \ No newline at end of file diff --git a/config/autoload/middleware-pipeline.php b/config/autoload/middleware-pipeline.php deleted file mode 100644 index dd43e9c..0000000 --- a/config/autoload/middleware-pipeline.php +++ /dev/null @@ -1,8 +0,0 @@ - \DI\factory(\Zend\Expressive\Helper\ServerUrlMiddlewareFactory::class), - \Zend\Expressive\Helper\UrlHelperMiddleware::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperMiddlewareFactory::class), -); \ No newline at end of file diff --git a/config/containerBuilder.php b/config/containerBuilder.php deleted file mode 100644 index a9566df..0000000 --- a/config/containerBuilder.php +++ /dev/null @@ -1,9 +0,0 @@ -addDefinitions(__DIR__ . '/autoload/dependencies.php') - ->addDefinitions(__DIR__ . '/autoload/dependencies.router.php') - ->addDefinitions(__DIR__ . '/autoload/dependencies.templating.php') - ->addDefinitions(__DIR__ . '/autoload/errorHandler.php') - ->addDefinitions(__DIR__ . '/autoload/middleware-pipeline.php'); diff --git a/src/DI/ExpressiveContainerBuilder.php b/src/DI/ExpressiveContainerBuilder.php new file mode 100644 index 0000000..5c0b0b5 --- /dev/null +++ b/src/DI/ExpressiveContainerBuilder.php @@ -0,0 +1,106 @@ +registerDependencies(); + $this->registerErrorHandler($inProduction); + $this->registerMiddlewarePipeline(); + } + + private function registerDependencies() + { + $this->addDefinitions([ + \Interop\Container\ContainerInterface::class => function (\Interop\Container\ContainerInterface $container) { + return $container; + }, + \Zend\Expressive\Application::class => \DI\factory(\Zend\Expressive\Container\ApplicationFactory::class), + \Zend\Expressive\Container\ApplicationFactory::class => \DI\object(), + \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\object(), + \Zend\Expressive\Helper\UrlHelper::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperFactory::class), + ]); + } + + private function registerErrorHandler($inProduction) + { + $this->addDefinitions([ + \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Container\WhoopsFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\object()->lazy(), + 'Zend\Expressive\Whoops' => \DI\factory(\Zend\Expressive\Container\WhoopsFactory::class), + 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), + 'Zend\Expressive\FinalHandler' => $inProduction ? \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class) : \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), + ]); + } + + public function registerAuraRouter() + { + $this->addDefinitions([ + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\AuraRouter::class), + \Zend\Expressive\Router\AuraRouter::class => \DI\object(), + ]); + } + + public function registerFastRouteRouter() + { + $this->addDefinitions([ + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), + \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), + ]); + } + + public function registerZendRouter() + { + $this->addDefinitions([ + \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\ZendRouter::class), + \Zend\Expressive\Router\ZendRouter::class => \DI\object(), + ]); + } + + public function registerPlatesRenderer() + { + $this->addDefinitions([ + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Plates\PlatesRendererFactory::class), + ]); + } + + public function registerTwigRenderer() + { + $this->addDefinitions([ + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Twig\TwigRendererFactory::class), + ]); + } + + /** + * Note that ZendViewRendererFactory::injectHelpers() seems to use an incorrect 'default' HelperPluginManager resulting in: + * Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in + * + * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() + */ + public function registerZendViewRenderer() + { + $this->addDefinitions([ + \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\ZendView\ZendViewRendererFactory::class), + \Zend\View\HelperPluginManager::class => \DI\factory(\Zend\Expressive\ZendView\HelperPluginManagerFactory::class), + ]); + } + + private function registerMiddlewarePipeline() + { + $this->addDefinitions([ + \Zend\Expressive\Helper\ServerUrlMiddleware::class => \DI\factory(\Zend\Expressive\Helper\ServerUrlMiddlewareFactory::class), + \Zend\Expressive\Helper\UrlHelperMiddleware::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperMiddlewareFactory::class), + ]); + } +} From e057742b4029e6dce9f544065fc6ce633aef44c4 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 27 Oct 2018 20:08:46 +0200 Subject: [PATCH 18/19] Bump versions --- composer.json | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 89dcdf1..e07de3e 100644 --- a/composer.json +++ b/composer.json @@ -4,11 +4,12 @@ "license": "MIT", "type": "library", "require": { - "php": "@stable", - "php-di/php-di": "^5.4", - "php-di/invoker": "^1.3", + "php": "^7.0", + "php-di/php-di": "^6.0", "zendframework/zend-expressive": "^2.0", - "zendframework/zend-expressive-helpers": "^4.0" + "zendframework/zend-expressive-helpers": "^4.0", + "http-interop/http-server-middleware": "^1.0", + "http-interop/http-middleware": "^0.5.0" }, "autoload": { "psr-4": { @@ -24,4 +25,4 @@ "zendframework/zend-expressive-twigrenderer": "To use Twig as templating engine", "zendframework/zend-expressive-zendviewrenderer": "To use zend-view PhpRenderer as templating engine" } -} \ No newline at end of file +} From e2be5aadae96212d0be8a8416576566c5d69e702 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 27 Oct 2018 20:20:31 +0200 Subject: [PATCH 19/19] Bump versions --- composer.json | 2 +- src/DI/ExpressiveContainerBuilder.php | 41 ++++++++++++--------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index e07de3e..b619ef1 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MIT", "type": "library", "require": { - "php": "^7.0", + "php": "^7.1", "php-di/php-di": "^6.0", "zendframework/zend-expressive": "^2.0", "zendframework/zend-expressive-helpers": "^4.0", diff --git a/src/DI/ExpressiveContainerBuilder.php b/src/DI/ExpressiveContainerBuilder.php index 5c0b0b5..c747ca4 100644 --- a/src/DI/ExpressiveContainerBuilder.php +++ b/src/DI/ExpressiveContainerBuilder.php @@ -5,12 +5,7 @@ class ExpressiveContainerBuilder extends ContainerBuilder { - - /** - * @param bool $inProduction - * @param string $containerClass - */ - public function __construct($inProduction = false, $containerClass = 'DI\Container') + public function __construct(bool $inProduction = false, string $containerClass = 'DI\Container') { parent::__construct($containerClass); @@ -19,63 +14,63 @@ public function __construct($inProduction = false, $containerClass = 'DI\Contain $this->registerMiddlewarePipeline(); } - private function registerDependencies() + private function registerDependencies(): void { $this->addDefinitions([ \Interop\Container\ContainerInterface::class => function (\Interop\Container\ContainerInterface $container) { return $container; }, \Zend\Expressive\Application::class => \DI\factory(\Zend\Expressive\Container\ApplicationFactory::class), - \Zend\Expressive\Container\ApplicationFactory::class => \DI\object(), - \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\object(), + \Zend\Expressive\Container\ApplicationFactory::class => \DI\autowire(), + \Zend\Expressive\Helper\ServerUrlHelper::class => \DI\autowire(), \Zend\Expressive\Helper\UrlHelper::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperFactory::class), ]); } - private function registerErrorHandler($inProduction) + private function registerErrorHandler(bool $inProduction): void { $this->addDefinitions([ - \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => \DI\object()->lazy(), - \Zend\Expressive\Container\WhoopsFactory::class => \DI\object()->lazy(), - \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\object()->lazy(), + \Zend\Expressive\Container\WhoopsErrorHandlerFactory::class => \DI\autowire()->lazy(), + \Zend\Expressive\Container\WhoopsFactory::class => \DI\autowire()->lazy(), + \Zend\Expressive\Container\WhoopsPageHandlerFactory::class => \DI\autowire()->lazy(), 'Zend\Expressive\Whoops' => \DI\factory(\Zend\Expressive\Container\WhoopsFactory::class), 'Zend\Expressive\WhoopsPageHandler' => \DI\factory(\Zend\Expressive\Container\WhoopsPageHandlerFactory::class), 'Zend\Expressive\FinalHandler' => $inProduction ? \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class) : \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), ]); } - public function registerAuraRouter() + public function registerAuraRouter(): void { $this->addDefinitions([ \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\AuraRouter::class), - \Zend\Expressive\Router\AuraRouter::class => \DI\object(), + \Zend\Expressive\Router\AuraRouter::class => \DI\autowire(), ]); } - public function registerFastRouteRouter() + public function registerFastRouteRouter(): void { $this->addDefinitions([ \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\FastRouteRouter::class), - \Zend\Expressive\Router\FastRouteRouter::class => \DI\object(), + \Zend\Expressive\Router\FastRouteRouter::class => \DI\autowire(), ]); } - public function registerZendRouter() + public function registerZendRouter(): void { $this->addDefinitions([ \Zend\Expressive\Router\RouterInterface::class => \DI\get(\Zend\Expressive\Router\ZendRouter::class), - \Zend\Expressive\Router\ZendRouter::class => \DI\object(), + \Zend\Expressive\Router\ZendRouter::class => \DI\autowire(), ]); } - public function registerPlatesRenderer() + public function registerPlatesRenderer(): void { $this->addDefinitions([ \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Plates\PlatesRendererFactory::class), ]); } - public function registerTwigRenderer() + public function registerTwigRenderer(): void { $this->addDefinitions([ \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\Twig\TwigRendererFactory::class), @@ -88,7 +83,7 @@ public function registerTwigRenderer() * * @see \Zend\Expressive\ZendView\ZendViewRendererFactory::injectHelpers() */ - public function registerZendViewRenderer() + public function registerZendViewRenderer(): void { $this->addDefinitions([ \Zend\Expressive\Template\TemplateRendererInterface::class => \DI\factory(\Zend\Expressive\ZendView\ZendViewRendererFactory::class), @@ -96,7 +91,7 @@ public function registerZendViewRenderer() ]); } - private function registerMiddlewarePipeline() + private function registerMiddlewarePipeline(): void { $this->addDefinitions([ \Zend\Expressive\Helper\ServerUrlMiddleware::class => \DI\factory(\Zend\Expressive\Helper\ServerUrlMiddlewareFactory::class),