-
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
Draft
Howriq
wants to merge
5
commits into
chapter-2
Choose a base branch
from
chapter-3
base: chapter-2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Chapter 3 code #17
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| 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"; | ||
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
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
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
| 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); | ||
| } | ||
| } |
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
| 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; | ||
| } | ||
| } |
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
| 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(); | ||
| } | ||
| } |
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
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
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
| 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.
Show resolved
Hide resolved
|
||
| { | ||
| $titles = $this->bookRepository->getTitles(); | ||
| $authors = $this->bookRepository->getAuthors(); | ||
|
|
||
| return new HtmlResponse( | ||
| $this->template->render('page::books', [ | ||
| 'titles' => $titles, | ||
| 'authors' => $authors, | ||
| ]) | ||
| ); | ||
| } | ||
| } | ||
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
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
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.