|
| 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 | + |
| 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 | + |
| 18 | + |
| 19 | +## Installing Aura.Di |
| 20 | + |
| 21 | +Aura.Di v3 only implements [container-interop](https://github.com/container-interop/container-interop). |
| 22 | + |
| 23 | +```bash |
| 24 | +$ composer require "aura/di:3.0.*@beta" |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +Aura.Di can help you to reorganize your code better with |
| 30 | +[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`. |
| 33 | + |
| 34 | +```php |
| 35 | +<?php |
| 36 | +use Aura\Di\ContainerBuilder; |
| 37 | + |
| 38 | +$container_builder = new ContainerBuilder(); |
| 39 | + |
| 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', |
| 45 | +]); |
| 46 | +``` |
| 47 | + |
| 48 | +The bare minimal `ContainerConfig ` code needed to make zend-expressive work is |
| 49 | + |
| 50 | +```php |
| 51 | +<?php |
| 52 | +namespace Application\_Config; |
| 53 | + |
| 54 | +use Aura\Di\Container; |
| 55 | +use Aura\Di\ContainerConfig; |
| 56 | + |
| 57 | +class Common extends ContainerConfig |
| 58 | +{ |
| 59 | + public function define(Container $di) |
| 60 | + { |
| 61 | + $di->params['Aura\Router\RouteCollection'] = array( |
| 62 | + 'route_factory' => $di->lazyNew('Aura\Router\RouteFactory'), |
| 63 | + ); |
| 64 | + $di->params['Aura\Router\Router'] = array( |
| 65 | + 'routes' => $di->lazyNew('Aura\Router\RouteCollection'), |
| 66 | + 'generator' => $di->lazyNew('Aura\Router\Generator'), |
| 67 | + ); |
| 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)); |
| 72 | + |
| 73 | + // Templating |
| 74 | + // In most cases, you can instantiate the template renderer you want to use |
| 75 | + // without using a factory: |
| 76 | + $di->set('Zend\Expressive\Template\TemplateInterface', $di->lazyNew('Zend\Expressive\Template\Plates')); |
| 77 | + |
| 78 | + // These next two can be added in any environment; they won't be used unless |
| 79 | + // 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)); |
| 84 | + |
| 85 | + // Error Handling |
| 86 | + |
| 87 | + // 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)); |
| 90 | + |
| 91 | + // If in production: |
| 92 | + // $di->set('Zend\Expressive\FinalHandler', $di->lazyGetCall('Zend\Expressive\Container\TemplatedErrorHandlerFactory', '__invoke', $di)); |
| 93 | + } |
| 94 | + |
| 95 | + public function modify(Container $di) |
| 96 | + { |
| 97 | + /* |
| 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(); |
| 101 | + $name = $request->getAttribute('name', 'World'); |
| 102 | + $response->write('Hello ' . $escaper->escapeHtml($name)); |
| 103 | + return $response; |
| 104 | + }, \Zend\Expressive\Router\Route::HTTP_METHOD_ANY, 'hello')); |
| 105 | + */ |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +Your bootstrap (typically `public/index.php`) will then look like this: |
| 111 | + |
| 112 | +```php |
| 113 | +chdir(dirname(__DIR__)); |
| 114 | +require 'vendor/autoload.php'; |
| 115 | +$container = require 'config/services.php'; |
| 116 | +$app = $container->get('Zend\Expressive\Application'); |
| 117 | +$app->run(); |
| 118 | +``` |
0 commit comments