1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controllers; |
6
|
|
|
|
7
|
|
|
use App\Component\Console\Input; |
8
|
|
|
use App\Component\Console\Terminal; |
9
|
|
|
use App\Interfaces\Controllers\IApi; |
10
|
|
|
use App\Reuse\Controllers\AbstractApi; |
11
|
|
|
use Nymfonya\Component\Container; |
12
|
|
|
use Nymfonya\Component\Http\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @OA\Info(title="Nymfonya",version="v1") |
16
|
|
|
* @OA\PathItem(path=".") |
17
|
|
|
*/ |
18
|
|
|
final class Config extends AbstractApi implements IApi |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const KEY_LENGTH = 64; |
22
|
|
|
const _TITLE = 'title'; |
23
|
|
|
const _ACTION = 'action'; |
24
|
|
|
const _DATAS = 'datas'; |
25
|
|
|
const _HOWTO = 'howto'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* instanciate |
29
|
|
|
* |
30
|
|
|
* @param Container $container |
31
|
|
|
*/ |
32
|
8 |
|
public function __construct(Container $container) |
33
|
|
|
{ |
34
|
8 |
|
parent::__construct($container); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* help action |
39
|
|
|
* |
40
|
|
|
* @Role anonymous |
41
|
|
|
* @return Config |
42
|
|
|
*/ |
43
|
1 |
|
final public function help(): Config |
44
|
|
|
{ |
45
|
1 |
|
$baseUri = $this->baseRootUri(); |
46
|
|
|
$helpAction = [ |
47
|
|
|
[ |
48
|
1 |
|
self::_TITLE => 'App key generate', |
49
|
1 |
|
self::_ACTION => $baseUri . 'keygen', |
50
|
1 |
|
self::_HOWTO => 'Copy paste result in ' |
51
|
|
|
. 'config/$env jwt/secret' |
52
|
|
|
] |
53
|
|
|
]; |
54
|
1 |
|
$this->response |
55
|
1 |
|
->setCode(Response::HTTP_OK) |
56
|
1 |
|
->setContent($helpAction); |
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* false action |
62
|
|
|
* |
63
|
|
|
* @return boolean |
64
|
|
|
*/ |
65
|
1 |
|
final public function false() |
66
|
|
|
{ |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* keygen action |
72
|
|
|
* |
73
|
|
|
* @Role anonymous |
74
|
|
|
* @return Config |
75
|
|
|
*/ |
76
|
1 |
|
final public function keygen(): Config |
77
|
|
|
{ |
78
|
1 |
|
$this->response |
79
|
1 |
|
->setCode(Response::HTTP_OK) |
80
|
1 |
|
->setContent( |
81
|
1 |
|
$this->getActionItem( |
82
|
1 |
|
'App key generate', |
83
|
1 |
|
__FUNCTION__, |
84
|
1 |
|
base64_encode( |
85
|
1 |
|
openssl_random_pseudo_bytes(self::KEY_LENGTH) |
86
|
|
|
) |
87
|
|
|
) |
88
|
|
|
); |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* account action |
94
|
|
|
* |
95
|
|
|
* @Role anonymous |
96
|
|
|
* @return Config |
97
|
|
|
*/ |
98
|
|
|
final public function account(): Config |
99
|
|
|
{ |
100
|
|
|
echo "- [enter] to validate\n"; |
101
|
|
|
echo '- Account name : '; |
102
|
|
|
$inputName = new Input(Input::STREAM_STDIN); |
103
|
|
|
$inputName->setMaxLength(2); |
104
|
|
|
$name = $inputName->value(); |
105
|
|
|
$this->response |
106
|
|
|
->setCode(Response::HTTP_OK) |
107
|
|
|
->setContent(['error' => false, 'command' => $name]); |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* generate swagger doc from code |
113
|
|
|
* |
114
|
|
|
* @return Config |
115
|
|
|
*/ |
116
|
1 |
|
final public function swaggerdoc(): Config |
117
|
|
|
{ |
118
|
1 |
|
$path = dirname(__FILE__); |
119
|
1 |
|
$controllerPath = '/Api/V1/Restful.php'; |
120
|
1 |
|
$openapi = \OpenApi\scan($path . $controllerPath); |
121
|
1 |
|
$this->response |
122
|
1 |
|
->setCode(Response::HTTP_OK) |
123
|
1 |
|
->setContent($openapi->toYaml()); |
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* return current base root uri |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
1 |
|
protected function baseRootUri(): string |
133
|
|
|
{ |
134
|
1 |
|
return dirname($this->request->getUri()) . '/'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* return array |
139
|
|
|
* |
140
|
|
|
* @param string $title |
141
|
|
|
* @param string $action |
142
|
|
|
* @param string $datas |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
1 |
|
protected function getActionItem(string $title, string $action, string $datas): array |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
[ |
149
|
1 |
|
self::_TITLE => $title, |
150
|
1 |
|
self::_ACTION => $this->baseRootUri() . $action, |
151
|
1 |
|
self::_DATAS => $datas |
152
|
|
|
] |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* return true if php was configured with --readline option |
158
|
|
|
* |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
1 |
|
protected function hasReadLine(): bool |
162
|
|
|
{ |
163
|
1 |
|
return function_exists("readline"); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|