Last active
April 5, 2019 14:58
-
-
Save jocoonopa/5051fe4ed890c079f80e11c66dadcfaa to your computer and use it in GitHub Desktop.
Ioc-Simple-Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class ComicBook implements Readable | |
| { | |
| public function read(NeedOne $n1, NeedTwo $n2, NeedThree $n3) | |
| { | |
| return $n3->seeWord( | |
| $n2->seeRaw( | |
| $n1->seePage($this) | |
| ) | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Eye | |
| { | |
| public function seePage() | |
| { | |
| return 1; | |
| } | |
| public function seeRaw($page) | |
| { | |
| $raws = [ | |
| 'Hello world', | |
| 'Say good night', | |
| ]; | |
| return data_get($raws, ($page - 1), ''); | |
| } | |
| public function seeWord($raw) | |
| { | |
| return explode(' ', $raw); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface NeedOne {} | |
| interface NeedTwo {} | |
| interface NeedThree {} | |
| $eye = new Eye; | |
| MyContainer::bind(NeedOne::class, $eye); | |
| MyContainer::bind(NeedTwo::class, $eye); | |
| MyContainer::bind(NeedThree::class, $eye); | |
| $people = new People(); | |
| $people->read(new ComicBook()); // output: ['Hello', 'world'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MyContainer | |
| { | |
| protected static $container; | |
| public static function bind($name, Callable $resolver) | |
| { | |
| static::$container[$name] = $resolver; | |
| } | |
| public static function make($name) | |
| { | |
| if (! isset(static::$container[$name])) { | |
| throw new Exception("Binding does not exist in containeer"); | |
| } | |
| $resolver = static::$container[$name]; | |
| return $resolver(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class People | |
| { | |
| public function read(Readable $readable) | |
| { | |
| $params = []; | |
| $receipeParameters = (new ReflectMethod($readable, 'read'))->getParameters(); | |
| foreach ($receipeParameters as $parameter) { | |
| $params[] = MyContainer::make($parameter->getType()); | |
| } | |
| return call_user_func_array( | |
| [ | |
| $readable, 'read', | |
| ], | |
| $params | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface Readable{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment