Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Chapter 3 code#17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:chapter-2
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Chapter 3 code #17
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
| declare(strict_types=1); | ||
| use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | ||
| use Doctrine\Common\DataFixtures\Loader; | ||
| use Doctrine\Common\DataFixtures\Purger\ORMPurger; | ||
| use Doctrine\ORM\EntityManager; | ||
| require_once 'vendor/autoload.php'; | ||
| $container = require 'config/container.php'; | ||
| $entityManager = $container->get(EntityManager::class); | ||
| $config = $container->get('config'); | ||
| // Get fixtures directory from config | ||
| $fixturesPath = $config['doctrine']['fixtures']; | ||
| if (! is_dir($fixturesPath)) { | ||
| echo "Fixtures directory not found: {$fixturesPath}\n"; | ||
| exit(1); | ||
| } | ||
| // Load fixtures | ||
| $loader = new Loader(); | ||
| $loader->loadFromDirectory($fixturesPath); | ||
| // Execute fixtures | ||
| $purger = new ORMPurger(); | ||
| $executor = new ORMExecutor($entityManager, $purger); | ||
| echo "Loading fixtures from: {$fixturesPath}\n"; | ||
| $executor->execute($loader->getFixtures()); | ||
| echo "Fixtures loaded successfully!\n"; | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace Light\App\Factory; | ||
| use Light\Book\Handler\GetBooksHandler; | ||
| use Light\Book\Repository\BookRepository; | ||
| use Mezzio\Template\TemplateRendererInterface; | ||
| use Psr\Container\ContainerExceptionInterface; | ||
| use Psr\Container\ContainerInterface; | ||
| use Psr\Container\NotFoundExceptionInterface; | ||
| use function assert; | ||
| class BookHandlerFactory | ||
| { | ||
| /** | ||
| * @throws ContainerExceptionInterface | ||
| * @throws NotFoundExceptionInterface | ||
| */ | ||
| public function __invoke(ContainerInterface $container, string $requestedName): GetBooksHandler | ||
| { | ||
| $repository = $container->get(BookRepository::class); | ||
| $template = $container->get(TemplateRendererInterface::class); | ||
| assert($repository instanceof BookRepository); | ||
| assert($template instanceof TemplateRendererInterface); | ||
| return new GetBooksHandler($template, $repository); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace Light\App\Factory; | ||
| use Doctrine\ORM\EntityManager; | ||
| use Light\Book\Entity\Book; | ||
| use Light\Book\Repository\BookRepository; | ||
| use Psr\Container\ContainerExceptionInterface; | ||
| use Psr\Container\ContainerInterface; | ||
| use Psr\Container\NotFoundExceptionInterface; | ||
| use function assert; | ||
| class BookRepositoryFactory | ||
| { | ||
| /** | ||
| * @throws ContainerExceptionInterface | ||
| * @throws NotFoundExceptionInterface | ||
| */ | ||
| public function __invoke(ContainerInterface $container): BookRepository | ||
| { | ||
| $entityManager = $container->get(EntityManager::class); | ||
| $repository = $entityManager->getRepository(Book::class); | ||
| assert($repository instanceof BookRepository); | ||
| return $repository; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace Light\App\Fixture; | ||
| use Doctrine\Bundle\FixturesBundle\Fixture; | ||
| use Doctrine\Persistence\ObjectManager; | ||
| use Light\Book\Entity\Book; | ||
| class BookLoader extends Fixture | ||
| { | ||
| public function load(ObjectManager $manager): void | ||
| { | ||
| $book1 = new Book(); | ||
| $book1->setTitle('A Game of Thrones'); | ||
| $book1->setAuthor('George Martin'); | ||
| $manager->persist($book1); | ||
| $book2 = new Book(); | ||
| $book2->setTitle('The Lord of the Rings'); | ||
| $book2->setAuthor('J.R.R. Tolkien'); | ||
| $manager->persist($book2); | ||
| $book3 = new Book(); | ||
| $book3->setTitle('Dune'); | ||
| $book3->setAuthor('Frank Herbert'); | ||
| $manager->persist($book3); | ||
| $manager->flush(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace Light\Book\Handler; | ||
| use Laminas\Diactoros\Response\HtmlResponse; | ||
| use Light\Book\Repository\BookRepository; | ||
| use Mezzio\Template\TemplateRendererInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Server\RequestHandlerInterface; | ||
| class GetBooksHandler implements RequestHandlerInterface | ||
| { | ||
| public function __construct( | ||
| protected TemplateRendererInterface $template, | ||
| protected BookRepository $bookRepository, | ||
| ) { | ||
| } | ||
| public function handle(ServerRequestInterface $request): ResponseInterface | ||
alexmerlin marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| $titles = $this->bookRepository->getTitles(); | ||
| $authors = $this->bookRepository->getAuthors(); | ||
| return new HtmlResponse( | ||
| $this->template->render('page::books', [ | ||
| 'titles' => $titles, | ||
| 'authors' => $authors, | ||
| ]) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| namespace Light\Book; | ||
| use Light\Book\Handler\GetBooksHandler; | ||
| use Mezzio\Application; | ||
| use Psr\Container\ContainerInterface; | ||
| use function assert; | ||
| class RoutesDelegator | ||
| { | ||
| public function __invoke(ContainerInterface $container, string $serviceName, callable $callback): Application | ||
| { | ||
| $app = $callback(); | ||
| assert($app instanceof Application); | ||
| $app->get('/books', [GetBooksHandler::class], 'books::list'); | ||
| return $app; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.