fix: provisions DB on container startup

This commit is contained in:
2025-05-16 13:15:28 -05:00
parent cc4942a537
commit 7bc8720377
9 changed files with 231 additions and 2 deletions

View File

@@ -6,10 +6,19 @@ use App\User\Action\Command\RegisterUserCommand;
use App\User\Action\Handler\RegisterUserHandler;
use App\User\Framework\Entity\User;
use App\User\Framework\Form\RegistrationFormType;
use App\User\Framework\Pipeline\GettingStarted\AddPreferencesToDatabase;
use App\User\Framework\Pipeline\GettingStarted\GettingStartedInput;
use App\User\Framework\Pipeline\GettingStarted\MigrateDatabase;
use App\User\Framework\Repository\PreferencesRepository;
use App\User\Framework\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use League\Pipeline\Pipeline;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
@@ -45,8 +54,12 @@ class RegistrationController extends AbstractController
}
#[Route(path: '/getting-started', name: 'app_getting_started')]
public function gettingStarted(Request $request, Security $security): Response
public function gettingStarted(Request $request, Security $security, UserRepository $userRepository, PreferencesRepository $preferencesRepository, KernelInterface $kernel, LoggerInterface $logger): Response
{
if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) {
return $this->redirectToRoute('app_index');
}
$form = $this->createForm(RegistrationFormType::class, new User());
$form->handleRequest($request);

View File

@@ -0,0 +1,36 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use App\User\Framework\Repository\PreferencesRepository;
use League\Pipeline\StageInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class AddPreferencesToDatabase implements StageInterface
{
/** @var GettingStartedInput $input */
public function __invoke($input)
{
if ($input->preferenceRepository->count() > 0) {
$input->logger->info('[AddPreferencesToDatabase] > Preferences already exist, skipping...');
return $input;
}
$app = new Application($input->kernel);
$app->setAutoExit(false);
$commandInput = new ArrayInput([
'command' => 'doctrine:fixtures:load',
'--no-interaction',
]);
$output = new BufferedOutput();
$app->run($commandInput, $output);
return $input;
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use App\User\Framework\Repository\PreferencesRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class GettingStartedInput
{
public function __construct(
public readonly KernelInterface $kernel,
public readonly PreferencesRepository $preferenceRepository,
public readonly LoggerInterface $logger
) {}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\User\Framework\Pipeline\GettingStarted;
use League\Pipeline\StageInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class MigrateDatabase implements StageInterface
{
/** @var GettingStartedInput $input */
public function __invoke($input)
{
$app = new Application($input->kernel);
$app->setAutoExit(false);
$commandInput = new ArrayInput([
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true,
]);
$output = new BufferedOutput();
$app->run($commandInput, $output);
$input->logger->info('[MigrateDatabase] > Migrating database...', ['output' => $output->fetch()]);
return $input;
}
}