|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
|
13
|
|
|
use Yiisoft\VarDumper\VarDumper; |
|
14
|
|
|
use Yiisoft\Yii\Debug\Api\ApplicationState; |
|
15
|
|
|
use Yiisoft\Yii\Debug\Api\Repository\CollectorRepositoryInterface; |
|
16
|
|
|
|
|
17
|
|
|
class InspectController |
|
18
|
|
|
{ |
|
19
|
|
|
private DataResponseFactoryInterface $responseFactory; |
|
20
|
|
|
private CollectorRepositoryInterface $collectorRepository; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
DataResponseFactoryInterface $responseFactory, |
|
24
|
|
|
CollectorRepositoryInterface $collectorRepository |
|
25
|
|
|
) { |
|
26
|
|
|
$this->responseFactory = $responseFactory; |
|
27
|
|
|
$this->collectorRepository = $collectorRepository; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function index(): ResponseInterface |
|
31
|
|
|
{ |
|
32
|
|
|
// TODO: how to get params for console or other param groups? |
|
33
|
|
|
$params = ApplicationState::$params; |
|
34
|
|
|
|
|
35
|
|
|
// TODO: also would be nice to inspect application config to access to di config at least |
|
36
|
|
|
|
|
37
|
|
|
// $config = ApplicationState::$config; |
|
38
|
|
|
// return $this->responseFactory->createResponse([$config->get('web'), $params]); |
|
39
|
|
|
|
|
40
|
|
|
return $this->responseFactory->createResponse($params); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function classes(ContainerInterface $container): ResponseInterface |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
// TODO: how to get params for console or other param groups? |
|
46
|
|
|
$classes = []; |
|
47
|
|
|
|
|
48
|
|
|
$inspected = array_merge(get_declared_classes(), get_declared_interfaces()); |
|
49
|
|
|
// TODO: think how to ignore heavy objects |
|
50
|
|
|
$patterns = [ |
|
51
|
|
|
'ComposerAutoloaderInit', |
|
52
|
|
|
'Composer\\', |
|
53
|
|
|
'Yiisoft\\Yii\\Debug\\', |
|
54
|
|
|
'Yiisoft\\ErrorHandler\\ErrorHandler', |
|
55
|
|
|
]; |
|
56
|
|
|
foreach ($patterns as $pattern) { |
|
57
|
|
|
$inspected = array_filter($inspected, fn (string $class) => !str_starts_with($class, $pattern)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($inspected as $className) { |
|
61
|
|
|
$class = new ReflectionClass($className); |
|
62
|
|
|
|
|
63
|
|
|
if ($class->isInternal()) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$classes[] = $className; |
|
68
|
|
|
} |
|
69
|
|
|
sort($classes); |
|
70
|
|
|
|
|
71
|
|
|
return $this->responseFactory->createResponse($classes); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function object(ContainerInterface $container, ServerRequestInterface $request): ResponseInterface |
|
75
|
|
|
{ |
|
76
|
|
|
$request = $request->getQueryParams(); |
|
77
|
|
|
$className = $request['classname']; |
|
78
|
|
|
|
|
79
|
|
|
$class = new ReflectionClass($className); |
|
80
|
|
|
|
|
81
|
|
|
if ($class->isInternal()) { |
|
82
|
|
|
throw new InvalidArgumentException('error'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$result = VarDumper::create($container->get($className))->asString(); |
|
86
|
|
|
|
|
87
|
|
|
return $this->responseFactory->createResponse($result); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.