|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\MetricBundle\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
|
6
|
|
|
use Symfony\Component\Config\Loader\LoaderResolverInterface; |
|
7
|
|
|
use Symfony\Component\Routing\Route; |
|
8
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
9
|
|
|
|
|
10
|
|
|
final class MetricRouteLoader implements LoaderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $controllers = []; |
|
13
|
|
|
|
|
14
|
|
|
private $loaded = false; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Add controllers on path for registration. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* @param string $path |
|
21
|
|
|
* @param string $serviceId |
|
22
|
|
|
* @param string $method |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \LogicException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function registerController(string $name, string $path, string $serviceId, string $method = '__invoke') |
|
27
|
|
|
{ |
|
28
|
|
|
if (array_key_exists($path, $this->controllers)) { |
|
29
|
|
|
throw new \LogicException('Cannot register metric controller on the same path twice'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->controllers[$name] = [$path, $serviceId, $method]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** {@inheritdoc} */ |
|
36
|
|
|
public function load($resource, $type = null): RouteCollection |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->loaded) { |
|
39
|
|
|
throw new \LogicException('Lamoda metrics routes have been already loaded'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$collection = new RouteCollection(); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($this->controllers as $name => list($path, $controller)) { |
|
45
|
|
|
$collection->add($controller, new Route($path, ['_controller' => $controller])); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->loaded = true; |
|
49
|
|
|
|
|
50
|
|
|
return $collection; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** {@inheritdoc} */ |
|
54
|
|
|
public function supports($resource, $type = null): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return 'lamoda_metrics' === $type; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** {@inheritdoc} */ |
|
60
|
|
|
public function getResolver() |
|
61
|
|
|
{ |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** {@inheritdoc} */ |
|
65
|
|
|
public function setResolver(LoaderResolverInterface $resolver) |
|
66
|
|
|
{ |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|