1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Debug\Repository; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Yii\Debug\Api\Debug\Exception\NotFoundException; |
8
|
|
|
use Yiisoft\Yii\Debug\Storage\StorageInterface; |
9
|
|
|
|
10
|
|
|
final class CollectorRepository implements CollectorRepositoryInterface |
11
|
|
|
{ |
12
|
|
|
public function __construct(private StorageInterface $storage) |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getSummary(?string $id = null): array |
17
|
|
|
{ |
18
|
|
|
$data = $this->loadData(StorageInterface::TYPE_SUMMARY, $id); |
19
|
|
|
if ($id !== null) { |
20
|
|
|
return $data; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return array_values(array_reverse($data)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getDetail(string $id): array |
27
|
|
|
{ |
28
|
|
|
return $this->loadData(StorageInterface::TYPE_DATA, $id); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getDumpObject(string $id): array |
32
|
|
|
{ |
33
|
|
|
return $this->loadData(StorageInterface::TYPE_OBJECTS, $id); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getObject(string $id, string $objectId): array|null |
37
|
|
|
{ |
38
|
|
|
$dump = $this->loadData(StorageInterface::TYPE_OBJECTS, $id); |
39
|
|
|
|
40
|
|
|
foreach ($dump as $name => $value) { |
41
|
|
|
if (($pos = strrpos($name, "#$objectId")) !== false) { |
42
|
|
|
return [substr($name, 0, $pos), $value]; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws NotFoundException |
51
|
|
|
*/ |
52
|
|
|
private function loadData(string $fileType, ?string $id = null): array |
53
|
|
|
{ |
54
|
|
|
$data = $this->storage->read($fileType, $id); |
55
|
|
|
if (!empty($id)) { |
56
|
|
|
if (!isset($data[$id])) { |
57
|
|
|
throw new NotFoundException(sprintf('Unable to find debug data ID with "%s"', $id)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $data[$id]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $data; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|