-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial approach #1
base: master
Are you sure you want to change the base?
Changes from 16 commits
be8479b
5d8657d
bdfbe80
0ef0f37
a848108
0d674fa
9d34e43
af2e339
58b53c1
9d4a831
809452b
5c1e295
3706c4d
f79fac9
faa7ae1
2c24eaa
a1cea82
2de1704
e057742
e2be5aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,61 @@ | ||
# Zend-Expressive-Bridge | ||
# Zend-Expressive-Bridge | ||
|
||
The easiest way to use PHP-DI with Zend Expressive, seems to: | ||
|
||
- 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 | ||
|
||
## In your ```./public/index.php``` | ||
|
||
```php | ||
|
||
<?php | ||
// Delegate static file requests back to the PHP built-in WebServer | ||
if (php_sapi_name() === 'cli-server' | ||
&& is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) | ||
) { | ||
return false; | ||
} | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
/* @var \Interop\Container\ContainerInterface $container */ | ||
$container = require __DIR__ . '/../config/container.php'; | ||
|
||
/* @var \Zend\Expressive\Application $app */ | ||
$app = $container->get(\Zend\Expressive\Application::class); | ||
$app->run(); | ||
|
||
``` | ||
|
||
## In your ```./config/container.php``` | ||
|
||
|
||
```php | ||
<?php | ||
/* @var \DI\ContainerBuilder $containerBuilder */ | ||
$containerBuilder = require __DIR__ . '/../vendor/php-di/zend-expressive-bridge/config/containerBuilder.php'; | ||
$inProduction = false; //You probably want to use an environment variable for this... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is an idea: instead of requiring the The advantage I see is it benefits from autoloading so it avoids the long line of include that goes looking in Here is an example of what it could look like: // the $inProduction could be used to enable/disable Twig debug or other debug for example
$containerBuilder = new ExpressiveContainerBuilder($inProduction);
$containerBuilder->registerAuraRouter();
$containerBuilder->registerTwigRenderer();
// All methods of the container builder can still be used of course
$containerBuilder->addDefinitions(/* your own definition files */); It's a bit less automatic than your current solution but relying on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mm, seems better indeed, will have a look at 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 | ||
* @link https://zend-expressive.readthedocs.io/en/latest/features/modular-applications/ | ||
*/ | ||
$pathToDependencyDefinitions = __DIR__ . '/../config/dependencies/{{,*.}global,{,*.}local}.php'; | ||
$phpFileProvider = new \Zend\ConfigAggregator\PhpFileProvider($pathToDependencyDefinitions); | ||
$dependencyDefinitions = $phpFileProvider(); | ||
foreach ($dependencyDefinitions as $definitions) { | ||
$containerBuilder->addDefinitions($definitions); | ||
} | ||
|
||
$container = $containerBuilder->build(); | ||
|
||
//Assign configuration to container | ||
$config = require __DIR__ . '/../config.php'; | ||
$container->set('config', $config); | ||
|
||
return $container; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "php-di/zend-expressive-bridge", | ||
"description": "PHP-DI integration in Zend Expressive", | ||
"license": "MIT", | ||
"type": "library", | ||
"require": { | ||
"php": "@stable", | ||
"php-di/php-di": "@stable", | ||
"php-di/invoker": "@stable", | ||
"zendframework/zend-expressive": "@stable", | ||
"zendframework/zend-expressive-helpers": "@stable" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to restrict to major versions to avoid being affected by a BC break in any of those packages. Random example: the class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
}, | ||
"suggest": { | ||
"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", | ||
"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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
return array( | ||
\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), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
if(class_exists('Zend\Expressive\Router\AuraRouter')){ | ||
return array( | ||
\Zend\Expressive\Router\RouterInterface::class => \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'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
if(class_exists('Zend\Expressive\Plates\PlatesRendererFactory')){ | ||
return array( | ||
\Zend\Expressive\Template\TemplateRendererInterface::class => \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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
return array( | ||
\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' => \DI\factory(\Zend\Expressive\Container\WhoopsErrorHandlerFactory::class), | ||
//'Zend\Expressive\FinalHandler' => \DI\factory(\Zend\Expressive\Container\TemplatedErrorHandlerFactory::class), //In production | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
return array( | ||
\Zend\Expressive\Helper\ServerUrlMiddleware::class => \DI\factory(\Zend\Expressive\Helper\ServerUrlMiddlewareFactory::class), | ||
\Zend\Expressive\Helper\UrlHelperMiddleware::class => \DI\factory(\Zend\Expressive\Helper\UrlHelperMiddlewareFactory::class), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline code blocks are made with single quotes: "In your
./public/index.php
"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, did not know that...