1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\HandlerStack; |
4
|
|
|
use GuzzleHttp\MessageFormatter; |
|
|
|
|
5
|
|
|
use GuzzleHttp\Middleware; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
if (! function_exists('environment_file_path')) { |
10
|
|
|
/** |
11
|
|
|
* Get application environment file path. |
12
|
|
|
* |
13
|
|
|
* For both Laravel and Lumen framework. |
14
|
|
|
* |
15
|
|
|
* @param string $helper |
16
|
|
|
* @param string $envFile |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
function environment_file_path($helper = 'environmentFilePath', $envFile = '.env'): string |
21
|
|
|
{ |
22
|
12 |
|
$app = Container::getInstance(); |
23
|
|
|
|
24
|
12 |
|
if (method_exists($app, $helper)) { |
25
|
12 |
|
return $app->{$helper}(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Lumen |
29
|
1 |
|
return $app->basePath($envFile); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (! function_exists('alphanumeric')) { |
34
|
|
|
/** |
35
|
|
|
* Strip all symbols from a string. |
36
|
|
|
* |
37
|
|
|
* @see https://stackoverflow.com/a/16791863/2732184 Source |
38
|
|
|
* |
39
|
|
|
* @param string $str |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
function alphanumeric($str): string |
44
|
|
|
{ |
45
|
7 |
|
return preg_replace('/[^\p{L}\p{N}\s]/u', '', $str); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (! function_exists('array_merge_recursive_distinct')) { |
50
|
|
|
/** |
51
|
|
|
* Merge arrays recursively - but distinctly. |
52
|
|
|
* |
53
|
|
|
* @see https://www.php.net/manual/en/function.array-merge-recursive.php#92195 Issue |
54
|
|
|
* @see https://stackoverflow.com/a/25712428 Solution |
55
|
|
|
* |
56
|
|
|
* @param string $str |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
function array_merge_recursive_distinct(array $array1, array $array2) |
61
|
|
|
{ |
62
|
|
|
$merged = $array1; |
63
|
|
|
|
64
|
|
|
foreach ($array2 as $key => & $value) { |
65
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
66
|
|
|
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value); |
67
|
|
|
} elseif (is_numeric($key)) { |
68
|
|
|
if (! in_array($value, $merged)) { |
69
|
|
|
$merged[] = $value; |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
$merged[$key] = $value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $merged; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (! function_exists('append_log_middleware')) { |
81
|
|
|
/** |
82
|
|
|
* Add log middleware to handler stack. |
83
|
|
|
* |
84
|
|
|
* @see https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php |
85
|
|
|
* |
86
|
|
|
* @param \GuzzleHttp\HandlerStack $handlerStack |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
* |
90
|
|
|
* @return \GuzzleHttp\HandlerStack |
91
|
|
|
*/ |
92
|
|
|
function append_log_middleware(HandlerStack $handlerStack) |
93
|
|
|
{ |
94
|
28 |
|
$id = Str::random(10); |
95
|
|
|
|
96
|
28 |
|
$messageFormats = [ |
97
|
28 |
|
"HTTP_OUT_{$id} [Request] {method} {target}" => 'info', |
98
|
28 |
|
"HTTP_OUT_{$id} [Request] [Headers] \n{req_headers}" => 'debug', |
99
|
28 |
|
"HTTP_OUT_{$id} [Request] [Body] {req_body}" => 'debug', |
100
|
28 |
|
"HTTP_OUT_{$id} [Response] HTTP/{version} {code} {phrase} Size: {res_header_Content-Length}" => 'info', |
101
|
28 |
|
"HTTP_OUT_{$id} [Response] [Headers] \n{res_headers}" => 'debug', |
102
|
28 |
|
"HTTP_OUT_{$id} [Response] [Body] {res_body}" => 'debug', |
103
|
28 |
|
"HTTP_OUT_{$id} [Error] {error}" => 'error', |
104
|
28 |
|
]; |
105
|
|
|
|
106
|
28 |
|
$logger = Container::getInstance()->get('log'); |
107
|
|
|
|
108
|
28 |
|
collect($messageFormats)->each(function ($level, $format) use ($logger, $handlerStack) { |
|
|
|
|
109
|
28 |
|
$messageFormatter = new MessageFormatter($format); |
110
|
|
|
|
111
|
28 |
|
$logMiddleware = Middleware::log($logger, $messageFormatter, $level); |
112
|
|
|
|
113
|
28 |
|
$handlerStack->unshift($logMiddleware); |
114
|
28 |
|
}); |
115
|
|
|
|
116
|
28 |
|
return $handlerStack; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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: