Setup.php
1.85 KB
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
<?php
namespace AC\Plugin;
use AC\Storage\KeyValuePair;
abstract class Setup {
/**
* @var KeyValuePair
*/
private $storage;
/**
* @var Version
*/
private $version;
/**
* @var InstallCollection
*/
private $installers;
/**
* @var UpdateCollection
*/
private $updates;
public function __construct(
KeyValuePair $storage,
Version $version,
InstallCollection $installers,
UpdateCollection $updates
) {
$this->storage = $storage;
$this->version = $version;
$this->installers = $installers;
$this->updates = $updates;
}
/**
* @param Version $version
*
* @return void
*/
protected function update_stored_version( Version $version ) {
$this->storage->save( (string) $version );
}
/**
* @return Version
*/
protected function get_stored_version() {
return new Version( (string) $this->storage->get() );
}
private function update_stored_version_to_current() {
$this->update_stored_version( $this->version );
}
/**
* @return bool
*/
abstract protected function is_new_install();
private function install() {
foreach ( $this->installers as $installer ) {
$installer->install();
}
$this->update_stored_version_to_current();
}
/**
* @return void
*/
private function update() {
foreach ( $this->updates as $update ) {
if ( ! $update->needs_update( $this->get_stored_version() ) ) {
continue;
}
$update->apply_update();
$this->update_stored_version( $update->get_version() );
}
$this->update_stored_version_to_current();
}
/**
* @param bool $force_install
*
* @return void
*/
public function run( $force_install = false ) {
if ( $force_install === true ) {
$this->install();
}
if ( $this->version->is_equal( $this->get_stored_version() ) ) {
return;
}
if ( $this->is_new_install() ) {
$this->install();
} else {
$this->update();
}
}
}