1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Routing; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\ServerBundle\Exception\{ |
7
|
|
|
InvalidArgumentException, |
8
|
|
|
RouteLoaderLoadedMultipleTimesException |
9
|
|
|
}; |
10
|
|
|
use Innmind\Rest\Server\{ |
11
|
|
|
Definition\Directory, |
12
|
|
|
Definition\HttpResource, |
13
|
|
|
Action |
14
|
|
|
}; |
15
|
|
|
use Innmind\Http\Message\MethodInterface; |
16
|
|
|
use Innmind\Immutable\{ |
17
|
|
|
MapInterface, |
18
|
|
|
StringPrimitive as Str |
19
|
|
|
}; |
20
|
|
|
use Symfony\Component\{ |
21
|
|
|
Config\Loader\Loader, |
22
|
|
|
Routing\RouteCollection, |
23
|
|
|
Routing\Route |
24
|
|
|
}; |
25
|
|
|
|
26
|
|
|
final class RouteLoader extends Loader |
27
|
|
|
{ |
28
|
|
|
private $directories; |
29
|
|
|
private $routeFactory; |
30
|
|
|
private $imported = false; |
31
|
|
|
|
32
|
3 |
View Code Duplication |
public function __construct( |
|
|
|
|
33
|
|
|
MapInterface $directories, |
34
|
|
|
RouteFactory $routeFactory |
35
|
|
|
) { |
36
|
|
|
if ( |
37
|
3 |
|
(string) $directories->keyType() !== 'string' || |
38
|
3 |
|
(string) $directories->valueType() !== Directory::class |
39
|
|
|
) { |
40
|
|
|
throw new InvalidArgumentException; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$this->directories = $directories; |
44
|
3 |
|
$this->routeFactory = $routeFactory; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
2 |
|
public function load($resource, $type = null) |
51
|
|
|
{ |
52
|
2 |
|
if ($this->imported === true) { |
53
|
1 |
|
throw new RouteLoaderLoadedMultipleTimesException; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$routes = new RouteCollection; |
57
|
|
|
|
58
|
2 |
|
foreach ($this->directories as $directory) { |
59
|
2 |
|
$routes->addCollection( |
60
|
2 |
|
$this->buildDirectoryRoutes($directory) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$routes->add( |
65
|
2 |
|
'innmind_rest_server_capabilities', |
66
|
2 |
|
new Route( |
67
|
2 |
|
'/*', |
68
|
2 |
|
['_controller' => 'innmind_rest_server.controller.capabilities:capabilitiesAction'], |
69
|
2 |
|
[], |
70
|
2 |
|
[], |
71
|
2 |
|
'', |
72
|
2 |
|
[], |
73
|
2 |
|
[MethodInterface::OPTIONS] |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
|
77
|
2 |
|
$this->imported = true; |
78
|
|
|
|
79
|
2 |
|
return $routes; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function supports($resource, $type = null) |
86
|
|
|
{ |
87
|
1 |
|
return $type === 'innmind_rest'; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
private function buildDirectoryRoutes(Directory $directory): RouteCollection |
91
|
|
|
{ |
92
|
|
|
return $directory |
93
|
2 |
|
->flatten() |
94
|
2 |
|
->reduce( |
95
|
2 |
|
new RouteCollection, |
96
|
|
|
function(RouteCollection $routes, string $name, HttpResource $definition) { |
97
|
2 |
|
$routes->addCollection( |
98
|
2 |
|
$this->buildResourceRoutes($name, $definition) |
99
|
|
|
); |
100
|
|
|
|
101
|
2 |
|
return $routes; |
102
|
2 |
|
} |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
private function buildResourceRoutes( |
107
|
|
|
string $name, |
108
|
|
|
HttpResource $definition |
109
|
|
|
): RouteCollection { |
110
|
2 |
|
return Action::all() |
111
|
2 |
|
->reduce( |
112
|
2 |
|
new RouteCollection, |
113
|
2 |
|
function(RouteCollection $carry, string $action) use ($name, $definition) { |
114
|
|
|
if ( |
115
|
2 |
|
$definition->options()->hasKey('actions') && |
116
|
2 |
|
!in_array($action, $definition->options()->get('actions')) |
117
|
|
|
) { |
118
|
|
|
return $carry; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$carry->add( |
122
|
2 |
|
$this->routeFactory->makeName($name, new Action($action)), |
123
|
2 |
|
$this->routeFactory->makeRoute($name, new Action($action)) |
124
|
|
|
); |
125
|
|
|
|
126
|
2 |
|
return $carry; |
127
|
2 |
|
} |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.