Created
November 21, 2012 01:54
-
-
Save jadell/4122568 to your computer and use it in GitHub Desktop.
DI migration examples
This file contains 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 | |
// index.php | |
$app->get('/{id}', function ($id) use ($app) { | |
$controller = new Lal\HelloController(); | |
return $controller->helloId($id); | |
}); | |
$app->get('/', function () use ($app) { | |
$controller = new Lal\HelloController(); | |
return $controller->index(); | |
}); | |
// HelloController.php | |
class HelloController | |
{ | |
protected $userRepo; | |
protected $view; | |
public function __construct() | |
{ | |
$this->userRepo = new UserRepository(); | |
$this->view = new View(); | |
} | |
// ... | |
} | |
// UserRepository.php | |
class UserRepository | |
{ | |
protected $datasource; | |
public function __construct() | |
{ | |
$this->datasource = new Datasource(); | |
} | |
// ... | |
} | |
// User.php | |
class User | |
{ | |
protected $userRepo; | |
public function __construct() | |
{ | |
$this->userRepo = new UserRepository(); | |
} | |
// ... | |
} |
This file contains 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 | |
// bootstrap.php | |
$app['data.path'] = __DIR__.'/data/hello.json'; | |
$app['data.source'] = $app->share(function ($app) { | |
return new Lal\Datasource($app['data.path']); | |
}); | |
$app['user.factory'] = $app->protect(function () use ($app) { | |
return new Lal\User($app['user.repo']); | |
}); | |
$app['user.repo'] = $app->share(function ($app) { | |
return new Lal\UserRepository($app['data.source'], $app['user.factory']); | |
}); | |
$app['view.templates'] = __DIR__.'/templates/'; | |
$app['view.renderer'] = function ($app) { | |
return new Lal\View($app['view.templates']); | |
}; | |
$app['hello.controller'] = $app->share(function ($app) { | |
return new Lal\HelloController($app['user.repo'], $app['view.renderer']); | |
}); | |
// index.php | |
$app->get('/{id}', function ($id) use ($app) { | |
return $app['hello.controller']->helloId($id); | |
}); | |
$app->get('/', function () use ($app) { | |
return $app['hello.controller']->index(); | |
}); |
This file contains 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 | |
// HelloController.php | |
class HelloController | |
{ | |
protected $userRepo; | |
protected $view; | |
public function __construct(UserRepository $userRepo, View $view) | |
{ | |
$this->userRepo = $userRepo; | |
$this->view = $view; | |
} | |
// ... | |
} | |
// UserRepository.php | |
class UserRepository | |
{ | |
protected $datasource; | |
protected $userFactory; | |
public function __construct(Datasource $datasource, $userFactory) | |
{ | |
$this->datasource = $datasource; | |
$this->userFactory = $userFactory; | |
} | |
// ... | |
} | |
// User.php | |
class User | |
{ | |
protected $userRepo; | |
public function __construct(UserRepository $userRepo) | |
{ | |
$this->userRepo = $userRepo; | |
} | |
} |
This file contains 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 | |
// bootstrap.php | |
$app['comment.factory'] = $app->protect(function () use ($app) { | |
return new Lal\Comment($app['user.repo']); | |
}); | |
$app['comment.repo'] = $app->share(function ($app) { | |
return new Lal\CommentRepository($app['data.source'], $app['comment.factory']); | |
}); | |
$app['user.factory'] = $app->protect(function () use ($app) { | |
return new Lal\User($app['user.repo'], $app['comment.repo']); | |
}); | |
$app['hello.controller'] = $app->share(function ($app) { | |
return new Lal\HelloController($app['user.repo'], $app['comment.repo'], $app['view.renderer']); | |
}); | |
// HelloController.php | |
class HelloController | |
{ | |
protected $userRepo; | |
protected $commentRepo; | |
protected $view; | |
public function __construct(UserRepository $userRepo, CommentRepository $commentRepo, View $view) | |
{ | |
$this->userRepo = $userRepo; | |
$this->commentRepo = $commentRepo; | |
$this->view = $view; | |
} | |
// ... | |
} | |
// User.php | |
class User | |
{ | |
protected $userRepo; | |
protected $commentRepo; | |
public function __construct(UserRepository $userRepo, CommentRepository $commentRepo) | |
{ | |
$this->userRepo = $userRepo; | |
$this->commentRepo = $commentRepo; | |
} | |
} |
This file contains 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 | |
// bootstrap.php | |
$app['event.emitter'] = $app->share(function ($app) { | |
return new Evenement\EventEmitter(); | |
}); | |
$app['pageview.repo'] = $app->share(function ($app) { | |
return new Lal\PageViewRepository($app['data.source']); | |
}); | |
$app['hello.controller'] = $app->share(function ($app) { | |
$app['event.emitter']->on('user.viewed', array($app['pageview.repo'], 'incrementCount')); | |
return new Lal\HelloController($app['user.repo'], $app['comment.repo'], $app['view.renderer'], $app['event.emitter']); | |
}); | |
// HelloController.php | |
class HelloController | |
{ | |
protected $userRepo; | |
protected $commentRepo; | |
protected $view; | |
protected $event; | |
public function __construct(UserRepository $userRepo, CommentRepository $commentRepo, View $view, EventEmitter $event) | |
{ | |
$this->userRepo = $userRepo; | |
$this->commentRepo = $commentRepo; | |
$this->view = $view; | |
$this->event = $event; | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment