-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MigrationHelper.php
144 lines (115 loc) · 4.14 KB
/
MigrationHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Migration\Tests\Support\Helper;
use Closure;
use Psr\Container\ContainerInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Files\FileHelper;
use Yiisoft\Db\Migration\Migrator;
use Yiisoft\Db\Migration\Service\Generate\CreateService;
use Yiisoft\Db\Migration\Service\MigrationService;
use function dirname;
final class MigrationHelper
{
public const PATH_ALIAS = '@runtime/migration-path';
public const NAMESPACE = 'Yiisoft\\Db\\Migration\\Tests\\runtime\\MigrationNamespace';
/**
* @return string The migrations directory
*/
public static function useMigrationsPath(ContainerInterface $container): string
{
$service = $container->get(MigrationService::class);
$service->setCreatePath(self::PATH_ALIAS);
$service->setUpdatePaths([self::PATH_ALIAS]);
self::preparePaths($container);
return self::getPathForMigrationPath($container);
}
/**
* @return string The migrations directory
*/
public static function useMigrationsNamespace(ContainerInterface $container): string
{
$service = $container->get(MigrationService::class);
$service->setCreateNamespace(self::NAMESPACE);
$service->setUpdateNamespaces([self::NAMESPACE]);
self::preparePaths($container);
return self::getPathForMigrationNamespace();
}
public static function findMigrationClassNameInOutput(string $output): string
{
preg_match('/.*\s+(M\d{12}\D\S*)\s+.*/m', $output, $matches);
return $matches[1] ?? '';
}
public static function createMigration(
ContainerInterface $container,
string $name,
string $command,
string $table,
array $fields = [],
Closure|null $callback = null
): string {
$migrationService = $container->get(MigrationService::class);
$createService = $container->get(CreateService::class);
$aliases = $container->get(Aliases::class);
$namespace = $migrationService->getCreateNamespace();
$className = $migrationService->generateClassName($name);
$content = $createService->run(
$command,
$table,
$className,
$namespace,
implode(',', $fields)
);
if ($callback) {
$content = $callback($content);
}
file_put_contents(
$aliases->get($migrationService->findMigrationPath()) . '/' . $className . '.php',
$content
);
return $namespace === ''
? $className
: ($namespace . '\\' . $className);
}
public static function createAndApplyMigration(
ContainerInterface $container,
string $name,
string $command,
string $table,
array $fields = [],
?Closure $callback = null
): string {
$className = self::createMigration($container, $name, $command, $table, $fields, $callback);
$migration = $container->get(MigrationService::class)->makeMigration($className);
$container->get(Migrator::class)->up($migration);
return $className;
}
public static function getPathForMigrationNamespace(): string
{
return dirname(__DIR__, 2) . '/runtime/MigrationNamespace';
}
private static function getPathForMigrationPath(ContainerInterface $container): string
{
return $container->get(Aliases::class)->get(self::PATH_ALIAS);
}
private static function preparePaths(ContainerInterface $container): void
{
$paths = [
self::getPathForMigrationNamespace(),
self::getPathForMigrationPath($container),
];
foreach ($paths as $path) {
file_exists($path)
? FileHelper::clearDirectory($path)
: mkdir($path);
}
}
public static function resetPathAndNamespace(ContainerInterface $container): void
{
$service = $container->get(MigrationService::class);
$service->setCreatePath('');
$service->setUpdatePaths([]);
$service->setCreateNamespace('');
$service->setUpdateNamespaces([]);
}
}