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

Commit 6165c67

Browse files
committed
Edited #112 to be consistent with other chapters.
1 parent 05c17e6 commit 6165c67

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

doc/book/container/aura-di.md

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,112 @@
33
[Aura.Di](https://github.com/auraphp/Aura.Di/) provides a serializable dependency
44
injection container with the following features:
55

6-
- constructor and setter injection
7-
8-
- inheritance of constructor parameter and setter method values from parent classes
9-
10-
- inheritance of setter method values from interfaces and traits
11-
12-
- lazy-loaded instances, services, includes/requires, and values
13-
14-
- instance factories
15-
16-
- optional auto-resolution of typehinted constructor parameter values
17-
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.
1813

1914
## Installing Aura.Di
2015

21-
Aura.Di v3 only implements [container-interop](https://github.com/container-interop/container-interop).
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).
2218

2319
```bash
2420
$ composer require "aura/di:3.0.*@beta"
2521
```
2622

2723
## Configuration
2824

29-
Aura.Di can help you to reorganize your code better with
25+
Aura.Di can help you to organize your code better with
3026
[ContainerConfig classes](http://auraphp.com/packages/Aura.Di/config.html) and
31-
[two step configuration](http://auraphp.com/blog/2014/04/07/two-stage-config/)
32-
in this example, we'll have that in `config/services.php`.
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`:
3329

3430
```php
3531
<?php
3632
use Aura\Di\ContainerBuilder;
3733

38-
$container_builder = new ContainerBuilder();
34+
$containerBuilder = new ContainerBuilder();
3935

40-
// use the builder to create and configure a container
41-
// using an array of ContainerConfig classes
42-
// make sure the classes can be autoloaded
43-
return $container_builder->newConfiguredInstance([
44-
'Application\_Config\Common',
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',
4540
]);
4641
```
4742

48-
The bare minimal `ContainerConfig ` code needed to make zend-expressive work is
43+
The bare minimum `ContainerConfig` code needed to make zend-expressive work is:
4944

5045
```php
5146
<?php
52-
namespace Application\_Config;
47+
// In src/Config/Common.php:
48+
namespace Application\Config;
5349

5450
use Aura\Di\Container;
5551
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;
5663

5764
class Common extends ContainerConfig
5865
{
5966
public function define(Container $di)
6067
{
61-
$di->params['Aura\Router\RouteCollection'] = array(
62-
'route_factory' => $di->lazyNew('Aura\Router\RouteFactory'),
68+
$di->params[RouteCollection::class] = array(
69+
'route_factory' => $di->lazyNew(RouteFactory::class),
6370
);
64-
$di->params['Aura\Router\Router'] = array(
65-
'routes' => $di->lazyNew('Aura\Router\RouteCollection'),
66-
'generator' => $di->lazyNew('Aura\Router\Generator'),
71+
$di->params[Router::class] = array(
72+
'routes' => $di->lazyNew(RouteCollection::class),
73+
'generator' => $di->lazyNew(Generator::class),
6774
);
68-
$di->params['Zend\Expressive\Router\Aura']['router'] = $di->lazyNew('Aura\Router\Router');
69-
$di->set('Zend\Expressive\Router\RouterInterface', $di->lazyNew('Zend\Expressive\Router\Aura'));
70-
$di->set('Zend\Expressive\Container\ApplicationFactory', $di->lazyNew('Zend\Expressive\Container\ApplicationFactory'));
71-
$di->set('Zend\Expressive\Application', $di->lazyGetCall('Zend\Expressive\Container\ApplicationFactory', '__invoke', $di));
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));
7279

7380
// Templating
7481
// In most cases, you can instantiate the template renderer you want to use
7582
// without using a factory:
76-
$di->set('Zend\Expressive\Template\TemplateInterface', $di->lazyNew('Zend\Expressive\Template\Plates'));
83+
$di->set(Template\TemplateInterface::class, $di->lazyNew(Template\Plates::class));
7784

7885
// These next two can be added in any environment; they won't be used unless
7986
// you add the WhoopsErrorHandler as the FinalHandler implementation:
80-
$di->set('Zend\Expressive\Container\WhoopsFactory', $di->lazyNew('Zend\Expressive\Container\WhoopsFactory'));
81-
$di->set('Zend\Expressive\Whoops', $di->lazyGetCall('Zend\Expressive\Container\WhoopsFactory', '__invoke', $di));
82-
$di->set('Zend\Expressive\Container\WhoopsPageHandlerFactory', $di->lazyNew('Zend\Expressive\Container\WhoopsPageHandlerFactory'));
83-
$di->set('Zend\Expressive\WhoopsPageHandler', $di->lazyGetCall('Zend\Expressive\Container\WhoopsPageHandlerFactory', '__invoke', $di));
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));
8491

8592
// Error Handling
8693

8794
// If in development:
88-
$di->set('Zend\Expressive\Container\WhoopsErrorHandlerFactory', $di->lazyNew('Zend\Expressive\Container\WhoopsErrorHandlerFactory'));
89-
$di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall('Zend\Expressive\Container\WhoopsErrorHandlerFactory', '__invoke', $di));
95+
$di->set(Container\WhoopsErrorHandlerFactory::class, $di->lazyNew(Container\WhoopsErrorHandlerFactory::class));
96+
$di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall(Container\WhoopsErrorHandlerFactory::class, '__invoke', $di));
9097

9198
// If in production:
92-
// $di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall('Zend\Expressive\Container\TemplatedErrorHandlerFactory', '__invoke', $di));
99+
// $di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall(Container\TemplatedErrorHandlerFactory::class, '__invoke', $di));
93100
}
94101

95102
public function modify(Container $di)
96103
{
97104
/*
98-
$router = $di->get('Zend\Expressive\Router\RouterInterface');
99-
$router->addRoute(new \Zend\Expressive\Router\Route('/hello/{name}', function ($request, $response, $next) {
100-
$escaper = new \Zend\Escaper\Escaper();
105+
$router = $di->get(RouterInterface::class);
106+
$router->addRoute(new Route('/hello/{name}', function ($request, $response, $next) {
107+
$escaper = new Escaper();
101108
$name = $request->getAttribute('name', 'World');
102109
$response->write('Hello ' . $escaper->escapeHtml($name));
103110
return $response;
104-
}, \Zend\Expressive\Router\Route::HTTP_METHOD_ANY, 'hello'));
111+
}, Route::HTTP_METHOD_ANY, 'hello'));
105112
*/
106113
}
107114
}

0 commit comments

Comments
 (0)