|
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(): array |
|
85
|
|
|
{ |
|
86
|
|
|
$response = [ |
|
87
|
|
|
'saved' => 0, |
|
88
|
|
|
'error' => [], |
|
89
|
|
|
'message' => [], |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($this->insert as $modelName => $insert) { |
|
93
|
|
|
|
|
94
|
|
|
foreach ($insert as $key => $row) { |
|
95
|
|
|
$entity = new $modelName(); |
|
96
|
|
|
assert($entity instanceof ModelInterface); |
|
97
|
|
|
|
|
98
|
|
|
$assign = isset($row[0]) ? array_combine($entity->columnMap(), $row) : $row; |
|
|
|
|
|
|
99
|
|
|
if (!$assign) { |
|
100
|
|
|
throw new CliException('Can\'t assign row #' . $key . ' for model `' . $modelName . '`.'); |
|
101
|
|
|
} else { |
|
102
|
|
|
$entity->assign($assign); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Automagically fill passwords |
|
106
|
|
|
if (property_exists($entity, 'password')) { |
|
107
|
|
|
if (empty($row['password'])) { |
|
108
|
|
|
$entity->assign(['password' => $row['username'], 'passwordConfirm' => $row['username']]); |
|
109
|
|
|
} |
|
110
|
|
|
elseif (empty($row['passwordConfirm'])) { |
|
111
|
|
|
$entity->assign(['passwordConfirm' => $row['password']]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!$entity->save()) { |
|
116
|
|
|
$response['error'][$modelName][] = $entity->toArray(); |
|
117
|
|
|
|
|
118
|
|
|
foreach ($entity->getMessages() as $message) { |
|
119
|
|
|
$response['message'][$modelName][] = $message; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
else { |
|
123
|
|
|
$response['saved']++; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $response; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function addModelsPermissions(?array $tables = null): void |
|
132
|
|
|
{ |
|
133
|
|
|
$permissions = []; |
|
134
|
|
|
$tables ??= $this->insert; |
|
135
|
|
|
foreach ($tables as $model => $entity) { |
|
136
|
|
|
$permissions[$model] = ['*']; |
|
137
|
|
|
} |
|
138
|
|
|
$this->config->merge([ |
|
139
|
|
|
'permissions' => [ |
|
140
|
|
|
'roles' => [ |
|
141
|
|
|
'cli' => [ |
|
142
|
|
|
'models' => $permissions |
|
143
|
|
|
], |
|
144
|
|
|
] |
|
145
|
|
|
] |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|