|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Liip\MonitorBundle\DependencyInjection\DoctrineMigrations; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Connection\ExistingConnection; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\Migration\JsonFile; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Migration\PhpFile; |
|
10
|
|
|
use Doctrine\Migrations\Configuration\Migration\XmlFile; |
|
11
|
|
|
use Doctrine\Migrations\Configuration\Migration\YamlFile; |
|
12
|
|
|
use Doctrine\Migrations\DependencyFactory; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class V3MigrationsLoader. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class V3MigrationsLoader extends AbstractDoctrineMigrationsLoader |
|
21
|
|
|
{ |
|
22
|
|
|
public function createMigrationConfigurationService( |
|
23
|
|
|
ContainerBuilder $container, |
|
24
|
|
|
string $connectionName, |
|
25
|
|
|
string $serviceId, |
|
26
|
|
|
string $filename = null |
|
27
|
|
|
): void { |
|
28
|
|
|
if (null !== $filename) { |
|
29
|
|
|
$configurationClass = $this->getConfigurationLoaderClass($container, $filename); |
|
30
|
|
|
$filenameHash = md5($filename); |
|
31
|
|
|
$configurationServiceId = 'liip_monitor.check.doctrine_migrations.configuration.'.$filenameHash; |
|
32
|
|
|
if (!$container->has($configurationServiceId)) { |
|
33
|
|
|
$container->register($configurationServiceId, $configurationClass) |
|
34
|
|
|
->setPublic(false) |
|
35
|
|
|
->setArguments([$filename]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$connectionLoaderId = 'liip_monitor.check.doctrine_migrations.connection_loader.'.$connectionName; |
|
39
|
|
|
if (!$container->has($connectionLoaderId)) { |
|
40
|
|
|
$container->register($connectionLoaderId, ExistingConnection::class) |
|
41
|
|
|
->setPublic(false) |
|
42
|
|
|
->setArguments([new Reference(sprintf('doctrine.dbal.%s_connection', $connectionName))]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$dependencyFactoryId = sprintf( |
|
46
|
|
|
'liip_monitor.check.doctrine_migrations.dependency_factory.%s.%s', |
|
47
|
|
|
$connectionName, |
|
48
|
|
|
$filenameHash |
|
49
|
|
|
); |
|
50
|
|
|
if (!$container->has($dependencyFactoryId)) { |
|
51
|
|
|
$container->register($dependencyFactoryId, DependencyFactory::class) |
|
52
|
|
|
->setFactory([DependencyFactory::class, 'fromConnection']) |
|
53
|
|
|
->setPublic(false) |
|
54
|
|
|
->setArguments([new Reference($configurationServiceId), new Reference($connectionLoaderId)]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$container->setAlias($serviceId, new Alias($dependencyFactoryId, true)); |
|
58
|
|
|
|
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$container->setAlias($serviceId, new Alias('doctrine.migrations.dependency_factory', true)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Creates in-memory migration configuration for setting up container service. |
|
67
|
|
|
* |
|
68
|
|
|
* @param ContainerBuilder $container The container |
|
69
|
|
|
* @param string $filename Migrations configuration file |
|
70
|
|
|
* |
|
71
|
|
|
* @return string FQCN of configuration class loader |
|
72
|
|
|
*/ |
|
73
|
|
|
private function getConfigurationLoaderClass(ContainerBuilder $container, string $filename): string |
|
74
|
|
|
{ |
|
75
|
|
|
// Available options are located under Doctrine\Migrations\Configuration\Migration namespace |
|
76
|
|
|
$map = [ |
|
77
|
|
|
'xml' => XmlFile::class, |
|
78
|
|
|
'yaml' => YamlFile::class, |
|
79
|
|
|
'yml' => YamlFile::class, |
|
80
|
|
|
'php' => PhpFile::class, |
|
81
|
|
|
'json' => JsonFile::class, |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
$filename = $container->getParameterBag()->resolveValue($filename); |
|
85
|
|
|
$info = pathinfo($filename); |
|
86
|
|
|
|
|
87
|
|
|
$extension = $info['extension'] ?? ''; |
|
88
|
|
|
if (empty($map[$extension])) { |
|
89
|
|
|
throw new \InvalidArgumentException(sprintf('Config file type "%s" is not supported', $extension)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $map[$info['extension']]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|