1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract class for all Migrations |
7
|
|
|
* |
8
|
|
|
* @package PinkCrab\Perique\Migration\Plugin_Lifecycle |
9
|
|
|
* @author Glynn Quelch [email protected] |
10
|
|
|
* @since 0.0.1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PinkCrab\Perique\Migration\Event; |
14
|
|
|
|
15
|
|
|
use PinkCrab\DB_Migration\Migration_Manager; |
16
|
|
|
use PinkCrab\Perique\Migration\Migration; |
17
|
|
|
use PinkCrab\Plugin_Lifecycle\State_Event\Activation as State_Events_Activation; |
18
|
|
|
|
19
|
|
|
class Activation implements State_Events_Activation { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Holds the current Migration Manager and list of all migrations. |
23
|
|
|
* |
24
|
|
|
* @var Migration_Manager |
25
|
|
|
*/ |
26
|
|
|
protected Migration_Manager $migration_manager; |
27
|
|
|
|
28
|
|
|
public function __construct( Migration_Manager $migration_manager ) { |
29
|
|
|
$this->migration_manager = $migration_manager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create all table and seed those that allow. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function run(): void { |
38
|
|
|
$this->upsert_tables(); |
39
|
|
|
$this->seed_tables(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Upsert all tables in migration manager |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
private function upsert_tables(): void { |
48
|
|
|
$this->migration_manager->create_tables(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Seed all valid tables in the migration manager |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
private function seed_tables(): void { |
57
|
|
|
$this->migration_manager->seed_tables( |
58
|
|
|
...$this->tables_to_exclude_from_seeding() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets a list of all tables which should not be seeded. |
64
|
|
|
* |
65
|
|
|
* @return string[] Array of table names. |
66
|
|
|
*/ |
67
|
|
|
private function tables_to_exclude_from_seeding(): array { |
68
|
|
|
/** @var Migration[] */ |
69
|
|
|
$migrations = $this->migration_manager->get_migrations(); |
70
|
|
|
|
71
|
|
|
return array_values( |
72
|
|
|
array_map( |
73
|
|
|
function( Migration $migration ): string { |
74
|
|
|
return $migration->get_table_name(); |
75
|
|
|
}, |
76
|
|
|
array_filter( |
77
|
|
|
$migrations, |
78
|
|
|
function( Migration $migration ):bool { |
79
|
|
|
return count( $migration->get_seeds() ) === 0 |
80
|
|
|
|| false === $migration->seed_on_inital_activation(); |
81
|
|
|
} |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|