From 97e055e424e67f5edfd5183b8b7361d470a29923 Mon Sep 17 00:00:00 2001 From: Howriq Date: Fri, 5 Dec 2025 16:33:32 +0200 Subject: [PATCH 1/5] Code chapter 3 Signed-off-by: Howriq --- bin/doctrine-fixtures.php | 38 ++++++++++++++++ composer.json | 10 +++-- config/autoload/local.php | 1 + src/App/src/ConfigProvider.php | 2 + src/App/src/Factory/BookHandlerFactory.php | 32 ++++++++++++++ src/App/src/Factory/BookRepositoryFactory.php | 31 +++++++++++++ src/App/src/Fixture/BookLoader.php | 32 ++++++++++++++ src/App/templates/layout/default.html.twig | 1 + src/Book/src/ConfigProvider.php | 30 ++++++++++++- src/Book/src/Handler/GetBooksHandler.php | 34 +++++++++++++++ src/Book/src/Repository/BookRepository.php | 23 +++++++++- src/Book/src/RoutesDelegator.php | 24 +++++++++++ src/Page/templates/page/books.html.twig | 43 +++++++++++++++++++ 13 files changed, 295 insertions(+), 6 deletions(-) create mode 100644 bin/doctrine-fixtures.php create mode 100644 src/App/src/Factory/BookHandlerFactory.php create mode 100644 src/App/src/Factory/BookRepositoryFactory.php create mode 100644 src/App/src/Fixture/BookLoader.php create mode 100644 src/Book/src/Handler/GetBooksHandler.php create mode 100644 src/Book/src/RoutesDelegator.php create mode 100644 src/Page/templates/page/books.html.twig diff --git a/bin/doctrine-fixtures.php b/bin/doctrine-fixtures.php new file mode 100644 index 0000000..910a4e4 --- /dev/null +++ b/bin/doctrine-fixtures.php @@ -0,0 +1,38 @@ +#!/usr/bin/env php +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"; diff --git a/composer.json b/composer.json index 558d0f3..922850e 100644 --- a/composer.json +++ b/composer.json @@ -27,18 +27,20 @@ }, "require": { "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", + "doctrine/data-fixtures": "^2.2", "dotkernel/dot-cache": "^4.0", - "ramsey/uuid": "^4.5.0", - "ramsey/uuid-doctrine": "^2.1.0", - "roave/psr-container-doctrine": "^5.2.2 || ^6.0.0", "dotkernel/dot-errorhandler": "^4.4.0", "laminas/laminas-component-installer": "^3.5.0", "laminas/laminas-config-aggregator": "^1.17.0", "mezzio/mezzio": "^3.24.0", "mezzio/mezzio-fastroute": "^3.13.0", - "mezzio/mezzio-twigrenderer": "^2.17.0" + "mezzio/mezzio-twigrenderer": "^2.17.0", + "ramsey/uuid": "^4.5.0", + "ramsey/uuid-doctrine": "^2.1.0", + "roave/psr-container-doctrine": "^5.2.2 || ^6.0.0" }, "require-dev": { + "doctrine/doctrine-fixtures-bundle": "^4.3", "filp/whoops": "^2.17.0", "laminas/laminas-coding-standard": "^3.0.1", "laminas/laminas-development-mode": "^3.13.0", diff --git a/config/autoload/local.php b/config/autoload/local.php index a58f3f8..2ee32a4 100644 --- a/config/autoload/local.php +++ b/config/autoload/local.php @@ -41,6 +41,7 @@ 'page' => [ 'about' => 'about', 'who-we-are' => 'who-we-are', + 'books' => 'books', ], ], ]; diff --git a/src/App/src/ConfigProvider.php b/src/App/src/ConfigProvider.php index fd5a51b..d81db9b 100644 --- a/src/App/src/ConfigProvider.php +++ b/src/App/src/ConfigProvider.php @@ -56,6 +56,7 @@ * class: class-string, * }, * }, + * fixtures: non-empty-string, * migrations: array{ * migrations_paths: array, * all_or_nothing: bool, @@ -165,6 +166,7 @@ private function getDoctrineConfig(): array 'class' => MappingDriverChain::class, ], ], + 'fixtures' => getcwd() . '/src/App/src/Fixture', 'migrations' => [ 'table_storage' => [ 'table_name' => 'doctrine_migration_versions', diff --git a/src/App/src/Factory/BookHandlerFactory.php b/src/App/src/Factory/BookHandlerFactory.php new file mode 100644 index 0000000..3f01d3e --- /dev/null +++ b/src/App/src/Factory/BookHandlerFactory.php @@ -0,0 +1,32 @@ +get(BookRepository::class); + $template = $container->get(TemplateRendererInterface::class); + + assert($repository instanceof BookRepository); + assert($template instanceof TemplateRendererInterface); + + return new GetBooksHandler($template, $repository); + } +} diff --git a/src/App/src/Factory/BookRepositoryFactory.php b/src/App/src/Factory/BookRepositoryFactory.php new file mode 100644 index 0000000..e7c1e1d --- /dev/null +++ b/src/App/src/Factory/BookRepositoryFactory.php @@ -0,0 +1,31 @@ +get(EntityManager::class); + + $repository = $entityManager->getRepository(Book::class); + assert($repository instanceof BookRepository); + + return $repository; + } +} diff --git a/src/App/src/Fixture/BookLoader.php b/src/App/src/Fixture/BookLoader.php new file mode 100644 index 0000000..4960abb --- /dev/null +++ b/src/App/src/Fixture/BookLoader.php @@ -0,0 +1,32 @@ +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(); + } +} diff --git a/src/App/templates/layout/default.html.twig b/src/App/templates/layout/default.html.twig index 6a49363..afdddaa 100644 --- a/src/App/templates/layout/default.html.twig +++ b/src/App/templates/layout/default.html.twig @@ -46,6 +46,7 @@