1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Url;
|
4
|
|
|
|
5
|
|
|
use Resta\Support\Arr;
|
6
|
|
|
use Resta\Support\Utils;
|
7
|
|
|
use Resta\Foundation\ApplicationProvider;
|
8
|
|
|
|
9
|
|
|
class UrlProvider extends ApplicationProvider
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
* @var array $urlList
|
13
|
|
|
*/
|
14
|
|
|
public $urlList = [];
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var $path
|
|
|
|
|
18
|
|
|
*/
|
19
|
|
|
private $path;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @var array $urlNames
|
23
|
|
|
*/
|
24
|
|
|
protected $urlNames = ['project','version','endpoint','method'];
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* assign url list
|
28
|
|
|
*
|
29
|
|
|
* @return void
|
30
|
|
|
*/
|
31
|
|
|
public function assignUrlList()
|
32
|
|
|
{
|
33
|
|
|
// We treat the url parameters in the size of
|
34
|
|
|
// the application usage and get the values
|
35
|
|
|
// to be processed throughout the application in query format.
|
36
|
|
|
$query = $this->convertArrayForQuery();
|
37
|
|
|
|
38
|
|
|
$parametersYaml = $this->app()->path()->app().'parameters.yaml';
|
|
|
|
|
39
|
|
|
|
40
|
|
|
if(isset($query[0]) && $this->app->has('parametersYaml')){
|
41
|
|
|
$callParametersYaml = $this->app->get('parametersYaml');
|
42
|
|
|
if(isset($callParametersYaml['workingDir'])){
|
43
|
|
|
$parametersWorkingDir = explode('@',$callParametersYaml['workingDir']);
|
44
|
|
|
if($query[0]==$parametersWorkingDir[0]){
|
45
|
|
|
$query[0] = $parametersWorkingDir[1] ?? $query[0];
|
46
|
|
|
}
|
47
|
|
|
}
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
// if the version in the query array does not conform to the format,
|
51
|
|
|
// it will be formatted.
|
52
|
|
|
$query = $this->queryFormatProcess($query);
|
53
|
|
|
|
54
|
|
|
foreach ($query as $key=>$value){
|
55
|
|
|
|
56
|
|
|
//set url list for urlNames property
|
57
|
|
|
if(isset($this->urlNames[$key])){
|
58
|
|
|
$this->getUrlListValues($key,$value);
|
59
|
|
|
}
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
// If there is no method key in the urlList property,
|
63
|
|
|
// then by default we assign the index to the method property.
|
64
|
|
|
if(!isset($this->urlList['method'])){
|
65
|
|
|
$this->urlList['method'] = 'index';
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
//determines the endpoint method for your project
|
69
|
|
|
$this->urlList['parameters'] = array_slice($query,3);
|
70
|
|
|
|
71
|
|
|
//url global instance
|
72
|
|
|
if($this->app->has('routeParameters')===false){
|
73
|
|
|
$this->definitor($this->urlList);
|
74
|
|
|
}
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
* convert array for query
|
79
|
|
|
*
|
80
|
|
|
* @return array
|
81
|
|
|
*/
|
82
|
|
|
public function convertArrayForQuery()
|
83
|
|
|
{
|
84
|
|
|
//set path for query
|
85
|
|
|
$query = $this->path;
|
86
|
|
|
|
87
|
|
|
//convert array for query
|
88
|
|
|
//we are removing the first empty element from the array due to the slash sign.
|
89
|
|
|
if(is_null($this->path)){
|
90
|
|
|
$query = $this->getRequestPathInfo();
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
array_shift($query);
|
94
|
|
|
|
95
|
|
|
//we set the first letter of the array elements
|
96
|
|
|
//to be big according to the project standards
|
97
|
|
|
return array_map(
|
98
|
|
|
function($query) {
|
99
|
|
|
return ucfirst($query);
|
100
|
|
|
},$query);
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* url definitor
|
105
|
|
|
*
|
106
|
|
|
* @param $urlList
|
107
|
|
|
* @return void
|
108
|
|
|
*/
|
109
|
|
|
public function definitor($urlList)
|
110
|
|
|
{
|
111
|
|
|
define('version',$urlList['version']);
|
112
|
|
|
define('endpoint',$urlList['endpoint']);
|
113
|
|
|
define('app',$urlList['project']);
|
114
|
|
|
define('method',$urlList['method']);
|
115
|
|
|
|
116
|
|
|
//route parameters kernel object register
|
117
|
|
|
$this->app->register('routeParameters',$urlList['parameters']);
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* get path
|
122
|
|
|
*
|
123
|
|
|
* @return mixed
|
124
|
|
|
*/
|
125
|
|
|
public function getPath()
|
126
|
|
|
{
|
127
|
|
|
return $this->path;
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
/**
|
131
|
|
|
* get request path info
|
132
|
|
|
*
|
133
|
|
|
* @param array $path
|
134
|
|
|
* @return array
|
135
|
|
|
*/
|
136
|
|
|
public function getRequestPathInfo($path=array())
|
137
|
|
|
{
|
138
|
|
|
if(count($path)){
|
139
|
|
|
$this->path = $path;
|
140
|
|
|
}
|
141
|
|
|
else{
|
142
|
|
|
return Utils::getRequestPathInfo();
|
143
|
|
|
}
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* get url list values
|
148
|
|
|
*
|
149
|
|
|
* @param $key
|
150
|
|
|
* @param $value
|
151
|
|
|
*/
|
152
|
|
|
private function getUrlListValues($key,$value)
|
153
|
|
|
{
|
154
|
|
|
//If the value from the url is an external value, the default format is applied.
|
155
|
|
|
$this->urlList[$this->urlNames[$key]] = (strlen($value)>0) ? $value : null;
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* url provider application handle
|
161
|
|
|
*
|
162
|
|
|
* @return mixed
|
163
|
|
|
*/
|
164
|
|
|
public function handle()
|
165
|
|
|
{
|
166
|
|
|
//convert array for query
|
167
|
|
|
//assign url list
|
168
|
|
|
$this->assignUrlList();
|
169
|
|
|
|
170
|
|
|
//register to container urlComponent value
|
171
|
|
|
$this->app->register('urlComponent',$this->urlList);
|
172
|
|
|
|
173
|
|
|
//we make url parse resolving with resolved
|
174
|
|
|
return (new UrlParseParamResolved)->urlParamResolve($this);
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* get query format process
|
179
|
|
|
*
|
180
|
|
|
* @param $query
|
181
|
|
|
* @return mixed
|
182
|
|
|
*/
|
183
|
|
|
private function queryFormatProcess($query)
|
184
|
|
|
{
|
185
|
|
|
// at urlNames property,
|
186
|
|
|
// we get the key of the version value registered.
|
187
|
|
|
$versionKey = array_search('version',$this->urlNames);
|
188
|
|
|
|
189
|
|
|
// if the query array has a version key,
|
190
|
|
|
// and the value does not start with Vnumber, the version will definitely be formatted.
|
191
|
|
|
if(isset($query[$versionKey]) && !preg_match('@V(\d+)@',$query[$versionKey])){
|
192
|
|
|
$query = Arr::overwriteWith($query,[$versionKey=>'V1']);
|
193
|
|
|
}
|
194
|
|
|
else{
|
195
|
|
|
|
196
|
|
|
if(!isset($query[$versionKey])){
|
197
|
|
|
$query[$versionKey] = 'V1';
|
198
|
|
|
}
|
199
|
|
|
|
200
|
|
|
if(!isset($query[$versionKey+1])){
|
201
|
|
|
$query[$versionKey+1] = NULL;
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
}
|
205
|
|
|
|
206
|
|
|
return $query;
|
207
|
|
|
}
|
208
|
|
|
|
209
|
|
|
|
210
|
|
|
} |