1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Ramsey\Uuid\Uuid; |
4
|
|
|
|
5
|
|
|
if (! function_exists('get_id')) { |
6
|
|
|
/** |
7
|
|
|
* Get id from a mixed variable. |
8
|
|
|
* |
9
|
|
|
* @param mixed $var |
10
|
|
|
* @param string $key |
11
|
|
|
* @return mixed |
12
|
|
|
*/ |
13
|
|
|
function get_id($var, $key = 'id') |
14
|
|
|
{ |
15
|
|
View Code Duplication |
if (is_object($var)) { |
|
|
|
|
16
|
|
|
return $var->{$key}; |
17
|
|
|
} elseif (is_array($var)) { |
18
|
|
|
return $var[$key]; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $var; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (! function_exists('is_domain')) { |
26
|
|
|
/** |
27
|
|
|
* Determines the current domain equals to the given domain identifier. |
28
|
|
|
* |
29
|
|
|
* @param string $identifier |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
function is_domain($identifier) |
33
|
|
|
{ |
34
|
|
|
return app('request')->getHost() === config('app.domains.'.$identifier); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (! function_exists('app_url')) { |
39
|
|
|
/** |
40
|
|
|
* Generate an URL for the application. |
41
|
|
|
* |
42
|
|
|
* @param string $path |
43
|
|
|
* @param mixed $parameters |
44
|
|
|
* @param string $identifier |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
function app_url($path = '', $parameters = null, $identifier = 'site') |
48
|
|
|
{ |
49
|
|
|
$path = trim($path, '/'); |
50
|
|
|
if (! empty($path) && ! starts_with($path, ['?', '&', '#'])) { |
51
|
|
|
$path = '/'.$path; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (! is_null($parameters)) { |
55
|
|
|
$query = http_build_query($parameters); |
56
|
|
|
if (! empty($query)) { |
57
|
|
|
$path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($identifier && ($root = config('support.url.'.$identifier))) { |
62
|
|
|
return $root.$path; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return url($path); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (! function_exists('revision')) { |
70
|
|
|
/** |
71
|
|
|
* Get the revisioned asset path. |
72
|
|
|
* |
73
|
|
|
* @param string $path |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
function revision($path) |
77
|
|
|
{ |
78
|
|
|
if ($rev = array_get(config('assets-version'), trim($path, DIRECTORY_SEPARATOR))) { |
79
|
|
|
return $path.'?'.$rev; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $path; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (! function_exists('asset_from')) { |
87
|
|
|
/** |
88
|
|
|
* Generate the URL to an asset from a custom root domain such as CDN, etc. |
89
|
|
|
* |
90
|
|
|
* @param string $root |
91
|
|
|
* @param string $path |
92
|
|
|
* @param bool|null $secure |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
function asset_from($root, $path = '', $secure = null) |
96
|
|
|
{ |
97
|
|
|
return app('url')->assetFrom($root, $path, $secure); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (! function_exists('asset_url')) { |
102
|
|
|
/** |
103
|
|
|
* Generate an asset URL. |
104
|
|
|
* |
105
|
|
|
* @param string $path |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
function asset_url($path, $identifier = 'asset') |
109
|
|
|
{ |
110
|
|
|
if (filter_var($path, FILTER_VALIDATE_URL) !== false) { |
111
|
|
|
return $path; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return config('support.url.'.$identifier).'/'.revision(trim($path, '/')); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (! function_exists('cdn_url')) { |
119
|
|
|
/** |
120
|
|
|
* Generate an asset CDN URL. |
121
|
|
|
* |
122
|
|
|
* @param string $path |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
function cdn_url($path) |
126
|
|
|
{ |
127
|
|
|
return asset_url($path, 'cdn'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (! function_exists('gravatar')) { |
132
|
|
|
/** |
133
|
|
|
* Generate a Gravatar url. |
134
|
|
|
* |
135
|
|
|
* @see http://cn.gravatar.com/site/implement/images/ |
136
|
|
|
* |
137
|
|
|
* @param string $email |
138
|
|
|
* @param int $size |
139
|
|
|
* @param string $default |
140
|
|
|
* @param string $rating |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
function gravatar($email, $size = 100, $default = null, $rating = null) |
144
|
|
|
{ |
145
|
|
|
if (is_null($default)) { |
146
|
|
|
$default = config('support.gravatar.default'); |
147
|
|
|
} |
148
|
|
|
if (is_null($rating)) { |
149
|
|
|
$rating = config('support.gravatar.rating'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$query = http_build_query(array_filter(compact('size', 'default', 'rating'))); |
153
|
|
|
|
154
|
|
|
return app('url')->assetFrom( |
155
|
|
|
config('support.gravatar.host', 'http://gravatar.com/avatar'), |
156
|
|
|
md5(strtolower(trim($email))).'?'.$query |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (! function_exists('optimus_encode')) { |
162
|
|
|
/** |
163
|
|
|
* Encode a number with Optimus. |
164
|
|
|
* |
165
|
|
|
* @param int $number |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
|
|
function optimus_encode($number) |
169
|
|
|
{ |
170
|
|
|
return app('optimus')->encode($number); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (! function_exists('optimus_decode')) { |
175
|
|
|
/** |
176
|
|
|
* Decode a number with Optimus. |
177
|
|
|
* |
178
|
|
|
* @param int $number |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
function optimus_decode($number) |
182
|
|
|
{ |
183
|
|
|
return app('optimus')->decode($number); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (! function_exists('random_uuid')) { |
188
|
|
|
/** |
189
|
|
|
* Generate a version 4 (random) UUID. |
190
|
|
|
* |
191
|
|
|
* @param bool $hex |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
function random_uuid($hex = false) |
195
|
|
|
{ |
196
|
|
|
$uuid = Uuid::uuid4(); |
197
|
|
|
|
198
|
|
|
return $hex ? $uuid->getHex() : $uuid->toString(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.