-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanUpShell.php
More file actions
160 lines (141 loc) · 3.7 KB
/
CleanUpShell.php
File metadata and controls
160 lines (141 loc) · 3.7 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
<?php
/**
* CleanUp Shell
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Current', 'NetCommons.Utility');
App::uses('CleanUp', 'CleanUp.Model');
App::uses('CleanUpLog', 'CleanUp.Lib');
App::uses('CleanUpLockFile', 'CleanUp.Lib');
/**
* CleanUp Shell
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\CleanUp\Console\Command
* @property CleanUp $CleanUp
*/
class CleanUpShell extends AppShell {
/**
* 全てのプラグインを指定
*
* @var string
*/
const PLUGIN_KEY_ALL = 'all';
/**
* use model
*
* @var array
*/
public $uses = [
'CleanUp.CleanUp',
];
/**
* construct
*
* @return void
*/
public function __construct() {
parent::__construct();
// とりあえず2:日本語をセット
Current::write('Language.id', '2');
//Configure::write('Config.language', 'fra');
}
/**
* ファイルクリーンアップ
* ### コマンド例
* ```
* Console/cake clean_up.clean_up clean_up all
* ```
*
* @return void
* @throws Exception
* @see http://www.php.net/manual/ja/info.configuration.php#ini.max-execution-time max_execution_time PHP を コマンドライン から実行する場合のデフォルト設定は 0 です。
*/
public function clean_up() {
$pluginKeys = $this->args;
$data = [];
if (array_search(self::PLUGIN_KEY_ALL, $pluginKeys)) {
// プラグインキーの一覧
$data['CleanUp']['plugin_key'] = $this->__getPluginKeys();
} else {
$data['CleanUp']['plugin_key'] = $pluginKeys;
}
if ($this->CleanUp->fileCleanUp($data)) {
// 成功
$this->out('Success!!');
return;
}
// エラー. 第1引数plugin_keyを選択肢からの必須にしたため, 基本到達しない. テストはここ通る
$this->out('[ValidationErrors]');
$this->out(var_export($this->CleanUp->validationErrors));
}
/**
* ロックファイルの強制削除
* ### コマンド例
* ```
* Console/cake clean_up.clean_up unlock
* ```
*
* @return void
*/
public function unlock() {
// ロックファイルの削除
CleanUpLockFile::deleteLockFileAndSetupLog();
}
/**
* 引数設定
*
* @return ConsoleOptionParser
* @link http://book.cakephp.org/2.0/ja/console-and-shells.html#Shell::getOptionParser
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
// 説明
$parser->description([
__d('clean_up', 'File cleanup command description', [
CleanUp::HOW_TO_BACKUP_URL,
LOGS . 'cleanup' . DS . CleanUpLog::LOG_FILE_NAME,
]),
]);
// プラグインキーの一覧
$pluginKeys = $this->__getPluginKeys();
$pluginKeys[] = self::PLUGIN_KEY_ALL;
// 引数
$arguments[] = [
'help' => __d('clean_up', 'File cleanup command arguments help'),
'required' => false,
'choices' => $pluginKeys,
];
// プラグイン数分ループして引数を追加する
// 最大プラグイン数と+1(上記helpで説明の分)の引数を設定
// @codingStandardsIgnoreStart
foreach ($pluginKeys as $pluginKey) {
// @codingStandardsIgnoreEnd
$arguments[] = [
'required' => false,
'choices' => $pluginKeys,
];
}
$parser->addArguments($arguments);
return $parser;
}
/**
* プラグインキーの一覧 ゲット
*
* @return array
*/
private function __getPluginKeys() {
// プラグインキーの一覧
$cleanUps = $this->CleanUp->findCleanUpsAndPlugin();
$pluginKeys = [];
foreach ($cleanUps as $cleanUp) {
$pluginKeys[] = $cleanUp['CleanUp']['plugin_key'];
}
return $pluginKeys;
}
}