Guest User

Untitled

a guest
Jun 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. <?php
  2.  
  3. // ArticlesRespositoryInterface.php
  4. interface ArticlesRepositoryInterface
  5. {
  6. public function get($id): Article;
  7. }
  8.  
  9. // PDOArticlesRespository.php
  10. class PDOArticleRepository implements ArticlesRepositoryInterface
  11. {
  12. public function get($id): Article
  13. {}
  14. }
  15.  
  16. // FileSystemArticlesRespository.php
  17. class FileSystemArticleRepository implements ArticlesRepositoryInterface
  18. {
  19. public function get($id): Article
  20. {}
  21. }
  22.  
  23. // GetArticlesController.php
  24. class GetArticleController
  25. {
  26. protected $articlesRepository:
  27.  
  28. public function __construct(
  29. ArticlesRepositoryInterface $articlesRepository
  30. ) {
  31. $this->articlesRepository = $articlesRepository;
  32. }
  33.  
  34. public function __invoke(
  35. Request $request,
  36. Response $response
  37. ) {
  38. $id = $request->input('id');
  39.  
  40. $article = $articlesRepository->get($id);
  41.  
  42. $response->getBody()->write("{$article->title}");
  43.  
  44. return $response;
  45. }
  46. }
  47.  
  48. // app.php
  49. $container->bind(ArticleRepositoryInterface::class, function($container) {
  50. $connection = $container->get(Connection::class);
  51.  
  52. return new FileSystemArticleRepository($connection);
  53. });
  54.  
  55. $container->bind(GetArticleController::class, function ($container) {
  56. $repository = $container->get(ArticleRepositoryInterface::class);
  57.  
  58. return new GetArticleController($repository):
  59. });
  60.  
  61. $container->get(GetArticleController::class);
  62.  
  63. Route::get('/articles/', 'GetArticleController'):
  64.  
  65. // config/services.yml
  66. services:
  67. Connection:
  68. arguments:
  69. database: mysql
  70. host: localhost
  71. user: ricardo
  72. pass: 123456
  73.  
  74. ArticleRepositoryInterface:
  75. class: PDOArticleRepository
  76. arguments:
  77. - Connection
  78.  
  79. GetArticleController:
  80. arguments:
  81. - ArticleRepositoryInterface
Add Comment
Please, Sign In to add comment