From 79cd1b51b9cee99fa213412efca9d8b458aaea28 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 17 Sep 2014 21:46:06 +0200 Subject: [PATCH] added content to di part --- 8-dependency-injector.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/8-dependency-injector.md b/8-dependency-injector.md index f942025..2ee20e7 100644 --- a/8-dependency-injector.md +++ b/8-dependency-injector.md @@ -8,4 +8,40 @@ My favorite injector is [Auryn](https://github.com/rdlowrey/Auryn), so install ` [Pimple](http://pimple.sensiolabs.org/), [Orno DI](https://github.com/orno/di), [PHP-DI](https://github.com/mnapoli/PHP-DI) +Create a new file called `Dependencies.php` in your `src/` folder. In there add the following content: + +``` +share('Http\CookieBuilder'); +$injector->delegate('Http\CookieBuilder', function($environment){ + $cookieBuilder = new \Http\CookieBuilder; + $cookieBuilder->setDefaultSecure($environment === 'production'); + return $cookieBuilder; +}); + +$injector->alias('Http\Response', 'Http\HttpResponse'); +$injector->share('Http\HttpRequest'); +$injector->define('Http\HttpRequest', [ + ':get' => $_GET, + ':post' => $_POST, + ':cookies' => $_COOKIE, + ':files' => $_FILES, + ':server' => $_SERVER, +]); + +$injector->alias('Http\Request', 'Http\HttpRequest'); +$injector->share('Http\HttpResponse'); + +return $injector; +``` + +Make sure you understand what `alias`, `share` and `define` are doing before you continue. You can read about them in the [Auryn documentation](https://github.com/rdlowrey/Auryn). + +You are sharing the HTTP objects because there would not be much point in adding content to one object and then returning another one. So by sharing it you always get the same instance. + +The alias allows you to type hint the interface instead of the class name. This makes it easy to switch the implementation without having to go back and edit all your classes that use the old implementation. + to be continued...