1 | <?php namespace Comodojo\Dispatcher\Router; |
||
30 | class Parser { |
||
31 | |||
32 | public $logger; |
||
33 | |||
34 | 1 | public function __construct(LoggerInterface $logger) { |
|
39 | |||
40 | // This method read the route (folder by folder recursively) and build |
||
41 | // the global regular expression against which all the request URI will be compared |
||
42 | 1 | public function read($folders = array(), Route $value = null, $regex = '') { |
|
43 | |||
44 | 1 | if (is_null($value)) { |
|
45 | |||
46 | 1 | $value = new Route(); |
|
47 | |||
48 | 1 | } |
|
49 | |||
50 | // if the first 'folder' is empty is removed |
||
51 | 1 | while (!empty($folders) && empty($folders[0])) { |
|
52 | |||
53 | array_shift($folders); |
||
54 | |||
55 | } |
||
56 | |||
57 | // if the 'folder' array is empty, the route has been fully analyzed |
||
58 | // this is the exit condition from the recursive loop. |
||
59 | 1 | if (empty($folders)) { |
|
60 | |||
61 | 1 | return '^'.$regex.'[\/]?$'; |
|
62 | |||
63 | } else { |
||
64 | |||
65 | // The first element of the array 'folders' is taken in order to be analyzed |
||
66 | 1 | $folder = array_shift($folders); |
|
67 | |||
68 | // All the parameters of the route must be json strings |
||
69 | 1 | $decoded = json_decode($folder, true); |
|
70 | |||
71 | 1 | if (!is_null($decoded) && is_array($decoded)) { |
|
72 | |||
73 | 1 | $param_regex = ''; |
|
74 | |||
75 | 1 | $param_required = false; |
|
76 | |||
77 | /* All the folders can include more than one parameter |
||
78 | * Eg: /service_name/{'param1': 'regex1', 'param2': 'regex2'}/ |
||
79 | * /calendar/{'ux_timestamp*': '\d{10}', 'microseconds': '\d{4}'}/ |
||
80 | * |
||
81 | * The '*' at the end of the paramerter name implies that the parameter is required |
||
82 | * This example can be read as a calendar service that accepts both |
||
83 | * timestamps in unix or javascript format. |
||
84 | * |
||
85 | * This is the reason of the following 'foreach' |
||
86 | */ |
||
87 | 1 | foreach ($decoded as $key => $string) { |
|
88 | |||
89 | 1 | $this->logger->debug("PARAMETER KEY: " . $key); |
|
90 | |||
91 | 1 | $this->logger->debug("PARAMETER STRING: " . $string); |
|
92 | |||
93 | /* The key and the regex of every paramater is passed to the 'param' |
||
94 | * method which will build an appropriate regular expression and will understand |
||
95 | * if the parameter is required and will build the Route query object |
||
96 | */ |
||
97 | 1 | $param_regex .= $this->param($key, $string, $value); |
|
98 | |||
99 | 1 | if ($value->isQueryRequired($key)) $param_required = true; |
|
100 | |||
101 | 1 | $this->logger->debug("PARAMETER REGEX: " . $param_regex); |
|
102 | |||
103 | 1 | } |
|
104 | // Once the parameter is analyzed, the result is passed to the next iteration |
||
105 | 1 | return $this->read( |
|
106 | 1 | $folders, |
|
107 | 1 | $value, |
|
108 | 1 | $regex.'(?:\/'.$param_regex.')'. (($param_required)?'{1}':'?') |
|
109 | 1 | ); |
|
110 | |||
111 | } else { |
||
112 | // if the element is not a json string, I assume it's the service name |
||
113 | 1 | $value->addService($folder); |
|
114 | |||
115 | 1 | return $this->read( |
|
116 | 1 | $folders, |
|
117 | 1 | $value, |
|
118 | 1 | $regex.'\/'.$folder |
|
119 | 1 | ); |
|
120 | |||
121 | } |
||
122 | |||
123 | } |
||
124 | |||
125 | } |
||
126 | |||
127 | // This method read a single parameter and build the regular expression |
||
128 | 1 | private function param($key, $string, $value) { |
|
167 | |||
168 | } |
||
169 |