1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Modules\Cli\Tasks\Traits; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\ModelInterface; |
15
|
|
|
use Zemit\Exception\CliException; |
16
|
|
|
use Zemit\Utils; |
17
|
|
|
|
18
|
|
|
trait DeploymentTrait |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Default action |
24
|
|
|
* @throws CliException |
25
|
|
|
*/ |
26
|
|
|
public function mainAction(): ?array |
27
|
|
|
{ |
28
|
|
|
$response = []; |
29
|
|
|
|
30
|
|
|
$response ['truncate'] = $this->truncateAction(); |
31
|
|
|
$response ['drop'] = $this->dropAction(); |
32
|
|
|
$response ['engine'] = $this->fixEngineAction(); |
33
|
|
|
$response ['insert'] = $this->insertAction(); |
34
|
|
|
|
35
|
|
|
return $response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Truncate tables |
40
|
|
|
*/ |
41
|
|
|
public function truncateAction(): array |
42
|
|
|
{ |
43
|
|
|
$response = []; |
44
|
|
|
|
45
|
|
|
foreach ($this->truncate as $table) { |
46
|
|
|
$response [] = $this->db->execute('TRUNCATE TABLE ' . $this->db->escapeIdentifier($table)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Drops tables |
54
|
|
|
*/ |
55
|
|
|
public function dropAction(): array |
56
|
|
|
{ |
57
|
|
|
$response = []; |
58
|
|
|
|
59
|
|
|
foreach ($this->drop as $table) { |
60
|
|
|
$response [] = $this->db->execute('DROP TABLE IF EXISTS ' . $this->db->escapeIdentifier($table)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Fix tables engine |
68
|
|
|
*/ |
69
|
|
|
public function fixEngineAction(): array |
70
|
|
|
{ |
71
|
|
|
$response = []; |
72
|
|
|
|
73
|
|
|
foreach ($this->engine as $table => $engine) { |
74
|
|
|
$response [] = $this->db->execute('ALTER TABLE ' . $this->db->escapeIdentifier($table) . ' ENGINE = ' . $engine); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Insert records |
82
|
|
|
* @throws CliException |
83
|
|
|
*/ |
84
|
|
|
public function insertAction(?string $models = null): array |
85
|
|
|
{ |
86
|
|
|
$response = [ |
87
|
|
|
'saved' => 0, |
88
|
|
|
'error' => [], |
89
|
|
|
'message' => [], |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$models = (!empty($models))? explode(',', $models) : null; |
93
|
|
|
|
94
|
|
|
foreach ($this->insert as $modelName => $insert) { |
95
|
|
|
if (is_array($models) && !in_array($modelName, $models, true)) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($insert as $key => $row) { |
100
|
|
|
$entity = new $modelName(); |
101
|
|
|
assert($entity instanceof ModelInterface); |
102
|
|
|
|
103
|
|
|
$assign = isset($row[0]) ? array_combine($entity->columnMap(), $row) : $row; |
|
|
|
|
104
|
|
|
if (!$assign) { |
105
|
|
|
throw new CliException('Can\'t assign row #' . $key . ' for model `' . $modelName . '`.'); |
106
|
|
|
} else { |
107
|
|
|
$entity->assign($assign); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Automagically fill passwords |
111
|
|
|
if (property_exists($entity, 'password')) { |
112
|
|
|
if (empty($row['password']) && property_exists($entity, 'username')) { |
113
|
|
|
$entity->assign(['password' => $row['username'], 'passwordConfirm' => $row['username']]); |
114
|
|
|
} |
115
|
|
|
elseif (empty($row['passwordConfirm'])) { |
116
|
|
|
$entity->assign(['passwordConfirm' => $row['password']]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!$entity->save()) { |
121
|
|
|
$response['error'][$modelName][] = $entity->toArray(); |
122
|
|
|
|
123
|
|
|
foreach ($entity->getMessages() as $message) { |
124
|
|
|
$response['message'][$modelName][] = $message; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
else { |
128
|
|
|
$response['saved']++; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $response; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function addModelsPermissions(?array $tables = null): void |
137
|
|
|
{ |
138
|
|
|
$permissions = []; |
139
|
|
|
$tables ??= $this->insert; |
140
|
|
|
foreach ($tables as $model => $entity) { |
141
|
|
|
$permissions[$model] = ['*']; |
142
|
|
|
} |
143
|
|
|
$this->config->merge([ |
144
|
|
|
'permissions' => [ |
145
|
|
|
'roles' => [ |
146
|
|
|
'cli' => [ |
147
|
|
|
'models' => $permissions |
148
|
|
|
], |
149
|
|
|
] |
150
|
|
|
] |
151
|
|
|
]); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|