1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Interfaces\Controllers\IApi; |
6
|
|
|
use App\Reuse\Controllers\AbstractApi; |
7
|
|
|
use App\Container; |
8
|
|
|
use App\Http\Response; |
9
|
|
|
|
10
|
|
|
final class Config extends AbstractApi implements IApi |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
const KEY_LENGTH = 64; |
14
|
|
|
const _TITLE = 'title'; |
15
|
|
|
const _ACTION = 'action'; |
16
|
|
|
const _DATAS = 'datas'; |
17
|
|
|
const _HOWTO = 'howto'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* instanciate |
21
|
|
|
* |
22
|
|
|
* @param Container $container |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Container $container) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($container); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* help action |
31
|
|
|
* |
32
|
|
|
* @Role anonymous |
33
|
|
|
* @return Config |
34
|
|
|
*/ |
35
|
|
|
final public function help(): Config |
36
|
|
|
{ |
37
|
|
|
$baseUri = $this->baseRootUri(); |
38
|
|
|
$helpAction = [ |
39
|
|
|
[ |
40
|
|
|
self::_TITLE => 'App key generate', |
41
|
|
|
self::_ACTION => $baseUri . 'keygen', |
42
|
|
|
self::_HOWTO => 'Copy paste result in ' |
43
|
|
|
. 'config/$env jwt/secret' |
44
|
|
|
] |
45
|
|
|
]; |
46
|
|
|
$this->response |
47
|
|
|
->setCode(Response::HTTP_OK) |
48
|
|
|
->setContent($helpAction); |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* false action |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
final public function false() |
58
|
|
|
{ |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* keygen action |
64
|
|
|
* |
65
|
|
|
* @Role anonymous |
66
|
|
|
* @return Config |
67
|
|
|
*/ |
68
|
|
|
final public function keygen(): Config |
69
|
|
|
{ |
70
|
|
|
$this->response |
71
|
|
|
->setCode(Response::HTTP_OK) |
72
|
|
|
->setContent( |
73
|
|
|
$this->getActionItem( |
74
|
|
|
'App key generate', |
75
|
|
|
__FUNCTION__, |
76
|
|
|
base64_encode( |
77
|
|
|
openssl_random_pseudo_bytes(self::KEY_LENGTH) |
78
|
|
|
) |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* account action |
86
|
|
|
* |
87
|
|
|
* @Role anonymous |
88
|
|
|
* @return Config |
89
|
|
|
*/ |
90
|
|
|
final public function account(): Config |
91
|
|
|
{ |
92
|
|
|
$line = 'Undefined function readline'; |
93
|
|
|
if ($this->hasReadLine()) { |
94
|
|
|
//$line = readline("Command: "); |
95
|
|
|
} |
96
|
|
|
$this->response |
97
|
|
|
->setCode(Response::HTTP_OK) |
98
|
|
|
->setContent(['error' => false, 'command' => $line]); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* return current base root uri |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function baseRootUri(): string |
108
|
|
|
{ |
109
|
|
|
return dirname($this->request->getUri()) . '/'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* return array |
114
|
|
|
* |
115
|
|
|
* @param string $title |
116
|
|
|
* @param string $action |
117
|
|
|
* @param string $datas |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function getActionItem(string $title, string $action, string $datas): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
[ |
124
|
|
|
self::_TITLE => $title, |
125
|
|
|
self::_ACTION => $this->baseRootUri() . $action, |
126
|
|
|
self::_DATAS => $datas |
127
|
|
|
] |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* return true if php was configured with --readline option |
133
|
|
|
* |
134
|
|
|
* @return boolean |
135
|
|
|
*/ |
136
|
|
|
protected function hasReadLine(): bool |
137
|
|
|
{ |
138
|
|
|
return function_exists("readline"); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|