1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use Faker\Factory;
|
4
|
|
|
use Faker\Generator;
|
|
|
|
|
5
|
|
|
use Resta\Router\Route;
|
|
|
|
|
6
|
|
|
use Resta\Support\Utils;
|
7
|
|
|
use Resta\Support\Filesystem;
|
8
|
|
|
use Resta\Logger\LoggerHandler;
|
9
|
|
|
use Store\Services\RequestService;
|
|
|
|
|
10
|
|
|
use Resta\Exception\ExceptionManager;
|
11
|
|
|
use Resta\Support\HigherOrderTapProxy;
|
12
|
|
|
use Resta\Response\ResponseOutManager;
|
13
|
|
|
use Resta\EventDispatcher\EventManager;
|
14
|
|
|
use Resta\Contracts\ExceptionContracts;
|
15
|
|
|
use Resta\Contracts\StaticPathContracts;
|
16
|
|
|
use Resta\Contracts\ApplicationContracts;
|
17
|
|
|
use Resta\Authenticate\AuthenticateContract;
|
18
|
|
|
use Resta\Authenticate\AuthenticateProvider;
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
20
|
|
|
use Resta\Foundation\PathManager\StaticPathList;
|
21
|
|
|
|
22
|
|
|
if (!function_exists('app')) {
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @return ApplicationContracts
|
26
|
|
|
*/
|
27
|
|
|
function app()
|
28
|
|
|
{
|
29
|
|
|
return appInstance();
|
30
|
|
|
}
|
31
|
|
|
}
|
32
|
|
|
|
33
|
|
|
if (!function_exists('appInstance')) {
|
34
|
|
|
|
35
|
|
|
/**
|
36
|
|
|
* @return mixed
|
37
|
|
|
*/
|
38
|
|
|
function appInstance()
|
39
|
|
|
{
|
40
|
|
|
return \application::getAppInstance();
|
|
|
|
|
41
|
|
|
}
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
if (!function_exists('applicationKey')) {
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @return string
|
48
|
|
|
*/
|
49
|
|
|
function applicationKey()
|
50
|
|
|
{
|
51
|
|
|
if(property_exists($kernel=app()->kernel(),'applicationKey')){
|
52
|
|
|
return $kernel->applicationKey;
|
53
|
|
|
}
|
54
|
|
|
return null;
|
55
|
|
|
|
56
|
|
|
}
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
if (!function_exists('auth')) {
|
60
|
|
|
/**
|
61
|
|
|
* @return AuthenticateContract
|
62
|
|
|
*/
|
63
|
|
|
function auth()
|
64
|
|
|
{
|
65
|
|
|
return app()->resolve(AuthenticateProvider::class);
|
66
|
|
|
}
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
if (!function_exists('bind')) {
|
70
|
|
|
|
71
|
|
|
function bind()
|
|
|
|
|
72
|
|
|
{
|
73
|
|
|
return (object)app()['serviceContainer'];
|
74
|
|
|
}
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
if (!function_exists('bundleName')) {
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* @return null|string
|
81
|
|
|
*/
|
82
|
|
|
function bundleName()
|
83
|
|
|
{
|
84
|
|
|
if(defined('endpoint')){
|
85
|
|
|
|
86
|
|
|
return endpoint.''.StaticPathList::$controllerBundleName;
|
87
|
|
|
}
|
88
|
|
|
return null;
|
89
|
|
|
}
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
if (!function_exists('config')) {
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @param null $config
|
|
|
|
|
96
|
|
|
* @param null $default
|
|
|
|
|
97
|
|
|
* @return mixed|null
|
98
|
|
|
*/
|
99
|
|
|
function config($config=null,$default=null)
|
100
|
|
|
{
|
101
|
|
|
$configResult = app()->config($config);
|
102
|
|
|
|
103
|
|
|
if($configResult === null && $default!==null){
|
|
|
|
|
104
|
|
|
return $default;
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
return $configResult;
|
108
|
|
|
}
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
if (!function_exists('container')) {
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* @param $class
|
115
|
|
|
* @param $bind array
|
116
|
|
|
* @return mixed
|
117
|
|
|
*/
|
118
|
|
|
function container($class,$bind=array())
|
119
|
|
|
{
|
120
|
|
|
return app()->singleton()->appClass->container(appInstance(),$class,$bind);
|
121
|
|
|
}
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
if (!function_exists('core')) {
|
125
|
|
|
|
126
|
|
|
/**
|
127
|
|
|
* @return mixed
|
128
|
|
|
*/
|
129
|
|
|
function core()
|
130
|
|
|
{
|
131
|
|
|
return app()->singleton();
|
132
|
|
|
}
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
if (!function_exists('dd')) {
|
136
|
|
|
function dd()
|
137
|
|
|
{
|
138
|
|
|
$args = func_get_args();
|
139
|
|
|
call_user_func_array('dump', $args);
|
140
|
|
|
die();
|
|
|
|
|
141
|
|
|
}
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
if (!function_exists('files')) {
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* @return Filesystem
|
148
|
|
|
*/
|
149
|
|
|
function files() : Filesystem
|
150
|
|
|
{
|
151
|
|
|
return app()->resolve(Filesystem::class);
|
152
|
|
|
}
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
if (!function_exists('environment')) {
|
156
|
|
|
function environment()
|
|
|
|
|
157
|
|
|
{
|
158
|
|
|
return app()->environment(func_get_args());
|
159
|
|
|
}
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
if (!function_exists('event')) {
|
163
|
|
|
|
164
|
|
|
/**
|
165
|
|
|
* @return EventManager
|
166
|
|
|
*/
|
167
|
|
|
function event()
|
168
|
|
|
{
|
169
|
|
|
return app()->singleton()->bindings['eventDispatcher'];
|
170
|
|
|
}
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
if (!function_exists('exception')) {
|
174
|
|
|
|
175
|
|
|
/**
|
176
|
|
|
* @param null $name
|
|
|
|
|
177
|
|
|
* @param array $params
|
178
|
|
|
* @return ExceptionContracts
|
179
|
|
|
*/
|
180
|
|
|
function exception($name=null,$params=array())
|
181
|
|
|
{
|
182
|
|
|
if(isset($params['key'])){
|
183
|
|
|
app()->register('errorInput',$params['key']);
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
$exceptionManager=ExceptionManager::class;
|
187
|
|
|
return app()->resolve($exceptionManager,['name'=>$name,'params'=>$params]);
|
188
|
|
|
}
|
189
|
|
|
}
|
190
|
|
|
|
191
|
|
|
if (!function_exists('faker')) {
|
192
|
|
|
|
193
|
|
|
/**
|
194
|
|
|
* @param null $locale
|
|
|
|
|
195
|
|
|
* @return Generator
|
196
|
|
|
*/
|
197
|
|
|
function faker($locale=null)
|
|
|
|
|
198
|
|
|
{
|
199
|
|
|
if($locale===null){
|
|
|
|
|
200
|
|
|
$faker=Factory::create();
|
201
|
|
|
}
|
202
|
|
|
else{
|
203
|
|
|
$faker=Factory::create($locale);
|
204
|
|
|
}
|
205
|
|
|
|
206
|
|
|
return $faker;
|
207
|
|
|
}
|
208
|
|
|
}
|
209
|
|
|
|
210
|
|
|
if (!function_exists('fingerPrint')) {
|
211
|
|
|
|
212
|
|
|
function fingerPrint()
|
213
|
|
|
{
|
214
|
|
|
return md5(sha1(implode("|",[
|
215
|
|
|
request()->getClientIp(),applicationKey()
|
216
|
|
|
])));
|
217
|
|
|
}
|
218
|
|
|
}
|
219
|
|
|
|
220
|
|
|
if (!function_exists('fullUrl')) {
|
221
|
|
|
|
222
|
|
|
function fullUrl()
|
223
|
|
|
{
|
224
|
|
|
return request()->getUri();
|
225
|
|
|
}
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
if (!function_exists('get')) {
|
229
|
|
|
|
230
|
|
|
/**
|
231
|
|
|
* @param null $param
|
|
|
|
|
232
|
|
|
* @param null $default
|
|
|
|
|
233
|
|
|
* @return null
|
234
|
|
|
*/
|
235
|
|
|
function get($param=null,$default=null)
|
236
|
|
|
{
|
237
|
|
|
//symfony request query object
|
238
|
|
|
$get=core()->get;
|
239
|
|
|
|
240
|
|
|
return ($param===null) ? $get : (isset($get[$param]) ? $get[$param] : $default);
|
|
|
|
|
241
|
|
|
}
|
242
|
|
|
}
|
243
|
|
|
|
244
|
|
|
if (!function_exists('headers')) {
|
245
|
|
|
|
246
|
|
|
/**
|
247
|
|
|
* @param null $param
|
|
|
|
|
248
|
|
|
* @param null $default
|
|
|
|
|
249
|
|
|
* @return array|string
|
250
|
|
|
*/
|
251
|
|
|
function headers($param=null,$default=null)
|
252
|
|
|
{
|
253
|
|
|
$list=[];
|
254
|
|
|
|
255
|
|
|
//We only get the objects in the list name to match the header objects
|
256
|
|
|
//that come with the request path to the objects sent by the client
|
257
|
|
|
foreach (request()->headers->all() as $key=>$value) {
|
258
|
|
|
$list[$key]=$value;
|
259
|
|
|
}
|
260
|
|
|
|
261
|
|
|
//return header list
|
262
|
|
|
return ($param===null) ? $list : (isset($list[$param]) ? $list[$param][0] : $default);
|
|
|
|
|
263
|
|
|
}
|
264
|
|
|
}
|
265
|
|
|
|
266
|
|
|
if (!function_exists('httpMethod')) {
|
267
|
|
|
|
268
|
|
|
/**
|
269
|
|
|
* @return string
|
270
|
|
|
*/
|
271
|
|
|
function httpMethod()
|
272
|
|
|
{
|
273
|
|
|
return strtolower(core()->httpMethod);
|
274
|
|
|
}
|
275
|
|
|
}
|
276
|
|
|
|
277
|
|
|
if (!function_exists('logger')) {
|
278
|
|
|
|
279
|
|
|
/**
|
280
|
|
|
* @param $file null
|
|
|
|
|
281
|
|
|
* @return LoggerHandler
|
282
|
|
|
*/
|
283
|
|
|
function logger($file=null)
|
284
|
|
|
{
|
285
|
|
|
return app()->resolve(LoggerHandler::class,['file'=>$file]);
|
286
|
|
|
}
|
287
|
|
|
}
|
288
|
|
|
|
289
|
|
|
if (!function_exists('request')) {
|
290
|
|
|
|
291
|
|
|
/**
|
292
|
|
|
* @return RequestService|Request
|
293
|
|
|
*/
|
294
|
|
|
function request()
|
|
|
|
|
295
|
|
|
{
|
296
|
|
|
return core()->request;
|
297
|
|
|
}
|
298
|
|
|
}
|
299
|
|
|
|
300
|
|
|
if (!function_exists('response')) {
|
301
|
|
|
|
302
|
|
|
/**
|
303
|
|
|
* @return ResponseOutManager
|
304
|
|
|
*/
|
305
|
|
|
function response()
|
306
|
|
|
{
|
307
|
|
|
$object=debug_backtrace()[1]['object'];
|
308
|
|
|
return new ResponseOutManager($object);
|
309
|
|
|
}
|
310
|
|
|
}
|
311
|
|
|
|
312
|
|
|
if (!function_exists('resolve')) {
|
313
|
|
|
|
314
|
|
|
/**
|
315
|
|
|
* @param $class
|
316
|
|
|
* @param array $bind
|
317
|
|
|
* @return mixed|null
|
318
|
|
|
*
|
319
|
|
|
* @throws \DI\DependencyException
|
320
|
|
|
* @throws \DI\NotFoundException
|
321
|
|
|
*/
|
322
|
|
|
function resolve($class,$bind=array())
|
323
|
|
|
{
|
324
|
|
|
return app()->resolve($class,$bind);
|
325
|
|
|
}
|
326
|
|
|
}
|
327
|
|
|
|
328
|
|
|
if (!function_exists('policy')) {
|
329
|
|
|
|
330
|
|
|
/**
|
331
|
|
|
* @return mixed
|
332
|
|
|
*/
|
333
|
|
|
function policy()
|
334
|
|
|
{
|
335
|
|
|
$policyPath = implode('/',[
|
336
|
|
|
Route::getRouteControllerNamespace(),
|
337
|
|
|
'Policy',
|
338
|
|
|
Route::getRouteControllerClass().'Policy'
|
339
|
|
|
]);
|
340
|
|
|
|
341
|
|
|
$policyNamespace = Utils::getNamespace($policyPath);
|
342
|
|
|
|
343
|
|
|
if(Utils::isNamespaceExists($policyNamespace)){
|
344
|
|
|
return app()->resolve(Utils::getNamespace($policyPath));
|
345
|
|
|
}
|
346
|
|
|
|
347
|
|
|
return stdClass::class;
|
348
|
|
|
|
349
|
|
|
}
|
350
|
|
|
}
|
351
|
|
|
|
352
|
|
|
if (!function_exists('route')) {
|
353
|
|
|
|
354
|
|
|
/**
|
355
|
|
|
* @param $key
|
356
|
|
|
* @return mixed
|
357
|
|
|
*/
|
358
|
|
|
function route($key=null)
|
|
|
|
|
359
|
|
|
{
|
360
|
|
|
if(is_null($key)){
|
361
|
|
|
return array_map(function($route){
|
362
|
|
|
return strtolower($route);
|
363
|
|
|
},app()->singleton()->appClass->route($key));
|
364
|
|
|
}
|
365
|
|
|
|
366
|
|
|
return app()->singleton()->appClass->route($key);
|
367
|
|
|
|
368
|
|
|
}
|
369
|
|
|
}
|
370
|
|
|
|
371
|
|
|
if (!function_exists('path')) {
|
372
|
|
|
|
373
|
|
|
/**
|
374
|
|
|
* @return StaticPathContracts
|
375
|
|
|
*/
|
376
|
|
|
function path()
|
377
|
|
|
{
|
378
|
|
|
return app()->path();
|
379
|
|
|
}
|
380
|
|
|
}
|
381
|
|
|
|
382
|
|
|
if (!function_exists('post')) {
|
383
|
|
|
|
384
|
|
|
/**
|
385
|
|
|
* @param null $param
|
|
|
|
|
386
|
|
|
* @param null $default
|
|
|
|
|
387
|
|
|
* @return mixed
|
388
|
|
|
*/
|
389
|
|
|
function post($param=null,$default=null)
|
390
|
|
|
{
|
391
|
|
|
//symfony request query object
|
392
|
|
|
$post=core()->post;
|
393
|
|
|
|
394
|
|
|
return ($param===null) ? $post : (isset($post[$param]) ? $post[$param] : $default);
|
|
|
|
|
395
|
|
|
}
|
396
|
|
|
}
|
397
|
|
|
|
398
|
|
|
if (!function_exists('tap')) {
|
399
|
|
|
|
400
|
|
|
/**
|
401
|
|
|
* @param $value
|
402
|
|
|
* @param $callback
|
403
|
|
|
* @return mixed
|
404
|
|
|
*/
|
405
|
|
|
function tap($value, $callback)
|
406
|
|
|
{
|
407
|
|
|
if (!is_callable($callback)) {
|
408
|
|
|
return new HigherOrderTapProxy($value);
|
409
|
|
|
}
|
410
|
|
|
|
411
|
|
|
$callback($value);
|
412
|
|
|
return $value;
|
413
|
|
|
}
|
414
|
|
|
}
|
415
|
|
|
|
416
|
|
|
if (!function_exists('trans')) {
|
417
|
|
|
|
418
|
|
|
/**
|
419
|
|
|
* @param $lang
|
420
|
|
|
* @param array $select
|
421
|
|
|
* @return mixed
|
422
|
|
|
*/
|
423
|
|
|
function trans($lang,$select=array())
|
424
|
|
|
{
|
425
|
|
|
return app()->singleton()->appClass->translator($lang,$select);
|
426
|
|
|
}
|
427
|
|
|
}
|
428
|
|
|
|
429
|
|
|
if (!function_exists('entities')) {
|
430
|
|
|
|
431
|
|
|
/**
|
432
|
|
|
* @param null|string $table
|
433
|
|
|
* @return void|array
|
434
|
|
|
*/
|
435
|
|
|
function entities($table=null)
|
436
|
|
|
{
|
437
|
|
|
if(!is_null($table) && app()->has('entities')){
|
438
|
|
|
return app()->get('entities')($table);
|
439
|
|
|
}
|
440
|
|
|
|
441
|
|
|
exception()->runtime('entities container not found');
|
442
|
|
|
}
|
443
|
|
|
}
|
444
|
|
|
|
445
|
|
|
if (!function_exists('serviceJson')) {
|
446
|
|
|
|
447
|
|
|
/**
|
448
|
|
|
* @return string
|
449
|
|
|
*/
|
450
|
|
|
function containerCacheFile()
|
451
|
|
|
{
|
452
|
|
|
$file = app()->path()->kernel().''.DIRECTORY_SEPARATOR.'service.json';
|
453
|
|
|
|
454
|
|
|
if(file_exists($file)){
|
455
|
|
|
return $file;
|
456
|
|
|
}
|
457
|
|
|
|
458
|
|
|
return null;
|
459
|
|
|
}
|
460
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: