-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathOptionStack.php
More file actions
194 lines (169 loc) · 5.16 KB
/
Copy pathOptionStack.php
File metadata and controls
194 lines (169 loc) · 5.16 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* This file is part of the Phalcon Migrations.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Phalcon\Migrations\Console;
use ArrayAccess;
use LogicException;
use Phalcon\Migrations\Mvc\Model\Migration as ModelMigration;
use Phalcon\Migrations\Version\IncrementalItem as IncrementalVersion;
use Phalcon\Migrations\Version\ItemCollection as VersionCollection;
use Phalcon\Migrations\Version\ItemInterface;
/**
* CLI options
*/
class OptionStack implements ArrayAccess
{
/**
* Parameters received by the script.
*
* @var array
*/
protected array $options = [];
/**
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* Get received options
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->options[$offset]);
}
/**
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->options[$offset] ?? '';
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value): void
{
$this->options[$offset] = $value;
}
/**
* @param mixed $offset
* @param mixed|null $default
*/
public function offsetSetDefault($offset, $default = null): void
{
if (!array_key_exists($offset, $this->options)) {
$this->options[$offset] = $default;
}
}
/**
* @param mixed $offset
* @param mixed|null $value
* @param mixed|null $default
*/
public function offsetSetOrDefault($offset, $value = null, $default = null): void
{
$this->options[$offset] = $value ?: $default;
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset): void
{
if (array_key_exists($offset, $this->options)) {
unset($this->options[$offset]);
}
}
/**
* Get prefix from the option
*
* @param string $prefix
* @param mixed $prefixEnd
*
* @return mixed
*/
public function getPrefixOption(string $prefix, $prefixEnd = '*')
{
if (substr($prefix, -1) != $prefixEnd) {
return '';
}
return substr($prefix, 0, -1);
}
/**
* Get version name to generate migration
*
* @return ItemInterface
*/
public function getVersionNameGeneratingMigration(): ItemInterface
{
/**
* Use timestamped version if description is provided.
*/
if (isset($this->options['descr']) || !empty($this->options['tsBased'] ?? false)) {
$this->options['version'] = (string) (int) (microtime(true) * pow(10, 6));
VersionCollection::setType(VersionCollection::TYPE_TIMESTAMPED);
$versionName = $this->options['version'] . ($this->options['descr'] ? '_' . $this->options['descr'] : '');
return VersionCollection::createItem($versionName);
}
VersionCollection::setType(VersionCollection::TYPE_INCREMENTAL);
$migrationsDirList = is_array($this->options['migrationsDir']) ? $this->options['migrationsDir'] : [];
/**
* Elsewhere, use old-style incremental versioning.
* The version is specified.
*/
if (isset($this->options['version'])) {
$versionItem = VersionCollection::createItem($this->options['version']);
// Check version if exists.
foreach ($migrationsDirList as $migrationsDir) {
$migrationsSubDirList = ModelMigration::scanForVersions($migrationsDir);
foreach ($migrationsSubDirList as $item) {
if ($item->getVersion() != $versionItem->getVersion()) {
continue;
}
if (!$this->options['force']) {
throw new LogicException('Version ' . $item->getVersion() . ' already exists');
} else {
rmdir(rtrim($migrationsDir, '\\/') . DIRECTORY_SEPARATOR . $versionItem->getVersion());
}
}
}
return $versionItem;
}
/**
* The version is guessed automatically
*/
$versionItems = [];
foreach ($migrationsDirList as $migrationsDir) {
$versionItems = $versionItems + ModelMigration::scanForVersions($migrationsDir);
}
if (!isset($versionItems[0])) {
$versionItem = VersionCollection::createItem('1.0.0');
} else {
/** @var IncrementalVersion $versionItem */
$versionItem = VersionCollection::maximum($versionItems);
$versionItem = $versionItem->addMinor(1);
}
return $versionItem;
}
}