├── README.md ├── example ├── indexWithoutDI.php └── index.php └── src └── DI.php /README.md: -------------------------------------------------------------------------------- 1 | PHP Dependency Injection 2 | ======================== 3 | Read more about that here: 4 | http://krasimirtsonev.com/blog/article/Dependency-Injection-in-PHP-example-how-to-DI-create-your-own-dependency-injection-container -------------------------------------------------------------------------------- /example/indexWithoutDI.php: -------------------------------------------------------------------------------- 1 | ".$str."
"; 6 | } 7 | } 8 | 9 | class UsersModel { 10 | public function get() { 11 | return array( 12 | (object) array("firstName" => "John", "lastName" => "Doe"), 13 | (object) array("firstName" => "Mark", "lastName" => "Black") 14 | ); 15 | } 16 | } 17 | 18 | class Navigation { 19 | private $view; 20 | public function __construct() { 21 | $this->view = new View(); 22 | } 23 | public function show() { 24 | $this->view->show(' 25 | Home | 26 | Products | 27 | Contacts 28 | '); 29 | } 30 | } 31 | 32 | class Content { 33 | 34 | private $title; 35 | private $view; 36 | private $usersModel; 37 | 38 | public function __construct($title) { 39 | $this->title = $title; 40 | $this->view = new View(); 41 | $this->usersModel = new UsersModel(); 42 | } 43 | public function show() { 44 | $users = $this->usersModel->get(); 45 | $this->view->show($this->title); 46 | foreach($users as $user) { 47 | $this->view->show($user->firstName." ".$user->lastName); 48 | } 49 | } 50 | } 51 | 52 | class PageController { 53 | public function show() { 54 | $navigation = new Navigation(); 55 | $content = new Content("Content title!"); 56 | $navigation->show(); 57 | $content->show(); 58 | } 59 | } 60 | 61 | $page = new PageController(); 62 | $page->show(); 63 | 64 | 65 | ?> -------------------------------------------------------------------------------- /example/index.php: -------------------------------------------------------------------------------- 1 | ".$str.""; 8 | } 9 | } 10 | 11 | class UsersModel { 12 | public function get() { 13 | return array( 14 | (object) array("firstName" => "John", "lastName" => "Doe"), 15 | (object) array("firstName" => "Mark", "lastName" => "Black") 16 | ); 17 | } 18 | } 19 | 20 | /** 21 | * @Inject view 22 | */ 23 | class Navigation { 24 | public function show() { 25 | $this->view->show(' 26 | Home | 27 | Products | 28 | Contacts 29 | '); 30 | } 31 | } 32 | 33 | /** 34 | * @Inject usersModel 35 | * @Inject view 36 | */ 37 | class Content { 38 | private $title; 39 | public function __construct($title) { 40 | $this->title = $title; 41 | } 42 | public function show() { 43 | $this->users = $this->usersModel->get(); 44 | $this->view->show($this->title); 45 | foreach($this->users as $user) { 46 | $this->view->show($user->firstName." ".$user->lastName); 47 | } 48 | } 49 | } 50 | 51 | /** 52 | * @Inject navigation 53 | * @Inject content 54 | */ 55 | class PageController { 56 | public function show() { 57 | $this->navigation->show(); 58 | $this->content->show(); 59 | } 60 | } 61 | 62 | // mapping 63 | DI::mapClass("navigation", "Navigation"); 64 | DI::mapClass("content", "Content", array("Content title!")); 65 | DI::mapClass("view", "View"); 66 | DI::mapClassAsSingleton("usersModel", "UsersModel"); 67 | 68 | // showing content 69 | $page = DI::getInstanceOf("PageController"); 70 | $page->show(); 71 | 72 | 73 | ?> -------------------------------------------------------------------------------- /src/DI.php: -------------------------------------------------------------------------------- 1 | newInstanceArgs($arguments); 25 | } 26 | 27 | // injecting 28 | if($doc = $reflection->getDocComment()) { 29 | $lines = explode("\n", $doc); 30 | foreach($lines as $line) { 31 | if(count($parts = explode("@Inject", $line)) > 1) { 32 | $parts = explode(" ", $parts[1]); 33 | if(count($parts) > 1) { 34 | $key = $parts[1]; 35 | $key = str_replace("\n", "", $key); 36 | $key = str_replace("\r", "", $key); 37 | if(isset(self::$map->$key)) { 38 | switch(self::$map->$key->type) { 39 | case "value": 40 | $obj->$key = self::$map->$key->value; 41 | break; 42 | case "class": 43 | $obj->$key = self::getInstanceOf(self::$map->$key->value, self::$map->$key->arguments); 44 | break; 45 | case "classSingleton": 46 | if(self::$map->$key->instance === null) { 47 | $obj->$key = self::$map->$key->instance = self::getInstanceOf(self::$map->$key->value, self::$map->$key->arguments); 48 | } else { 49 | $obj->$key = self::$map->$key->instance; 50 | } 51 | break; 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | 59 | // return the created instance 60 | return $obj; 61 | 62 | } 63 | public static function mapValue($key, $value) { 64 | self::addToMap($key, (object) array( 65 | "value" => $value, 66 | "type" => "value" 67 | )); 68 | } 69 | public static function mapClass($key, $value, $arguments = null) { 70 | self::addToMap($key, (object) array( 71 | "value" => $value, 72 | "type" => "class", 73 | "arguments" => $arguments 74 | )); 75 | } 76 | public static function mapClassAsSingleton($key, $value, $arguments = null) { 77 | self::addToMap($key, (object) array( 78 | "value" => $value, 79 | "type" => "classSingleton", 80 | "instance" => null, 81 | "arguments" => $arguments 82 | )); 83 | } 84 | private static function addToMap($key, $obj) { 85 | if(self::$map === null) { 86 | self::$map = (object) array(); 87 | } 88 | self::$map->$key = $obj; 89 | } 90 | 91 | } 92 | 93 | ?> --------------------------------------------------------------------------------