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

Commit 9d06dc0

Browse files
committed
Merge branch 'hotfix/112' into develop
Forward port #112
2 parents 40c58ad + 1181392 commit 9d06dc0

File tree

5 files changed

+131
-4
lines changed

5 files changed

+131
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ All notable changes to this project will be documented in this file, in reverse
3333

3434
### Added
3535

36-
- Nothing.
36+
- [#112](https://github.com/zendframework/zend-expressive/pull/112) adds a
37+
chapter to the documentation on using Aura.Di (v3beta) with zend-expressive.
3738

3839
### Deprecated
3940

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"filp/whoops": "^1.1 to use the Whoops error handler",
4545
"league/plates": "^3.1 to use the Plates template adapter",
4646
"mouf/pimple-interop": "^1.0 to use Pimple for dependency injection",
47+
"aura/di": "3.0.*@beta to make use of Aura.Di dependency injection container",
4748
"nikic/fast-route": "^0.6.0 to use the FastRoute routing adapter",
4849
"zendframework/zend-mvc": "^2.5 to use the zend-mvc TreeRouteStack routing adapter",
4950
"zendframework/zend-psr7bridge": "^0.1.0 to use the zend-mvc TreeRouteStack routing adapter",

doc/book/container/aura-di.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Using Aura.Di
2+
3+
[Aura.Di](https://github.com/auraphp/Aura.Di/) provides a serializable dependency
4+
injection container with the following features:
5+
6+
- constructor and setter injection.
7+
- inheritance of constructor parameter and setter method values from parent
8+
classes.
9+
- inheritance of setter method values from interfaces and traits.
10+
- lazy-loaded instances, services, includes/requires, and values.
11+
- instance factories.
12+
- optional auto-resolution of typehinted constructor parameter values.
13+
14+
## Installing Aura.Di
15+
16+
Aura.Di only implements [container-interop](https://github.com/container-interop/container-interop)
17+
as of version 3 (in beta at the time of writing).
18+
19+
```bash
20+
$ composer require "aura/di:3.0.*@beta"
21+
```
22+
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/services.php`:
29+
30+
```php
31+
<?php
32+
use Aura\Di\ContainerBuilder;
33+
34+
$containerBuilder = new ContainerBuilder();
35+
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+
]);
41+
```
42+
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;
59+
use Zend\Expressive\Router\AuraRouter;
60+
use Zend\Expressive\Router\Route;
61+
use Zend\Expressive\Router\RouterInterface;
62+
use Zend\Expressive\Template;
63+
64+
class Common extends ContainerConfig
65+
{
66+
public function define(Container $di)
67+
{
68+
$di->params[RouteCollection::class] = array(
69+
'route_factory' => $di->lazyNew(RouteFactory::class),
70+
);
71+
$di->params[Router::class] = array(
72+
'routes' => $di->lazyNew(RouteCollection::class),
73+
'generator' => $di->lazyNew(Generator::class),
74+
);
75+
$di->params[AuraRouter::class]['router'] = $di->lazyNew(Router::class);
76+
$di->set(RouterInterface::class, $di->lazyNew(AuraRouter::class));
77+
$di->set(Container\ApplicationFactory::class, $di->lazyNew(Container\ApplicationFactory::class));
78+
$di->set(Application::class, $di->lazyGetCall(Container\ApplicationFactory::class, '__invoke', $di));
79+
80+
// Templating
81+
// In most cases, you can instantiate the template renderer you want to use
82+
// without using a factory:
83+
$di->set(Template\TemplateInterface::class, $di->lazyNew(Template\Plates::class));
84+
85+
// These next two can be added in any environment; they won't be used unless
86+
// you add the WhoopsErrorHandler as the FinalHandler implementation:
87+
$di->set(Container\WhoopsFactory::class, $di->lazyNew(Container\WhoopsFactory::class));
88+
$di->set('Zend\Expressive\Whoops', $di->lazyGetCall(Container\WhoopsFactory::class, '__invoke', $di));
89+
$di->set(Container\WhoopsPageHandlerFactory::class, $di->lazyNew(Container\WhoopsPageHandlerFactory::class));
90+
$di->set('Zend\Expressive\WhoopsPageHandler', $di->lazyGetCall(Container\WhoopsPageHandlerFactory::class, '__invoke', $di));
91+
92+
// Error Handling
93+
94+
// If in development:
95+
$di->set(Container\WhoopsErrorHandlerFactory::class, $di->lazyNew(Container\WhoopsErrorHandlerFactory::class));
96+
$di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall(Container\WhoopsErrorHandlerFactory::class, '__invoke', $di));
97+
98+
// If in production:
99+
// $di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall(Container\TemplatedErrorHandlerFactory::class, '__invoke', $di));
100+
}
101+
102+
public function modify(Container $di)
103+
{
104+
/*
105+
$router = $di->get(RouterInterface::class);
106+
$router->addRoute(new Route('/hello/{name}', function ($request, $response, $next) {
107+
$escaper = new Escaper();
108+
$name = $request->getAttribute('name', 'World');
109+
$response->write('Hello ' . $escaper->escapeHtml($name));
110+
return $response;
111+
}, Route::HTTP_METHOD_ANY, 'hello'));
112+
*/
113+
}
114+
}
115+
```
116+
117+
Your bootstrap (typically `public/index.php`) will then look like this:
118+
119+
```php
120+
chdir(dirname(__DIR__));
121+
require 'vendor/autoload.php';
122+
$container = require 'config/services.php';
123+
$app = $container->get('Zend\Expressive\Application');
124+
$app->run();
125+
```

doc/book/container/bookdown.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{"Introduction": "intro.md"},
55
{"Container Factories": "factories.md"},
66
{"Using zend-servicemanager": "zend-servicemanager.md"},
7-
{"Using Pimple": "pimple.md"}
7+
{"Using Pimple": "pimple.md"},
8+
{"Using Aura.Di": "aura-di.md"}
89
]
910
}
10-

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pages:
55
- { 'Overview and Features': features.md }
66
- { 'Quick Start': quick-start.md }
77
- { Applications: application.md }
8-
- { Containers: [{ Introduction: container/intro.md }, { 'Container Factories': container/factories.md }, { 'Using zend-servicemanager': container/zend-servicemanager.md }, { 'Using Pimple': container/pimple.md }] }
8+
- { Containers: [{ Introduction: container/intro.md }, { 'Container Factories': container/factories.md }, { 'Using zend-servicemanager': container/zend-servicemanager.md }, { 'Using Pimple': container/pimple.md }, { 'Using Aura.Di': container/aura-di.md }] }
99
- { 'Routing Adapters': [{ Introduction: router/intro.md }, { 'Routing Interface': router/interface.md }, { 'URI Generation': router/uri-generation.md }, { 'Routing vs Piping': router/piping.md }, { 'Using Aura': router/aura.md }, { 'Using FastRoute': router/fast-route.md }, { 'Using the ZF2 Router': router/zf2.md }] }
1010
- { Templating: [{ Introduction: template/intro.md }, { 'Template Interface': template/interface.md }, { 'Templated Middleware': template/middleware.md }, { 'Using Plates': template/plates.md }, { 'Using Twig': template/twig.md }, { 'Using zend-view': template/zend-view.md }] }
1111
- { 'Error Handling': error-handling.md }

0 commit comments

Comments
 (0)