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
|
12 |
|
public function makeName(string $path, Action $action): string |
22
|
|
|
{ |
23
|
12 |
|
return (string) (new Str($path)) |
24
|
12 |
|
->prepend('innmind_rest_server.') |
25
|
12 |
|
->append('.') |
26
|
12 |
|
->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
|
24 |
|
public function makeRoute(string $path, Action $action): Route |
38
|
|
|
{ |
39
|
24 |
|
$name = $path; |
40
|
24 |
|
$path = (new Str($path))->replace('.', '/')->append('/'); |
41
|
|
|
|
42
|
24 |
|
switch ((string) $action) { |
43
|
24 |
|
case Action::GET: |
44
|
10 |
|
$path = $path->append('{identity}'); |
45
|
22 |
|
case Action::LIST: |
46
|
12 |
|
$method = MethodInterface::GET; |
47
|
12 |
|
break; |
48
|
20 |
|
case Action::CREATE: |
49
|
10 |
|
$method = MethodInterface::POST; |
50
|
10 |
|
break; |
51
|
18 |
|
case Action::UPDATE: |
52
|
10 |
|
$method = MethodInterface::PUT; |
53
|
10 |
|
$path = $path->append('{identity}'); |
54
|
10 |
|
break; |
55
|
16 |
|
case Action::REMOVE: |
56
|
10 |
|
$method = MethodInterface::DELETE; |
57
|
10 |
|
$path = $path->append('{identity}'); |
58
|
10 |
|
break; |
59
|
14 |
|
case Action::LINK: |
60
|
10 |
|
$method = MethodInterface::LINK; |
61
|
10 |
|
$path = $path->append('{identity}'); |
62
|
10 |
|
break; |
63
|
12 |
|
case Action::UNLINK: |
64
|
10 |
|
$method = MethodInterface::UNLINK; |
65
|
10 |
|
$path = $path->append('{identity}'); |
66
|
10 |
|
break; |
67
|
10 |
|
case Action::OPTIONS: |
68
|
10 |
|
$method = MethodInterface::OPTIONS; |
69
|
10 |
|
break; |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
return new Route( |
73
|
24 |
|
(string) $path, |
74
|
|
|
[ |
75
|
24 |
|
'_innmind_resource' => $name, |
76
|
24 |
|
'_innmind_action' => (string) $action, |
77
|
24 |
|
'_controller' => sprintf( |
78
|
24 |
|
'innmind_rest_server.controller.resource.%s:defaultAction', |
79
|
|
|
$action |
80
|
|
|
), |
81
|
|
|
], |
82
|
24 |
|
[], |
83
|
24 |
|
[], |
84
|
24 |
|
'', |
85
|
24 |
|
[], |
86
|
|
|
$method |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|