1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Routing; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Loader\Loader; |
6
|
|
|
use Symfony\Component\Routing\Route; |
7
|
|
|
use Symfony\Component\Routing\RouteCollection; |
8
|
|
|
|
9
|
|
|
class EntityRouteLoader extends Loader |
10
|
|
|
{ |
11
|
|
|
const RESOURCE_TYPE = 'cruds_mount'; |
12
|
|
|
/** @var Route[][] */ |
13
|
|
|
private $routes = []; |
14
|
|
|
/** @var bool[] */ |
15
|
|
|
private $loaded = []; |
16
|
|
|
|
17
|
|
|
/** {@inheritdoc} */ |
18
|
12 |
|
public function load($mount, $type = null) |
19
|
|
|
{ |
20
|
12 |
|
$this->assertLoaded($mount); |
21
|
|
|
|
22
|
12 |
|
$collection = new RouteCollection(); |
23
|
12 |
|
if (!array_key_exists($mount, $this->routes)) { |
24
|
|
|
throw new \LogicException(sprintf('No routes configured for %s CRUDS mount point', $mount)); |
25
|
|
|
} |
26
|
|
|
|
27
|
12 |
|
foreach ($this->routes[$mount] as $name => $route) { |
28
|
12 |
|
$collection->add($name, $route); |
29
|
12 |
|
} |
30
|
|
|
|
31
|
12 |
|
$this->loaded = true; |
|
|
|
|
32
|
|
|
|
33
|
12 |
|
return $collection; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** {@inheritdoc} */ |
37
|
12 |
|
public function supports($mount, $type = null) |
38
|
|
|
{ |
39
|
12 |
|
return self::RESOURCE_TYPE === $type; |
40
|
|
|
} |
41
|
|
|
|
42
|
12 |
|
public function addRoute($mount, $name, $path, $controller, array $methods, array $options = []) |
43
|
|
|
{ |
44
|
12 |
|
$this->assertLoaded($mount); |
45
|
|
|
|
46
|
12 |
|
$this->routes[$mount][$name] = CrudsRoute::create($path, $controller, $methods, $options); |
47
|
12 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $resource |
51
|
|
|
*/ |
52
|
12 |
|
private function assertLoaded($resource) |
53
|
|
|
{ |
54
|
12 |
|
if (array_key_exists($resource, $this->loaded) && $this->loaded[$resource]) { |
55
|
|
|
throw new \LogicException('Already loaded'); |
56
|
|
|
} |
57
|
12 |
|
} |
58
|
|
|
} |
59
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..