Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 18e813f

Browse files
committed
Merge branch 'hotfix/542-container-docs' into release-3.0.0
Close #542
2 parents dc1ae72 + 4274c33 commit 18e813f

File tree

5 files changed

+53
-188
lines changed

5 files changed

+53
-188
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ All notable changes to this project will be documented in this file, in reverse
2424
The related factories now consume these services in order to receive a
2525
response prototype for the services they produce.
2626

27+
- [#542](https://github.com/zendframework/zend-expressive/pull/542) modifies the
28+
`composer.json` to no longer suggest the pimple/pimple package, but rather the
29+
zendframework/zend-pimple-config package.
30+
31+
- [#542](https://github.com/zendframework/zend-expressive/pull/542) modifies the
32+
`composer.json` to no longer suggest the aura/di package, but rather the
33+
zendframework/zend-auradi-config package.
34+
2735
### Deprecated
2836

2937
- Nothing.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ can recommend the following implementations:
7373

7474
- [zend-servicemanager](https://github.com/zendframework/zend-servicemanager):
7575
`composer require zendframework/zend-servicemanager`
76-
- [Pimple](https://github.com/silexphp/Pimple):
77-
`composer require pimple/pimple`
78-
- [Aura.Di](https://github.com/auraphp/Aura.Di):
79-
`composer require aura/di`
76+
- [Pimple](https://github.com/silexphp/Pimple) (see [docs](docs/book/features/container/pimple.md) for more details):
77+
`composer require zendframework/zend-pimple-config`
78+
- [Aura.Di](https://github.com/auraphp/Aura.Di) (see [docs](docs/book/features/container/aura-di.md) for more details):
79+
`composer require zendframework/zend-auradi-config`
8080

8181
Additionally, you may optionally want to install a template renderer
8282
implementation, and/or an error handling integration. These are covered in the

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
},
5050
"suggest": {
5151
"filp/whoops": "^2.1 to use the Whoops error handler",
52+
"zendframework/zend-auradi-config": "^1.0 to use Aura.Di dependency injection container",
5253
"zendframework/zend-expressive-helpers": "^3.0 for its UrlHelper, ServerUrlHelper, and BodyParseMiddleware",
53-
"aura/di": "^3.2 to make use of Aura.Di dependency injection container",
54-
"pimple/pimple": "^3.2.2 to use Pimple for dependency injection",
5554
"zendframework/zend-expressive-tooling": "For migration and development tools; require it with the --dev flag",
55+
"zendframework/zend-pimple-config": "^1.0 to use Pimple for dependency injection container",
5656
"zendframework/zend-servicemanager": "^3.3 to use zend-servicemanager for dependency injection"
5757
},
5858
"autoload": {

docs/book/features/container/aura-di.md

Lines changed: 17 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,127 +11,45 @@ injection container with the following features:
1111
- instance factories.
1212
- optional auto-resolution of typehinted constructor parameter values.
1313

14-
## Installing Aura.Di
14+
## Installing and configuring Aura.Di
1515

1616
Aura.Di implements [container-interop](https://github.com/container-interop/container-interop)
17-
as of version 3.
17+
as of version 3. To use Aura.Di as a dependency injection container, we
18+
recommend using [zendframework/zend-auradi-config](https://github.com/zendframework/zend-auradi-config),
19+
which helps you to configure its container. First install the package:
1820

1921
```bash
20-
$ composer require aura/di
22+
$ composer require zendframework/zend-auradi-config
2123
```
2224

23-
## Configuration
24-
25-
Aura.Di can help you to organize your code better with
26-
[ContainerConfig classes](http://auraphp.com/packages/Aura.Di/config.html) and
27-
[two step configuration](http://auraphp.com/blog/2014/04/07/two-stage-config/).
28-
In this example, we'll put that in `config/container.php`:
25+
To configure Aura.Di, create the file `config/container.php` with the following
26+
contents:
2927

3028
```php
3129
<?php
32-
use Aura\Di\ContainerBuilder;
30+
use Zend\AuraDi\Config\Config;
31+
use Zend\AuraDi\Config\ContainerFactory;
3332

34-
$containerBuilder = new ContainerBuilder();
33+
$config = require __DIR__ . '/config.php';
34+
$factory = new ContainerFactory();
3535

36-
// Use the builder to create and configure a container using an array of
37-
// ContainerConfig classes. Make sure the classes can be autoloaded!
38-
return $containerBuilder->newConfiguredInstance([
39-
'Application\Config\Common',
40-
]);
36+
return $factory(new Config($config));
4137
```
4238

43-
The bare minimum `ContainerConfig` code needed to make zend-expressive work is:
44-
45-
```php
46-
<?php
47-
// In src/Config/Common.php:
48-
namespace Application\Config;
49-
50-
use Aura\Di\Container;
51-
use Aura\Di\ContainerConfig;
52-
use Aura\Router\Generator;
53-
use Aura\Router\RouteCollection;
54-
use Aura\Router\RouteFactory;
55-
use Aura\Router\Router;
56-
use Zend\Escaper\Escaper;
57-
use Zend\Expressive\Application;
58-
use Zend\Expressive\Container as ExpressiveContainer;
59-
use Zend\Expressive\Delegate;
60-
use Zend\Expressive\Middleware;
61-
use Zend\Expressive\Plates\PlatesRenderer;
62-
use Zend\Expressive\Router\AuraRouter;
63-
use Zend\Expressive\Router\Route;
64-
use Zend\Expressive\Router\RouterInterface;
65-
use Zend\Expressive\Template\TemplateRendererInterface;
66-
67-
class Common extends ContainerConfig
68-
{
69-
public function define(Container $di)
70-
{
71-
$di->params[RouteCollection::class] = array(
72-
'route_factory' => $di->lazyNew(RouteFactory::class),
73-
);
74-
$di->params[Router::class] = array(
75-
'routes' => $di->lazyNew(RouteCollection::class),
76-
'generator' => $di->lazyNew(Generator::class),
77-
);
78-
$di->params[AuraRouter::class]['router'] = $di->lazyNew(Router::class);
79-
$di->set(RouterInterface::class, $di->lazyNew(AuraRouter::class));
80-
$di->set(Container\NotFoundDelegateFactory::class, $di->lazyNew(ExpressiveContainer\NotFoundDelegateFactory::class));
81-
$di->set(Delegate\NotFoundDelegate::class, $di->lazyGetCall(ExpressiveContainer\NotFoundDelegateFactory::class, '__invoke', $di));
82-
$di->set('Zend\Expressive\Delegate\DefaultDelegate', $di->lazyGetCall(ExpressiveContainer\NotFoundDelegateFactory::class, '__invoke', $di));
83-
$di->set(Container\ApplicationFactory::class, $di->lazyNew(ExpressiveContainer\ApplicationFactory::class));
84-
$di->set(Application::class, $di->lazyGetCall(ExpressiveContainer\ApplicationFactory::class, '__invoke', $di));
85-
86-
// Not Found handler
87-
$di->set(Middleware\NotFoundHandler::class, $di->lazyGetCall(ExpressiveContainer\NotFoundHandlerFactory::class, '__invoke', $di));
88-
89-
// Templating
90-
// In most cases, you can instantiate the template renderer you want to use
91-
// without using a factory:
92-
$di->set(TemplateRendererInterface::class, $di->lazyNew(PlatesRenderer::class));
93-
94-
// These next two can be added in any environment; they won't be used unless
95-
// you add the WhoopsErrorResponseGenerator as the ErrorResponseGenerator implementation:
96-
$di->set(ExpressiveContainer\WhoopsFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsFactory::class));
97-
$di->set('Zend\Expressive\Whoops', $di->lazyGetCall(ExpressiveContainer\WhoopsFactory::class, '__invoke', $di));
98-
$di->set(ExpressiveContainer\WhoopsPageHandlerFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsPageHandlerFactory::class));
99-
$di->set('Zend\Expressive\WhoopsPageHandler', $di->lazyGetCall(ExpressiveContainer\WhoopsPageHandlerFactory::class, '__invoke', $di));
100-
101-
// Error Handling
102-
$di->set('Zend\Stratigility\Middleware\ErrorHandler', $di->lazyGetCall(ExpressiveContainer\ErrorHandlerFactory::class, '__invoke', $di));
103-
104-
// If in development:
105-
$di->set(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class, $di->lazyNew(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class));
106-
$di->set(Middleware\ErrorResponseGenerator::class, $di->lazyGetCall(ExpressiveContainer\WhoopsErrorResponseGeneratorFactory::class, '__invoke', $di));
107-
108-
// If in production:
109-
// $di->set(Middleware\ErrorResponseGenerator::class, $di->lazyGetCall(Container\ErrorResponseGeneratorFactory::class, '__invoke', $di));
110-
}
111-
112-
public function modify(Container $di)
113-
{
114-
/*
115-
$router = $di->get(RouterInterface::class);
116-
$router->addRoute(new Route('/hello/{name}', function ($request, $response, $next) {
117-
$escaper = new Escaper();
118-
$name = $request->getAttribute('name', 'World');
119-
$response->getBody()->write('Hello ' . $escaper->escapeHtml($name));
120-
return $response;
121-
}, Route::HTTP_METHOD_ANY, 'hello'));
122-
*/
123-
}
124-
}
125-
```
39+
For more information, please see the
40+
[zend-auradi-config documentation](https://github.com/zendframework/zend-auradi-config/blob/master/README.md).
12641

12742
Your bootstrap (typically `public/index.php`) will then look like this:
12843

12944
```php
13045
chdir(dirname(__DIR__));
13146
require 'vendor/autoload.php';
47+
13248
$container = require 'config/container.php';
13349
$app = $container->get(Zend\Expressive\Application::class);
50+
13451
require 'config/pipeline.php';
13552
require 'config/routes.php';
53+
13654
$app->run();
13755
```
Lines changed: 22 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,53 @@
11
# Using Pimple
22

3-
[Pimple](http://pimple.sensiolabs.org/) is a widely used code-driven dependency
4-
injection container provided as a standalone component by SensioLabs. It
5-
features:
3+
[Pimple](http://pimple.sensiolabs.org/) is a widely used, code-driven,
4+
dependency injection container provided as a standalone component by SensioLabs.
5+
It features:
66

77
- combined parameter and service storage.
88
- ability to define factories for specific classes.
99
- lazy-loading via factories.
1010

1111
Pimple only supports programmatic creation at this time.
1212

13-
## Installing Pimple
13+
## Installing and configuring Pimple
1414

1515
Pimple implements [PSR-11 Container](https://github.com/php-fig/container)
16-
as of version 3.2.
16+
as of version 3.2. To use Pimple as a dependency injection container, we
17+
recommend using [zendframework/zend-pimple-config](https://github.com/zendframework/zend-pimple-config),
18+
which helps you to configure the PSR-11 container. First install the package:
1719

1820
```bash
19-
$ composer require pimple/pimple
21+
$ composer require zendframework/zend-pimple-config
2022
```
2123

22-
## Configuring Pimple
23-
24-
To configure Pimple, instantiate it, and then add the factories desired. We
25-
recommend doing this in a dedicated script that returns the Pimple instance; in
26-
this example, we'll have that in `config/container.php`.
24+
Now, create the file `config/container.php` with the following contents:
2725

2826
```php
29-
use Pimple\Container as PimpleContainer;
30-
use Pimple\Psr11\Container as PsrContainer;
31-
use Zend\Expressive\Container;
32-
use Zend\Expressive\Plates\PlatesRenderer;
33-
use Zend\Expressive\Router;
34-
use Zend\Expressive\Template\TemplateRendererInterface;
35-
36-
$container = new PimpleContainer();
37-
38-
// Application and configuration
39-
$container['config'] = include 'config/config.php';
40-
$container['Zend\Expressive\Application'] = new Container\ApplicationFactory;
41-
42-
// Routing
43-
// In most cases, you can instantiate the router you want to use without using a
44-
// factory:
45-
$container['Zend\Expressive\Router\RouterInterface'] = function (PimpleContainer $container) {
46-
return new Router\Aura();
47-
};
48-
49-
// Expressive 2.X: We'll provide a default delegate:
50-
$delegateFactory = new Container\NotFoundDelegateFactory();
51-
$container['Zend\Expressive\Delegate\DefaultDelegate'] = $delegateFactory;
52-
$container[Zend\Expressive\Delegate\NotFoundDelegate::class] = $delegateFactory;
53-
54-
// Expressive 2.X: We'll provide a not found handler:
55-
$container[Zend\Expressive\Middleware\NotFoundHandler::class] = new Container\NotFoundHandlerFactory();
56-
57-
// Templating
58-
// In most cases, you can instantiate the template renderer you want to use
59-
// without using a factory:
60-
$container[TemplateRendererInterface::class] = function (PimpleContainer $container) {
61-
return new PlatesRenderer();
62-
};
63-
64-
// These next two can be added in any environment; they won't be used unless:
65-
// - (Expressive 1.X): you add the WhoopsErrorHandler as the FinalHandler
66-
// implementation:
67-
// - (Expressive 2.X): you add the WhoopsErrorResponseGenerator as the
68-
// ErrorResponseGenerator implementation
69-
$container['Zend\Expressive\Whoops'] = new Container\WhoopsFactory();
70-
$container['Zend\Expressive\WhoopsPageHandler'] = new Container\WhoopsPageHandlerFactory();
71-
72-
// Error Handling
73-
74-
// - In Expressive 2.X, all environments:
75-
$container['Zend\Expressive\Middleware\ErrorHandler'] = new Container\ErrorHandlerFactory();
76-
77-
// If in development:
78-
// - Expressive 1.X:
79-
$container['Zend\Expressive\FinalHandler'] = new Container\WhoopsErrorHandlerFactory();
80-
// - Expressive 2.X:
81-
$container[Zend\Expressive\Middleware\ErrorResponseGenerator::class] = new Container\WhoopsErrorResponseGeneratorFactory();
82-
83-
// If in production:
84-
// - Expressive 1.X:
85-
$container['Zend\Expressive\FinalHandler'] = new Container\TemplatedErrorHandlerFactory();
86-
// - Expressive 2.X:
87-
$container[Zend\Expressive\Middleware\ErrorResponseGenerator::class] = new Container\ErrorResponseGeneratorFactory();
88-
89-
return new PsrContainer($container);
27+
<?php
28+
use Zend\Pimple\Config\Config;
29+
use Zend\Pimple\Config\ContainerFactory;
30+
31+
$config = require __DIR__ . '/config.php';
32+
$factory = new ContainerFactory();
33+
34+
return $factory(new Config($config));
9035
```
9136

37+
For more information, please see the
38+
[zend-pimple-config documentation](https://github.com/zendframework/zend-pimple-config/blob/master/README.md).
39+
9240
Your bootstrap (typically `public/index.php`) will then look like this:
9341

9442
```php
9543
chdir(dirname(__DIR__));
44+
require 'vendor/autoload.php';
45+
9646
$container = require 'config/container.php';
9747
$app = $container->get(Zend\Expressive\Application::class);
9848

99-
// In Expressive 2.X:
10049
require 'config/pipeline.php';
10150
require 'config/routes.php';
10251

103-
// All versions:
10452
$app->run();
10553
```
106-
107-
> ### Environments
108-
>
109-
> In the example above, we provide two alternate definitions for
110-
> either the service `Zend\Expressive\FinalHandler` (Expressive 1.X) or the
111-
> service `Zend\Expressive\Middleware\ErrorResponseGenerator` (Expressive 2.X),
112-
> one for development and one for production. You will need to add logic to
113-
> your file to determine which definition to provide; this could be accomplished
114-
> via an environment variable.

0 commit comments

Comments
 (0)