1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Modules\Cli\Tasks; |
12
|
|
|
|
13
|
|
|
use Phalcon\Db\Adapter\Pdo\Mysql; |
14
|
|
|
use Zemit\Models\Lang; |
15
|
|
|
use Zemit\Models\Site; |
16
|
|
|
use Zemit\Models\UserRole; |
17
|
|
|
use Zemit\Modules\Cli\Task; |
18
|
|
|
use Zemit\Models\Role; |
19
|
|
|
use Zemit\Models\User; |
20
|
|
|
use Zemit\Models\Setting; |
21
|
|
|
use Zemit\Models\Template; |
22
|
|
|
use Zemit\Utils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class DeploymentTask |
26
|
|
|
* |
27
|
|
|
* @author Julien Turbide <[email protected]> |
28
|
|
|
* @copyright Zemit Team <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @since 1.0 |
31
|
|
|
* @version 1.0 |
32
|
|
|
* |
33
|
|
|
* @package Zemit\Modules\Cli\Tasks |
34
|
|
|
*/ |
35
|
|
|
class DeploymentTask extends Task |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $consoleDoc = <<<DOC |
41
|
|
|
Usage: |
42
|
|
|
php zemit cli deployment <action> [<params> ...] |
43
|
|
|
|
44
|
|
|
Options: |
45
|
|
|
task: cache |
46
|
|
|
action: clear |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
DOC; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Tables to truncate |
53
|
|
|
* @var array Raw DB table sources |
54
|
|
|
*/ |
55
|
|
|
private $_truncates = [ |
56
|
|
|
'audit', |
57
|
|
|
'audit_detail', |
58
|
|
|
'category', |
59
|
|
|
'channel', |
60
|
|
|
'data', |
61
|
|
|
'email', |
62
|
|
|
'email_file', |
63
|
|
|
'field', |
64
|
|
|
'file', |
65
|
|
|
'file_relation', |
66
|
|
|
'flag', |
67
|
|
|
'group', |
68
|
|
|
'group_role', |
69
|
|
|
'group_type', |
70
|
|
|
'lang', |
71
|
|
|
'log', |
72
|
|
|
'menu', |
73
|
|
|
'meta', |
74
|
|
|
'page', |
75
|
|
|
'phalcon_migrations', |
76
|
|
|
'post', |
77
|
|
|
'post_category', |
78
|
|
|
'role', |
79
|
|
|
'session', |
80
|
|
|
'setting', |
81
|
|
|
'site', |
82
|
|
|
'site_lang', |
83
|
|
|
'template', |
84
|
|
|
'translate', |
85
|
|
|
'translate_field', |
86
|
|
|
'translate_table', |
87
|
|
|
'type', |
88
|
|
|
'user', |
89
|
|
|
'user_group', |
90
|
|
|
'user_role', |
91
|
|
|
'user_type', |
92
|
|
|
'validator', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Deprecated tables |
97
|
|
|
* @var array Raw DB tables sources to drop |
98
|
|
|
*/ |
99
|
|
|
private $_drops = [ |
|
|
|
|
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
public $_insert = [ |
103
|
|
|
Role::class => [ |
104
|
|
|
['index' => 'everyone', 'labelFr' => 'Tous', 'labelEn' => 'Everyone'], |
105
|
|
|
['index' => 'guest', 'labelFr' => 'Invité', 'labelEn' => 'Guest'], |
106
|
|
|
['index' => 'user', 'labelFr' => 'Utilisateur', 'labelEn' => 'User'], |
107
|
|
|
['index' => 'admin', 'labelFr' => 'Administrateur', 'labelEn' => 'Administrator'], |
108
|
|
|
['index' => 'dev', 'labelFr' => 'Développeur', 'labelEn' => 'Developer'], |
109
|
|
|
], |
110
|
|
|
User::class => [ |
111
|
|
|
['username' => 'dev', 'email' => '[email protected]', 'firstName' => 'Developer', 'lastName' => 'Zemit'], |
112
|
|
|
], |
113
|
|
|
Lang::class => [ |
114
|
|
|
['label' => 'Francais', 'code' => 'fr'], |
115
|
|
|
['label' => 'English', 'code' => 'en'], |
116
|
|
|
['label' => 'Spanish', 'code' => 'sp'], |
117
|
|
|
], |
118
|
|
|
Site::class => [ |
119
|
|
|
['name' => 'Zemit CMS', 'title' => 'Zemit CMS', 'description' => ''], |
120
|
|
|
], |
121
|
|
|
Template::class => [ |
122
|
|
|
], |
123
|
|
|
Setting::class => [ |
124
|
|
|
], |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
public function initialize() |
128
|
|
|
{ |
129
|
|
|
Utils::setUnlimitedRuntime(); |
130
|
|
|
|
131
|
|
|
// force migration status to the config |
132
|
|
|
$this->config->app['migration'] = true; |
|
|
|
|
133
|
|
|
|
134
|
|
|
// force permissions to the config |
135
|
|
|
$permissions = []; |
136
|
|
|
$permissions[Site::class] = ['*']; |
137
|
|
|
$permissions[Lang::class] = ['*']; |
138
|
|
|
$permissions[User::class] = ['*']; |
139
|
|
|
$permissions[Role::class] = ['*']; |
140
|
|
|
$permissions[UserRole::class] = ['*']; |
141
|
|
|
$permissions[Template::class] = ['*']; |
142
|
|
|
$permissions[Setting::class] = ['*']; |
143
|
|
|
$this->config->permissions->roles->everyone->models = $permissions; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function helpAction() |
147
|
|
|
{ |
148
|
|
|
echo $this->consoleDoc; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function mainAction() |
156
|
|
|
{ |
157
|
|
|
$response = []; |
158
|
|
|
|
159
|
|
|
$response ['truncate'] = $this->truncateAction(); |
160
|
|
|
$response ['insert'] = $this->insertAction(); |
161
|
|
|
|
162
|
|
|
return $response; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Truncate tables before inserts |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
public function truncateAction() |
170
|
|
|
{ |
171
|
|
|
$response = []; |
172
|
|
|
|
173
|
|
|
/** @var Mysql $db */ |
174
|
|
|
$db = $this->getDi()->getShared('db'); |
175
|
|
|
|
176
|
|
|
foreach ($this->_truncates as $table) { |
177
|
|
|
$response [] = $db->execute('TRUNCATE TABLE ' . $db->escapeIdentifier($table)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// double loop for possible fk dependencies |
181
|
|
|
// foreach ($this->_truncates as $table1) { |
182
|
|
|
// foreach ($this->_truncates as $table2) { |
183
|
|
|
// $response [] = $db->execute('TRUNCATE TABLE ' . $db->escapeIdentifier($table1)); |
184
|
|
|
// $response [] = $db->execute('TRUNCATE TABLE ' . $db->escapeIdentifier($table2)); |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
// |
188
|
|
|
// // double loop for possible fk dependencies |
189
|
|
|
// foreach ($this->_drops as $table1) { |
190
|
|
|
// foreach ($this->_drops as $table2) { |
191
|
|
|
// $response [] = $db->execute('DROP TABLE IF EXISTS ' . $db->escapeIdentifier($table1)); |
192
|
|
|
// $response [] = $db->execute('DROP TABLE IF EXISTS ' . $db->escapeIdentifier($table2)); |
193
|
|
|
// } |
194
|
|
|
// } |
195
|
|
|
|
196
|
|
|
return $response; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Insert default data |
201
|
|
|
* @return array[] |
202
|
|
|
*/ |
203
|
|
|
public function insertAction() |
204
|
|
|
{ |
205
|
|
|
$response = [ |
206
|
|
|
'saved' => [], |
207
|
|
|
'error' => [], |
208
|
|
|
'message' => [], |
209
|
|
|
]; |
210
|
|
|
|
211
|
|
|
foreach ($this->_insert as $modelName => $insert) { |
212
|
|
|
|
213
|
|
|
foreach ($insert as $row) { |
214
|
|
|
$entity = new $modelName(); |
215
|
|
|
$entity->assign($row); |
216
|
|
|
|
217
|
|
|
if ($modelName === User::class) { |
218
|
|
|
if (empty($row['password'])) { |
219
|
|
|
$entity->assign(['password' => $row['username'], 'passwordConfirm' => $row['username']]); |
220
|
|
|
} |
221
|
|
|
$entity->RoleList = [Role::findFirstByIndex($entity->getUsername())]; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$saved = $entity->save(); |
225
|
|
|
// $response ['saved'] [] = $saved; |
226
|
|
|
|
227
|
|
|
if (!$saved) { |
228
|
|
|
$response['error'] [] = $entity->toArray(); |
229
|
|
|
foreach ($entity->getMessages() as $message) { |
230
|
|
|
$response['message'][] = $message; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $response; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|