|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Migrations; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager; |
|
6
|
|
|
use Doctrine\Migrations\Configuration\Migration\PhpFile; |
|
7
|
|
|
use Doctrine\Migrations\DependencyFactory; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Stu\Config\ConfigStageEnum; |
|
10
|
|
|
use Stu\Config\Init; |
|
11
|
|
|
|
|
12
|
|
|
class StuDependencyFactory extends DependencyFactory |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct() {} |
|
15
|
|
|
|
|
16
|
|
|
public static function createDependencyFactory(): DependencyFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array<string> */ |
|
19
|
|
|
$argv = $_SERVER['argv']; |
|
20
|
|
|
|
|
21
|
|
|
$configStage = ConfigStageEnum::from(self::popArgument('--stage=', $argv, ConfigStageEnum::PRODUCTION->value)); |
|
22
|
|
|
$configuration = self::popArgument('--configuration=', $argv, 'config/migrations/production.php'); |
|
23
|
|
|
|
|
24
|
|
|
return DependencyFactory::fromEntityManager( |
|
25
|
|
|
new PhpFile($configuration), |
|
26
|
|
|
new ExistingEntityManager(Init::getContainer($configStage) |
|
27
|
|
|
->get(EntityManagerInterface::class)) |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private static function popArgument(string $name, array $argv, string $default): string |
|
32
|
|
|
{ |
|
33
|
|
|
$filtered = array_filter($argv, fn(string $token): bool => str_starts_with($token, $name)); |
|
34
|
|
|
if (count($filtered) !== 1) { |
|
35
|
|
|
return $default; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
foreach ($filtered as $key => $value) { |
|
39
|
|
|
unset($_SERVER['argv'][$key]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return str_replace($name, '', current($filtered)); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|