Files
slim-blog/src/Shared/Database/Provisioner.php
2026-03-16 01:47:07 +01:00

38 lines
956 B
PHP

<?php
declare(strict_types=1);
namespace App\Shared\Database;
use PDO;
/**
* Orchestration du provisionnement de la base de données.
*
* Exécute les migrations puis le seeding éventuel sous verrou fichier
* afin d'éviter les exécutions concurrentes au démarrage ou en CLI.
*/
final class Provisioner
{
public static function run(PDO $db): void
{
$lockPath = __DIR__ . '/../../../database/.provision.lock';
$handle = fopen($lockPath, 'c+');
if ($handle === false) {
throw new \RuntimeException('Impossible d\'ouvrir le verrou de provisionnement');
}
try {
if (!flock($handle, LOCK_EX)) {
throw new \RuntimeException('Impossible d\'obtenir le verrou de provisionnement');
}
Migrator::run($db);
Seeder::seed($db);
} finally {
flock($handle, LOCK_UN);
fclose($handle);
}
}
}