1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Controller; |
6
|
|
|
|
7
|
|
|
use FilesystemIterator; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Psr7\Message; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use RecursiveDirectoryIterator; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use SplFileInfo; |
18
|
|
|
use Throwable; |
19
|
|
|
use Yiisoft\Aliases\Aliases; |
20
|
|
|
use Yiisoft\Config\ConfigInterface; |
21
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
22
|
|
|
use Yiisoft\Router\CurrentRoute; |
23
|
|
|
use Yiisoft\Router\RouteCollectionInterface; |
24
|
|
|
use Yiisoft\Translator\CategorySource; |
25
|
|
|
use Yiisoft\VarDumper\VarDumper; |
26
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\ApplicationState; |
27
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\CommandInterface; |
28
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\Database\SchemaProviderInterface; |
29
|
|
|
use Yiisoft\Yii\Debug\Api\Repository\CollectorRepositoryInterface; |
30
|
|
|
use Yiisoft\Yii\Debug\Collector\RequestCollector; |
31
|
|
|
|
32
|
|
|
class InspectController |
33
|
|
|
{ |
34
|
|
|
public function __construct( |
35
|
|
|
private DataResponseFactoryInterface $responseFactory, |
36
|
|
|
) { |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function config(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface |
40
|
|
|
{ |
41
|
|
|
$config = $container->get(ConfigInterface::class); |
42
|
|
|
|
43
|
|
|
$request = $request->getQueryParams(); |
44
|
|
|
$group = $request['group'] ?? 'web'; |
45
|
|
|
|
46
|
|
|
$data = $config->get($group); |
47
|
|
|
ksort($data); |
48
|
|
|
|
49
|
|
|
$response = VarDumper::create($data)->asJson(false, 255); |
50
|
|
|
return $this->responseFactory->createResponse(json_decode($response, null, 512, JSON_THROW_ON_ERROR)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getTranslations(ContainerInterface $container): ResponseInterface |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* @var $categorySources CategorySource[] |
57
|
|
|
*/ |
58
|
|
|
$categorySources = $container->get('[email protected]'); |
59
|
|
|
|
60
|
|
|
$params = ApplicationState::$params; |
61
|
|
|
|
62
|
|
|
$locales = array_keys($params['locale']['locales']); |
63
|
|
|
if ($locales === []) { |
64
|
|
|
throw new RuntimeException( |
65
|
|
|
'Unable to determine list of available locales. ' . |
66
|
|
|
'Make sure that "$params[\'locale\'][\'locales\']" contains all available locales.' |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
$messages = []; |
70
|
|
|
foreach ($categorySources as $categorySource) { |
71
|
|
|
$messages[$categorySource->getName()] = []; |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
foreach ($locales as $locale) { |
75
|
|
|
$messages[$categorySource->getName()][$locale] = $categorySource->getMessages($locale); |
76
|
|
|
} |
77
|
|
|
} catch (Throwable) { |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$response = VarDumper::create($messages)->asPrimitives(255); |
82
|
|
|
return $this->responseFactory->createResponse($response); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function putTranslation(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface |
86
|
|
|
{ |
87
|
|
|
/** |
88
|
|
|
* @var $categorySources CategorySource[] |
89
|
|
|
*/ |
90
|
|
|
$categorySources = $container->get('[email protected]'); |
91
|
|
|
|
92
|
|
|
$body = $request->getParsedBody(); |
93
|
|
|
$categoryName = $body['category'] ?? ''; |
94
|
|
|
$locale = $body['locale'] ?? ''; |
95
|
|
|
$translationId = $body['translation'] ?? ''; |
96
|
|
|
$newMessage = $body['message'] ?? ''; |
97
|
|
|
|
98
|
|
|
$categorySource = null; |
99
|
|
|
foreach ($categorySources as $possibleCategorySource) { |
100
|
|
|
if ($possibleCategorySource->getName() === $categoryName) { |
101
|
|
|
$categorySource = $possibleCategorySource; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
if ($categorySource === null) { |
105
|
|
|
throw new InvalidArgumentException( |
106
|
|
|
sprintf( |
107
|
|
|
'Invalid category name "%s". Only the following categories are available: "%s"', |
108
|
|
|
$categoryName, |
109
|
|
|
implode( |
110
|
|
|
'", "', |
111
|
|
|
array_map(fn (CategorySource $categorySource) => $categorySource->getName(), $categorySources) |
112
|
|
|
) |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
$messages = $categorySource->getMessages($locale); |
117
|
|
|
$messages = array_replace_recursive($messages, [ |
118
|
|
|
$translationId => [ |
119
|
|
|
'message' => $newMessage, |
120
|
|
|
], |
121
|
|
|
]); |
122
|
|
|
$categorySource->write($locale, $messages); |
123
|
|
|
|
124
|
|
|
$result = [$locale => $messages]; |
125
|
|
|
$response = VarDumper::create($result)->asPrimitives(255); |
126
|
|
|
return $this->responseFactory->createResponse($response); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function params(): ResponseInterface |
130
|
|
|
{ |
131
|
|
|
$params = ApplicationState::$params; |
132
|
|
|
ksort($params); |
133
|
|
|
|
134
|
|
|
return $this->responseFactory->createResponse($params); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function files(Aliases $aliases, ServerRequestInterface $request): ResponseInterface |
138
|
|
|
{ |
139
|
|
|
$request = $request->getQueryParams(); |
140
|
|
|
$path = $request['path'] ?? ''; |
141
|
|
|
|
142
|
|
|
$rootPath = $aliases->get('@root'); |
143
|
|
|
|
144
|
|
|
$destination = $this->removeBasePath($rootPath, $path); |
145
|
|
|
|
146
|
|
|
if (!str_starts_with('/', $destination)) { |
147
|
|
|
$destination = '/' . $destination; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$destination = realpath($rootPath . $destination); |
151
|
|
|
|
152
|
|
|
if (!file_exists($destination)) { |
153
|
|
|
throw new InvalidArgumentException(sprintf('Destination "%s" does not exist', $destination)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!is_dir($destination)) { |
157
|
|
|
$file = new SplFileInfo($destination); |
158
|
|
|
return $this->responseFactory->createResponse( |
159
|
|
|
array_merge( |
160
|
|
|
[ |
161
|
|
|
'directory' => $this->removeBasePath($rootPath, dirname($destination)), |
162
|
|
|
'content' => file_get_contents($destination), |
163
|
|
|
'path' => $this->removeBasePath($rootPath, $destination), |
164
|
|
|
'absolutePath' => $destination, |
165
|
|
|
], |
166
|
|
|
$this->serializeFileInfo($file) |
167
|
|
|
) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @var $directoryIterator SplFileInfo[] |
173
|
|
|
*/ |
174
|
|
|
$directoryIterator = new RecursiveDirectoryIterator( |
175
|
|
|
$destination, |
176
|
|
|
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$files = []; |
180
|
|
|
foreach ($directoryIterator as $file) { |
181
|
|
|
if ($file->getBasename() === '.') { |
182
|
|
|
continue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$path = $file->getPathName(); |
186
|
|
|
if ($file->isDir()) { |
187
|
|
|
if ($file->getBasename() === '..') { |
188
|
|
|
$path = realpath($path); |
189
|
|
|
} |
190
|
|
|
$path .= '/'; |
191
|
|
|
} |
192
|
|
|
/** |
193
|
|
|
* Check if path is inside the application directory |
194
|
|
|
*/ |
195
|
|
|
if (!str_starts_with($path, $rootPath)) { |
196
|
|
|
continue; |
197
|
|
|
} |
198
|
|
|
$path = $this->removeBasePath($rootPath, $path); |
199
|
|
|
$files[] = array_merge( |
200
|
|
|
[ |
201
|
|
|
'path' => $path, |
202
|
|
|
], |
203
|
|
|
$this->serializeFileInfo($file) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->responseFactory->createResponse($files); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function classes(ContainerInterface $container): ResponseInterface |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
// TODO: how to get params for console or other param groups? |
213
|
|
|
$classes = []; |
214
|
|
|
|
215
|
|
|
$inspected = [...get_declared_classes(), ...get_declared_interfaces()]; |
216
|
|
|
// TODO: think how to ignore heavy objects |
217
|
|
|
$patterns = [ |
218
|
|
|
fn (string $class) => !str_starts_with($class, 'ComposerAutoloaderInit'), |
219
|
|
|
fn (string $class) => !str_starts_with($class, 'Composer\\'), |
220
|
|
|
fn (string $class) => !str_starts_with($class, 'Yiisoft\\Yii\\Debug\\'), |
221
|
|
|
fn (string $class) => !str_starts_with($class, 'Yiisoft\\ErrorHandler\\ErrorHandler'), |
222
|
|
|
fn (string $class) => !str_contains($class, '@anonymous'), |
223
|
|
|
fn (string $class) => !is_subclass_of($class, Throwable::class), |
224
|
|
|
]; |
225
|
|
|
foreach ($patterns as $patternFunction) { |
226
|
|
|
$inspected = array_filter($inspected, $patternFunction); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
foreach ($inspected as $className) { |
230
|
|
|
$class = new ReflectionClass($className); |
231
|
|
|
|
232
|
|
|
if ($class->isInternal()) { |
233
|
|
|
continue; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$classes[] = $className; |
237
|
|
|
} |
238
|
|
|
sort($classes); |
239
|
|
|
|
240
|
|
|
return $this->responseFactory->createResponse($classes); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function object(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface |
244
|
|
|
{ |
245
|
|
|
$queryParams = $request->getQueryParams(); |
246
|
|
|
$className = $queryParams['classname']; |
247
|
|
|
|
248
|
|
|
$reflection = new ReflectionClass($className); |
249
|
|
|
|
250
|
|
|
if ($reflection->isInternal()) { |
251
|
|
|
throw new InvalidArgumentException('Inspector cannot initialize internal classes.'); |
252
|
|
|
} |
253
|
|
|
if ($reflection->implementsInterface(Throwable::class)) { |
254
|
|
|
throw new InvalidArgumentException('Inspector cannot initialize exceptions.'); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$variable = $container->get($className); |
258
|
|
|
$result = VarDumper::create($variable)->asJson(false, 3); |
259
|
|
|
|
260
|
|
|
return $this->responseFactory->createResponse([ |
261
|
|
|
'object' => json_decode($result, null, 512, JSON_THROW_ON_ERROR), |
262
|
|
|
'path' => $reflection->getFileName(), |
263
|
|
|
]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getCommands(ConfigInterface $config): ResponseInterface |
267
|
|
|
{ |
268
|
|
|
$params = $config->get('params'); |
269
|
|
|
$commandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? []; |
270
|
|
|
|
271
|
|
|
$result = []; |
272
|
|
|
foreach ($commandMap as $groupName => $commands) { |
273
|
|
|
foreach ($commands as $name => $command) { |
274
|
|
|
if (!is_subclass_of($command, CommandInterface::class)) { |
275
|
|
|
continue; |
276
|
|
|
} |
277
|
|
|
$result[] = [ |
278
|
|
|
'name' => $name, |
279
|
|
|
'title' => $command::getTitle(), |
280
|
|
|
'group' => $groupName, |
281
|
|
|
'description' => $command::getDescription(), |
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->responseFactory->createResponse($result); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function routes(RouteCollectionInterface $routeCollection): ResponseInterface |
290
|
|
|
{ |
291
|
|
|
$routes = []; |
292
|
|
|
foreach ($routeCollection->getRoutes() as $route) { |
293
|
|
|
$data = $route->__debugInfo(); |
294
|
|
|
$routes[] = [ |
295
|
|
|
'name' => $data['name'], |
296
|
|
|
'hosts' => $data['hosts'], |
297
|
|
|
'pattern' => $data['pattern'], |
298
|
|
|
'methods' => $data['methods'], |
299
|
|
|
'defaults' => $data['defaults'], |
300
|
|
|
'override' => $data['override'], |
301
|
|
|
'middlewares' => $data['middlewareDefinitions'], |
302
|
|
|
]; |
303
|
|
|
} |
304
|
|
|
$response = VarDumper::create($routes)->asJson(false, 5); |
305
|
|
|
return $this->responseFactory->createResponse(json_decode($response, null, 512, JSON_THROW_ON_ERROR)); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function runCommand( |
309
|
|
|
ServerRequestInterface $request, |
310
|
|
|
ContainerInterface $container, |
311
|
|
|
ConfigInterface $config |
312
|
|
|
): ResponseInterface { |
313
|
|
|
$params = $config->get('params'); |
314
|
|
|
$commandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? []; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @var array<string, class-string<CommandInterface>> $commandList |
318
|
|
|
*/ |
319
|
|
|
$commandList = []; |
320
|
|
|
foreach ($commandMap as $commands) { |
321
|
|
|
foreach ($commands as $name => $command) { |
322
|
|
|
if (!is_subclass_of($command, CommandInterface::class)) { |
323
|
|
|
continue; |
324
|
|
|
} |
325
|
|
|
$commandList[$name] = $command; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$request = $request->getQueryParams(); |
330
|
|
|
$commandName = $request['command'] ?? null; |
331
|
|
|
|
332
|
|
|
if ($commandName === null) { |
333
|
|
|
throw new InvalidArgumentException( |
334
|
|
|
sprintf( |
335
|
|
|
'Command must not be null. Available commands: "%s".', |
336
|
|
|
implode('", "', $commandList) |
337
|
|
|
) |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if (!array_key_exists($commandName, $commandList)) { |
342
|
|
|
throw new InvalidArgumentException( |
343
|
|
|
sprintf( |
344
|
|
|
'Unknown command "%s". Available commands: "%s".', |
345
|
|
|
$commandName, |
346
|
|
|
implode('", "', $commandList) |
347
|
|
|
) |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$commandClass = $commandList[$commandName]; |
352
|
|
|
/** |
353
|
|
|
* @var $command CommandInterface |
354
|
|
|
*/ |
355
|
|
|
$command = $container->get($commandClass); |
356
|
|
|
|
357
|
|
|
$result = $command->run(); |
358
|
|
|
|
359
|
|
|
return $this->responseFactory->createResponse([ |
360
|
|
|
'status' => $result->getStatus(), |
361
|
|
|
'result' => $result->getResult(), |
362
|
|
|
'error' => $result->getErrors(), |
363
|
|
|
]); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function getTables(SchemaProviderInterface $schemaProvider): ResponseInterface |
367
|
|
|
{ |
368
|
|
|
return $this->responseFactory->createResponse($schemaProvider->getTables()); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function getTable(SchemaProviderInterface $schemaProvider, CurrentRoute $currentRoute): ResponseInterface |
372
|
|
|
{ |
373
|
|
|
$tableName = $currentRoute->getArgument('name'); |
374
|
|
|
|
375
|
|
|
return $this->responseFactory->createResponse($schemaProvider->getTable($tableName)); |
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function request( |
379
|
|
|
ServerRequestInterface $request, |
380
|
|
|
CollectorRepositoryInterface $collectorRepository |
381
|
|
|
): ResponseInterface { |
382
|
|
|
$request = $request->getQueryParams(); |
383
|
|
|
$debugEntryId = $request['debugEntryId'] ?? null; |
384
|
|
|
|
385
|
|
|
$data = $collectorRepository->getDetail($debugEntryId); |
|
|
|
|
386
|
|
|
$rawRequest = $data[RequestCollector::class]['requestRaw']; |
387
|
|
|
|
388
|
|
|
$request = Message::parseRequest($rawRequest); |
389
|
|
|
|
390
|
|
|
$client = new Client(); |
391
|
|
|
$response = $client->send($request); |
392
|
|
|
|
393
|
|
|
$result = VarDumper::create($response)->asPrimitives(); |
394
|
|
|
|
395
|
|
|
return $this->responseFactory->createResponse($result); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
private function removeBasePath(string $rootPath, string $path): string|array|null |
399
|
|
|
{ |
400
|
|
|
return preg_replace( |
401
|
|
|
'/^' . preg_quote($rootPath, '/') . '/', |
402
|
|
|
'', |
403
|
|
|
$path, |
404
|
|
|
1 |
405
|
|
|
); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
private function getUserOwner(int $uid): array |
409
|
|
|
{ |
410
|
|
|
if ($uid === 0 || !function_exists('posix_getpwuid') || false === ($info = posix_getpwuid($uid))) { |
411
|
|
|
return [ |
412
|
|
|
'id' => $uid, |
413
|
|
|
]; |
414
|
|
|
} |
415
|
|
|
return [ |
416
|
|
|
'uid' => $info['uid'], |
417
|
|
|
'gid' => $info['gid'], |
418
|
|
|
'name' => $info['name'], |
419
|
|
|
]; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
private function getGroupOwner(int $gid): array |
423
|
|
|
{ |
424
|
|
|
if ($gid === 0 || !function_exists('posix_getgrgid') || false === ($info = posix_getgrgid($gid))) { |
425
|
|
|
return [ |
426
|
|
|
'id' => $gid, |
427
|
|
|
]; |
428
|
|
|
} |
429
|
|
|
return [ |
430
|
|
|
'gid' => $info['gid'], |
431
|
|
|
'name' => $info['name'], |
432
|
|
|
]; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
private function serializeFileInfo(SplFileInfo $file): array |
436
|
|
|
{ |
437
|
|
|
return [ |
438
|
|
|
'baseName' => $file->getBasename(), |
439
|
|
|
'extension' => $file->getExtension(), |
440
|
|
|
'user' => $this->getUserOwner((int) $file->getOwner()), |
441
|
|
|
'group' => $this->getGroupOwner((int) $file->getGroup()), |
442
|
|
|
'size' => $file->getSize(), |
443
|
|
|
'type' => $file->getType(), |
444
|
|
|
'permissions' => substr(sprintf('%o', $file->getPerms()), -4), |
445
|
|
|
]; |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|