1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* © 2018 - Phoponent |
5
|
|
|
* Author: Nicolas Choquet |
6
|
|
|
* Email: [email protected] |
7
|
|
|
* LICENSE GPL ( GNU General Public License ) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace phoponent\framework\traits; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
trait command { |
15
|
|
|
private $argv = []; |
16
|
|
|
|
17
|
|
|
protected static $LOG_ECHO = 'echo'; |
18
|
|
|
protected static $LOG_FILE = ''; |
19
|
|
|
|
20
|
|
|
public function __construct($argv = []) { |
21
|
|
|
$this->argv = $argv; |
22
|
|
|
$this->after_connstruct(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function after_connstruct() {} |
26
|
|
|
|
27
|
|
|
protected function log($text = '', $type_log = 'echo') { |
28
|
|
|
if ($type_log === self::$LOG_FILE) { |
29
|
|
|
$path = str_replace('/'.basename(self::$LOG_FILE), '', self::$LOG_FILE); |
30
|
|
|
if(!is_dir($path)) { |
31
|
|
|
mkdir($path, 0777, true); |
32
|
|
|
} |
33
|
|
|
$content = ''; |
34
|
|
|
if(is_file(self::$LOG_FILE)) { |
35
|
|
|
$content = file_get_contents(self::$LOG_FILE); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
file_put_contents(self::$LOG_FILE, $content."\n".date('#~ '.exec('echo $USER').': '.$text)); |
39
|
|
|
} |
40
|
|
|
else { |
41
|
|
|
echo date('Y-m-d H:i:s').' '.exec('echo $USER').' #~ '.$text."\n"; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function before_run() {} |
46
|
|
|
|
47
|
|
|
protected function after_run() {} |
48
|
|
|
|
49
|
|
|
protected function run() {} |
50
|
|
|
|
51
|
|
|
protected function argument($name) { |
52
|
|
|
foreach ($this->argv as $argv) { |
53
|
|
|
if(explode('=', $argv)[0] === $name) { |
54
|
|
|
return explode('=', $argv)[1]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $method |
62
|
|
|
* @return bool |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function execute($method = 'run') { |
66
|
|
|
$this->before_run(); |
67
|
|
|
if(in_array($method, get_class_methods(get_class($this)))) { |
68
|
|
|
$this->$method(); |
69
|
|
|
$this->after_run(); |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
throw new Exception("method ".explode('\\', __CLASS__)[count(explode('\\', __CLASS__))-1]."::{$method}() not found"); |
73
|
|
|
} |
74
|
|
|
} |