diff --git a/bin/doctrine-fixtures b/bin/doctrine-fixtures new file mode 100644 index 0000000..910a4e4 --- /dev/null +++ b/bin/doctrine-fixtures @@ -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/bin/doctrine-migrations.php b/bin/doctrine-migrations similarity index 100% rename from bin/doctrine-migrations.php rename to bin/doctrine-migrations diff --git a/composer.json b/composer.json index 558d0f3..47fb533 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,8 @@ }, "require": { "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", + "doctrine/data-fixtures": "^2.2", + "doctrine/doctrine-fixtures-bundle": "^4.3", "dotkernel/dot-cache": "^4.0", "ramsey/uuid": "^4.5.0", "ramsey/uuid-doctrine": "^2.1.0", diff --git a/config/autoload/local.php b/config/autoload/local.php deleted file mode 100644 index a58f3f8..0000000 --- a/config/autoload/local.php +++ /dev/null @@ -1,46 +0,0 @@ - [ - 'host' => 'localhost', - 'dbname' => 'light', - 'user' => 'root', - 'password' => '123', - 'port' => 3306, - 'driver' => 'pdo_mysql', - 'charset' => 'utf8mb4', - 'collate' => 'utf8mb4_general_ci', - ], - // you can add more database connections into this array -]; - -return [ - 'databases' => $databases, - 'doctrine' => [ - 'connection' => [ - 'orm_default' => [ - 'params' => $databases['default'], - ], - ], - ], - 'application' => [ - 'url' => $baseUrl, - ], - 'routes' => [ - 'page' => [ - 'about' => 'about', - 'who-we-are' => 'who-we-are', - ], - ], -]; 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 @@