1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of orm |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Orm\OrmModule\EventHandlers; |
13
|
|
|
|
14
|
|
|
use Slick\Orm\OrmModule; |
15
|
|
|
use Slick\Orm\OrmModule\ModuleEventHandler; |
16
|
|
|
use Slick\WebStack\Infrastructure\ComposerParser; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* MigrationsHandler |
20
|
|
|
* |
21
|
|
|
* @package Slick\Orm\OrmModule\EventHandlers |
22
|
|
|
*/ |
23
|
|
|
final class MigrationsHandler implements ModuleEventHandler |
24
|
|
|
{ |
25
|
|
|
/** @var array<string, mixed> */ |
26
|
|
|
private static array $defaultSettings = [ |
27
|
|
|
'table_storage' => [ |
28
|
|
|
'table_name' => 'doctrine_migration_versions', |
29
|
|
|
'version_column_name' => 'version', |
30
|
|
|
'version_column_length' => 192, |
31
|
|
|
'executed_at_column_name' => 'executed_at', |
32
|
|
|
'execution_time_column_name' => 'execution_time', |
33
|
|
|
], |
34
|
|
|
|
35
|
|
|
'migrations_paths' => null, |
36
|
|
|
|
37
|
|
|
'all_or_nothing' => true, |
38
|
|
|
'transactional' => true, |
39
|
|
|
'check_database_platform' => true, |
40
|
|
|
'organize_migrations' => 'none', |
41
|
|
|
'connection' => null, |
42
|
|
|
'em' => null, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
public function __construct(private readonly ComposerParser $composerParser) |
46
|
|
|
{ |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function onEnable(array $context = []): void |
53
|
|
|
{ |
54
|
|
|
if (is_file(OrmModule::$migrationCnfFile)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$settings = self::$defaultSettings; |
59
|
|
|
$namespace = ''; |
60
|
|
|
$namespaces = $this->composerParser->psr4Namespaces(); |
61
|
|
|
if (!empty($namespaces)) { |
62
|
|
|
$namespace = reset($namespaces); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$settings['migrations_paths'] = (object) ["{$namespace}Migrations" => '../lib/Migrations']; |
66
|
|
|
if (!is_dir(OrmModule::$appConfig)) { |
67
|
|
|
mkdir(OrmModule::$appConfig, 0755, true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!is_dir(APP_ROOT . '/lib/Migrations')) { |
71
|
|
|
mkdir(APP_ROOT . '/lib/Migrations', 0755, true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
file_put_contents(OrmModule::$migrationCnfFile, json_encode($settings, JSON_PRETTY_PRINT)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function onDisable(array $context = []): void |
81
|
|
|
{ |
82
|
|
|
if (!$context['purge']) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (is_file(OrmModule::$migrationCnfFile)) { |
87
|
|
|
unlink(OrmModule::$migrationCnfFile); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|