1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Controller; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\SimpleCache\CacheInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
13
|
|
|
use Yiisoft\VarDumper\VarDumper; |
14
|
|
|
|
15
|
|
|
class CacheController |
16
|
|
|
{ |
17
|
|
|
public function __construct( |
18
|
|
|
private DataResponseFactoryInterface $responseFactory, |
19
|
|
|
) { |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function view( |
23
|
|
|
ServerRequestInterface $request, |
24
|
|
|
ContainerInterface $container, |
25
|
|
|
): ResponseInterface { |
26
|
|
|
$params = $request->getQueryParams(); |
27
|
|
|
$key = $params['key'] ?? ''; |
28
|
|
|
|
29
|
|
|
if ($key === '') { |
30
|
|
|
throw new RuntimeException('Cache key must not be empty.'); |
31
|
|
|
} |
32
|
|
|
if (!$container->has(CacheInterface::class)) { |
33
|
|
|
// TODO: fix message |
34
|
|
|
throw new RuntimeException( |
35
|
|
|
'Psr\\SimpleCache\\CacheInterface does not exist in the application configuration.' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
$cache = $container->get(CacheInterface::class); |
39
|
|
|
|
40
|
|
|
if (!$cache->has($key)) { |
41
|
|
|
return $this->responseFactory->createResponse([ |
42
|
|
|
'error' => 'Key does not exist in cache', |
43
|
|
|
], 404); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$result = $cache->get($key); |
47
|
|
|
|
48
|
|
|
$response = VarDumper::create($result)->asJson(false, 255); |
49
|
|
|
|
50
|
|
|
return $this->responseFactory->createResponse(json_decode($response, null, 512, JSON_THROW_ON_ERROR)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function delete( |
54
|
|
|
ServerRequestInterface $request, |
55
|
|
|
ContainerInterface $container, |
56
|
|
|
): ResponseInterface { |
57
|
|
|
$params = $request->getQueryParams(); |
58
|
|
|
$key = $params['key'] ?? ''; |
59
|
|
|
|
60
|
|
|
if ($key === '') { |
61
|
|
|
throw new RuntimeException('Cache key must not be empty.'); |
62
|
|
|
} |
63
|
|
|
if (!$container->has(CacheInterface::class)) { |
64
|
|
|
// TODO: fix message |
65
|
|
|
throw new RuntimeException( |
66
|
|
|
'Psr\\SimpleCache\\CacheInterface does not exist in the application configuration.' |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
$cache = $container->get(CacheInterface::class); |
70
|
|
|
|
71
|
|
|
if (!$cache->has($key)) { |
72
|
|
|
throw new RuntimeException('Key does not exist in cache'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$result = $cache->delete($key); |
76
|
|
|
|
77
|
|
|
return $this->responseFactory->createResponse([ |
78
|
|
|
'result' => $result, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function clear( |
83
|
|
|
ContainerInterface $container, |
84
|
|
|
): ResponseInterface { |
85
|
|
|
if (!$container->has(CacheInterface::class)) { |
86
|
|
|
// TODO: fix message |
87
|
|
|
throw new RuntimeException( |
88
|
|
|
'Psr\\SimpleCache\\CacheInterface does not exist in the application configuration.' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
$cache = $container->get(CacheInterface::class); |
92
|
|
|
|
93
|
|
|
$result = $cache->clear(); |
94
|
|
|
|
95
|
|
|
return $this->responseFactory->createResponse([ |
96
|
|
|
'result' => $result, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|