-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginBehavior.php
More file actions
398 lines (346 loc) · 12 KB
/
PluginBehavior.php
File metadata and controls
398 lines (346 loc) · 12 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Plugin Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('ModelBehavior', 'Model');
App::uses('File', 'Utility');
App::uses('Folder', 'Utility');
App::uses('Plugin', 'PluginManager.Model');
/**
* Plugin Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\PluginManager\Model\Behavior
*/
class PluginBehavior extends ModelBehavior {
/**
* Setup this behavior with the specified configuration settings.
*
* @param Model $model 呼び出し元のモデル
* @param array $config Configuration settings for $model
* @return void
*/
public function setup(Model $model, $config = array()) {
parent::setup($model, $config);
$this->connection = Hash::get($config, 'connection', $model->useDbConfig);
$this->corePluginPatterns = Hash::get($config, 'corePluginPatterns', [
'#^netcommons/#' => Plugin::PLUGIN_TYPE_CORE,
]);
$this->migrationPlugins = Hash::get($config, 'migrationPlugins', [
'cakephp-plugin',
]);
}
/**
* バージョンアップを実行
*
* @param Model $model 呼び出し元Model
* @param string $plugin Plugin key
* @return bool True on success
* @throws InternalErrorException
*/
public function runVersionUp(Model $model, $plugin) {
CakeLog::info(sprintf('[version up] Start version up "%s"', Hash::get($plugin, 'Plugin.name')));
try {
//トランザクションBegin
$model->begin();
if (! Hash::get($plugin, 'latest') && Hash::get($plugin, 'Plugin.id')) {
$pluginKey = Hash::get($plugin, 'Plugin.key');
if (! $model->uninstallPlugin($pluginKey)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$model->deleteOldPackageDir($plugin, true);
//img,js,cssをwebrootから削除。エラーとはしない
$model->deleteFromWebroot($plugin);
} else {
if (in_array(Hash::get($plugin, 'latest.packageType'), $this->migrationPlugins, true)) {
if (! $model->runMigration(Hash::get($plugin, 'latest.key'))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
//img,js,cssをwebrootにコピー。エラーとはしない
$model->copyToWebroot($plugin);
if (! $model->updateVersion(array(Hash::get($plugin, 'latest')))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$model->deleteOldPackageDir($plugin);
}
//キャッシュのクリア
$model->cacheClear();
CakeLog::info(
sprintf('[version up] Successfully version up "%s"', Hash::get($plugin, 'Plugin.name'))
);
//トランザクションCommit
$model->commit();
} catch (Exception $ex) {
CakeLog::info(
sprintf('[version up] Failure version up "%s"', Hash::get($plugin, 'Plugin.name'))
);
//トランザクションRollback
$model->rollback($ex);
}
return true;
}
/**
* Pluginのアンインストール
*
* @param Model $model 呼び出し元Model
* @param array $data Pluginデータ
* @return bool True on success
* @throws InternalErrorException
*/
public function uninstallPlugin(Model $model, $data) {
$model->loadModels([
'Plugin' => 'PluginManager.Plugin',
'PluginsRole' => 'PluginManager.PluginsRole',
'PluginsRoom' => 'PluginManager.PluginsRoom',
]);
if (is_string($data)) {
$key = $data;
} else {
$key = $data[$model->Plugin->alias]['key'];
}
CakeLog::info(sprintf('[uninstall] Start uninstall plugin "%s"', $key));
//トランザクションBegin
$model->begin();
try {
//Pluginの削除
if (! $model->Plugin->deleteAll(array($model->Plugin->alias . '.key' => $key), false)) {
CakeLog::info(sprintf('[uninstall] Error(' . __LINE__ . ')'));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//PluginsRoomの削除
$conditions = array($model->PluginsRoom->alias . '.plugin_key' => $key);
if (! $model->PluginsRoom->deleteAll($conditions, false)) {
CakeLog::info(sprintf('[uninstall] Error(' . __LINE__ . ')'));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//PluginsRoleの削除
$conditions = array($model->PluginsRole->alias . '.plugin_key' => $key);
if (! $model->PluginsRole->deleteAll($conditions, false)) {
CakeLog::info(sprintf('[uninstall] Error(' . __LINE__ . ')'));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
CakeLog::info(sprintf('[uninstall] Successfully uninstall plugin "%s"', $key));
//キャッシュのクリア
$model->Plugin->cacheClear();
//トランザクションCommit
$model->commit();
} catch (Exception $ex) {
//トランザクションRollback
CakeLog::info(sprintf('[uninstall] Failure uninstall plugin "%s"', $key));
$model->rollback($ex);
}
return true;
}
/**
* バージョンアップを実行
*
* @param Model $model 呼び出し元Model
* @param string $plugin Plugin key
* @param string|null $connection DB接続先
* @return bool True on success
*/
public function runMigration(Model $model, $plugin, $connection = null) {
if (! $connection) {
$connection = $this->connection;
}
return self::staticRunMigration($plugin, $connection);
}
/**
* Migrationの実行処理
*
* @param string $plugin Plugin key
* @param string|null $connection DB接続先
* @return bool True on success
*/
public static function staticRunMigration($plugin, $connection = null) {
$plugin = Inflector::camelize($plugin);
CakeLog::info(
sprintf('[migration] Start migrating "%s" for %s connection', $plugin, $connection)
);
$messages = array();
$ret = null;
$cmd = sprintf(
'cd %s && Console%scake Migrations.migration run all -p %s -c %s -i %s 2>&1',
ROOT . DS . APP_DIR, DS, escapeshellcmd($plugin), $connection, $connection
);
CakeLog::info('[migration] ' . $cmd);
exec($cmd, $messages, $ret);
// Write logs
foreach ($messages as $message) {
CakeLog::info(sprintf('[migration] %s', $message));
}
$result = true;
if ($ret) {
$matches = preg_grep('/No migrations/', $messages);
if (count($matches) === 0) {
CakeLog::info(
sprintf('[migration] Failure migrated "%s" for %s connection', $plugin, $connection)
);
$result = false;
} else {
//@codeCoverageIgnoreStart
//Migrationの戻り値が0になって処理が通らなくなったが、念のため処理として残しておく
CakeLog::info(
sprintf('[migration] Successfully migrated "%s" for %s connection', $plugin, $connection)
);
//@codeCoverageIgnoreEnd
}
} else {
$matches = preg_grep('/Error: |SQLSTATE/', $messages);
if (count($matches) === 0) {
CakeLog::info(
sprintf('[migration] Successfully migrated "%s" for %s connection', $plugin, $connection)
);
} else {
CakeLog::info(
sprintf('[migration] Failure migrated "%s" for %s connection', $plugin, $connection)
);
$result = false;
}
}
return $result;
}
/**
* パッケージのディレクトリ削除
*
* @param Model $model 呼び出し元Model
* @param string $plugin Plugin key
* @param bool $force 強制的に実行するかどうか
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @return bool True on success
*/
public function deleteOldPackageDir(Model $model, $plugin, $force = false) {
if (Hash::get($plugin, 'latest.originalSource') ==
Hash::get($plugin, 'Plugin.serialize_data.originalSource')) {
return true;
}
if (Hash::get($plugin, 'latest.packageType') === 'bower') {
$dirPath = WWW_ROOT . 'components' . DS;
$oldPath = $dirPath . Hash::get($plugin, 'Plugin.serialize_data.originalSource');
$newPath = $dirPath . Hash::get($plugin, 'latest.originalSource');
} elseif (Hash::get($plugin, 'latest.packageType') === 'cakephp-plugin') {
$oldPath = App::pluginPath(Inflector::camelize(Hash::get($plugin, 'Plugin.key')));
$newPath = '';
} else {
$dirPath = VENDORS;
$oldPath = $dirPath . strtr(Hash::get($plugin, 'Plugin.serialize_data.originalSource'), '/', DS);
$newPath = $dirPath . strtr(Hash::get($plugin, 'latest.originalSource'), '/', DS);
}
if ($force || $oldPath !== $newPath && file_exists($oldPath) && file_exists($newPath)) {
CakeLog::info(sprintf('[delete package files] Start delete package files "%s"', $oldPath));
CakeLog::info(var_export(Hash::get($plugin, 'latest.originalSource'), true));
CakeLog::info(var_export(Hash::get($plugin, 'Plugin.serialize_data.originalSource'), true));
$Folder = new Folder($oldPath);
$Folder->delete();
CakeLog::info(
sprintf('[delete package files] Successfully delete package files "%s"', $oldPath)
);
}
return true;
}
/**
* バージョン情報をDBに更新する
*
* @param Model $model 呼び出し元Model
* @param array $packages パッケージリスト
* @return bool
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function updateVersion(Model $model, $packages) {
$model->loadModels([
'Plugin' => 'PluginManager.Plugin',
]);
//トランザクションBegin
$model->Plugin->begin();
if (is_string($packages)) {
$package = $model->Plugin->find('first', array(
'recursive' => -1,
'conditions' => array(
'key' => $packages,
'language_id' => array(Current::read('Language.id'), '0')
),
));
$packages = array($package);
}
try {
foreach ($packages as $package) {
CakeLog::info(
sprintf('[update version] Start updated version data "%s"', $package['namespace'])
);
if ($package['namespace'] === 'netcommons/photo-albums') {
$conditions = array(
'namespace' => array('netcommons/photo-albums', 'netcommons/photo_albums')
);
} else {
$conditions = array('namespace' => $package['namespace']);
}
$count = $model->Plugin->find('count', array(
'recursive' => -1,
'conditions' => $conditions,
));
if ($count > 0) {
$update = array(
'version' => '\'' . $package['version'] . '\'',
'commit_version' => '\'' . $package['commit_version'] . '\'',
'commited' => '\'' . $package['commited'] . '\'',
'serialize_data' => $model->Plugin->getDataSource()->value(serialize($package), 'string'),
);
if (! $model->Plugin->updateAll($update, $conditions)) {
CakeLog::info(sprintf('[update version] Line(' . __LINE__ . ') Error'));
CakeLog::info(var_export($update, true));
CakeLog::info(var_export($conditions, true));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
} else {
$data = array(
'language_id' => '0',
'name' => $package['name'],
'key' => $package['key'],
'namespace' => $package['namespace'],
//'type' => $type,
'version' => $package['version'],
'commit_version' => $package['commit_version'],
'commited' => $package['commited'],
'serialize_data' => serialize($package),
);
foreach ($this->corePluginPatterns as $pattern => $type) {
if (preg_match($pattern, $package['namespace'])) {
$data['type'] = Hash::get($package, 'type', $type);
break;
}
}
if (! isset($data['type'])) {
$data['type'] = Hash::get($package, 'type');
}
$data['is_m17n'] = null;
$model->Plugin->create(false);
if (! $model->Plugin->save($data)) {
CakeLog::info(sprintf('[update version] Line(' . __LINE__ . ') Error'));
CakeLog::info(var_export($data, true));
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
CakeLog::info(
sprintf('[update version] Successfully updated "%s"', $package['namespace'])
);
}
//トランザクションCommit
$model->Plugin->commit();
} catch (Exception $ex) {
CakeLog::info(
sprintf('[update version] Failure updated "%s"', $package['namespace'])
);
//トランザクションRollback
$model->Plugin->rollback($ex);
}
return true;
}
}