|
| 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 | +``` |
0 commit comments