|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/mvc package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Http\Router\Builder; |
|
11
|
|
|
|
|
12
|
|
|
use Aura\Router\Map; |
|
13
|
|
|
use Aura\Router\Route; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* RouteFactory |
|
17
|
|
|
* |
|
18
|
|
|
* @package Slick\Mvc\Http\Router\Builder |
|
19
|
|
|
* @author Filipe Silva <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class RouteFactory implements FactoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Receives an array with parameters to create a route or route group |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $name The route name |
|
27
|
|
|
* @param string|array $data Meta data fo the route |
|
28
|
|
|
* @param Map $map The route map to populate |
|
29
|
|
|
* |
|
30
|
|
|
* @return Route |
|
31
|
|
|
*/ |
|
32
|
|
|
public function parse($name, $data, Map $map) |
|
33
|
|
|
{ |
|
34
|
|
|
if (is_array($data)) { |
|
35
|
|
|
return $this->complexRoute($name, $data, $map); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $map->get($name, $data); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Route construct chain start |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name The route name |
|
45
|
|
|
* @param string|array $data Meta data fo the route |
|
46
|
|
|
* @param Map $map The route map to populate |
|
47
|
|
|
* |
|
48
|
|
|
* @return Route |
|
49
|
|
|
*/ |
|
50
|
|
|
private function complexRoute($name, array $data, Map $map) |
|
51
|
|
|
{ |
|
52
|
|
|
$route = $map->route($name, $data['path']); |
|
53
|
|
|
|
|
54
|
|
|
$method = array_key_exists('method', $data) |
|
55
|
|
|
? [$data['method']] |
|
56
|
|
|
: ['GET']; |
|
57
|
|
|
|
|
58
|
|
|
$allows = array_key_exists('allows', $data) |
|
59
|
|
|
? $data['allows'] |
|
60
|
|
|
: []; |
|
61
|
|
|
|
|
62
|
|
|
$route->allows(array_merge($method, $allows)); |
|
63
|
|
|
|
|
64
|
|
|
$this->addProperties($route, $data); |
|
65
|
|
|
|
|
66
|
|
|
return $route; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Parses data array to set route properties |
|
71
|
|
|
* |
|
72
|
|
|
* @param Route $route |
|
73
|
|
|
* @param array $data |
|
74
|
|
|
* |
|
75
|
|
|
* @return Route |
|
76
|
|
|
*/ |
|
77
|
|
|
private function addProperties(Route $route, array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
$methods = get_class_methods(Route::class); |
|
80
|
|
|
$methods = array_diff($methods, ["allows", "path"]); |
|
81
|
|
|
|
|
82
|
|
|
foreach($data as $method => $args) { |
|
83
|
|
|
if (in_array($method, $methods)) { |
|
84
|
|
|
$route->$method($args); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $route; |
|
89
|
|
|
} |
|
90
|
|
|
} |