1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace hiapi\Core\Endpoint; |
5
|
|
|
|
6
|
|
|
use hiapi\exceptions\ConfigurationException; |
7
|
|
|
use Psalm\Type\Atomic\TLiteralClassString; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
|
10
|
|
|
class EndpointRepository |
11
|
|
|
{ |
12
|
|
|
private ContainerInterface $container; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private BuilderFactory $builderFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array<string, string|TLiteralClassString> |
18
|
|
|
*/ |
19
|
|
|
private array $endpoints = []; |
20
|
|
|
|
21
|
|
|
public function __construct(array $endpoints, ContainerInterface $container, BuilderFactory $builderFactory) |
22
|
|
|
{ |
23
|
|
|
foreach ($endpoints as $name => $handler) { |
24
|
|
|
$this->addEndpoint($name, $handler); |
25
|
|
|
} |
26
|
|
|
$this->container = $container; |
27
|
|
|
$this->builderFactory = $builderFactory; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function has(string $name): bool |
31
|
|
|
{ |
32
|
|
|
return isset($this->endpoints[$name]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
* @param string $handlerClassName |
38
|
|
|
* @psalm-param TLiteralClassString $handlerClassName |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function addEndpoint(string $name, string $handlerClassName): self |
42
|
|
|
{ |
43
|
|
|
$this->endpoints[$name] = $handlerClassName; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getByName(string $name): Endpoint |
49
|
|
|
{ |
50
|
|
|
if (!$this->has($name)) { |
51
|
|
|
throw new ConfigurationException(sprintf('Endpoint %s does not exist', $name)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->container->get($this->endpoints[$name])($this->builderFactory); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @psalm-return list<string> |
59
|
|
|
*/ |
60
|
|
|
public function getEndpointNames(): array |
61
|
|
|
{ |
62
|
|
|
return array_keys($this->endpoints); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|