|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Routing; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Rest\Server\Action; |
|
7
|
|
|
use Innmind\Http\Message\MethodInterface; |
|
8
|
|
|
use Innmind\Immutable\StringPrimitive as Str; |
|
9
|
|
|
use Symfony\Component\Routing\Route; |
|
10
|
|
|
|
|
11
|
|
|
final class RouteFactory |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Make a route name |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $path Path to resource definition |
|
17
|
|
|
* @param Action $action |
|
18
|
|
|
* |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function makeName(string $path, Action $action): string |
|
22
|
|
|
{ |
|
23
|
3 |
|
return (string) (new Str($path)) |
|
24
|
3 |
|
->prepend('innmind_rest_server.') |
|
25
|
3 |
|
->append('.') |
|
26
|
3 |
|
->append((string) $action); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Make a route |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $path Path to resource definition |
|
33
|
|
|
* @param Action $action |
|
34
|
|
|
* |
|
35
|
|
|
* @return Route |
|
36
|
|
|
*/ |
|
37
|
11 |
|
public function makeRoute(string $path, Action $action): Route |
|
38
|
|
|
{ |
|
39
|
11 |
|
$name = $path; |
|
40
|
11 |
|
$path = (new Str($path))->replace('.', '/')->append('/'); |
|
41
|
11 |
|
$condition = ''; |
|
42
|
|
|
|
|
43
|
11 |
|
switch ((string) $action) { |
|
44
|
11 |
|
case Action::GET: |
|
45
|
3 |
|
$path = $path->append('{identity}'); |
|
46
|
10 |
|
case Action::LIST: |
|
47
|
4 |
|
$method = MethodInterface::GET; |
|
48
|
4 |
|
break; |
|
49
|
9 |
|
case Action::CREATE: |
|
50
|
3 |
|
$method = MethodInterface::POST; |
|
51
|
3 |
|
break; |
|
52
|
8 |
|
case Action::UPDATE: |
|
53
|
3 |
|
$method = MethodInterface::PUT; |
|
54
|
3 |
|
$path = $path->append('{identity}'); |
|
55
|
3 |
|
break; |
|
56
|
7 |
|
case Action::REMOVE: |
|
57
|
3 |
|
$method = MethodInterface::DELETE; |
|
58
|
3 |
|
$path = $path->append('{identity}'); |
|
59
|
3 |
|
break; |
|
60
|
6 |
View Code Duplication |
case Action::LINK: |
|
61
|
3 |
|
$method = MethodInterface::LINK; |
|
62
|
3 |
|
$path = $path->append('{identity}'); |
|
63
|
3 |
|
$condition = 'request.headers.has(\'Link\')'; |
|
64
|
3 |
|
break; |
|
65
|
5 |
View Code Duplication |
case Action::UNLINK: |
|
66
|
3 |
|
$method = MethodInterface::UNLINK; |
|
67
|
3 |
|
$path = $path->append('{identity}'); |
|
68
|
3 |
|
$condition = 'request.headers.has(\'Link\')'; |
|
69
|
3 |
|
break; |
|
70
|
4 |
|
case Action::OPTIONS: |
|
71
|
4 |
|
$method = MethodInterface::OPTIONS; |
|
72
|
4 |
|
break; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
11 |
|
return new Route( |
|
76
|
11 |
|
(string) $path, |
|
77
|
|
|
[ |
|
78
|
11 |
|
'_innmind_resource' => $name, |
|
79
|
11 |
|
'_innmind_action' => (string) $action, |
|
80
|
|
|
'_controller' => sprintf( |
|
81
|
11 |
|
'innmind_rest_server.controller.resource:%sAction', |
|
82
|
11 |
|
$action |
|
83
|
11 |
|
), |
|
84
|
11 |
|
], |
|
85
|
|
|
[], |
|
86
|
|
|
[], |
|
87
|
|
|
'', |
|
88
|
|
|
[], |
|
89
|
|
|
$method, |
|
90
|
|
|
$condition |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|