1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.newicon.net/neon |
4
|
|
|
* @copyright Copyright (c) 2016 Newicon Ltd |
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
6
|
|
|
* @author Steve O'Brien <[email protected]> 13/11/2016 17:38 |
7
|
|
|
* @package neon |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This file is autoloaded via the composer autoload_file.php |
12
|
|
|
*/ |
13
|
|
|
use \neon\core\helpers\Str; |
14
|
|
|
use \neon\core\helpers\Collection; |
15
|
|
|
use \neon\core\helpers\Arr; |
16
|
|
|
use \neon\core\Env; |
17
|
|
|
use \Carbon\Carbon; |
18
|
|
|
|
19
|
|
|
if (! function_exists('dp')) { |
20
|
|
|
/** |
21
|
|
|
* Debug function. Prints all passed in arguments to the screen. |
22
|
|
|
* arguments containing objects or arrays are output using print_r |
23
|
|
|
* arguments containing simple types are output using var_export. |
24
|
|
|
* **Note:** Strings will be shown in single quotes. |
25
|
|
|
* **Note:** boolean's will be output as string true | false |
26
|
|
|
* **Note:** if not in debug mode, will print to the error log |
27
|
|
|
* @param mixed |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
function dp(...$dump) |
31
|
|
|
{ |
32
|
|
|
echo dp_string(...$dump); |
33
|
|
|
$backtrace = backtrace_line_by_function('dp'); |
34
|
|
|
if (neon()->debug) |
|
|
|
|
35
|
|
|
echo $backtrace; |
36
|
|
|
else |
37
|
|
|
\Neon::error($backtrace); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (! function_exists('backtrace_line_by_function')) { |
42
|
|
|
/** |
43
|
|
|
* Tries to get the line and file that called the specified functions |
44
|
|
|
* - will return the earliest match in execution order that it found. |
45
|
|
|
* @see dp() |
46
|
|
|
* @param string $functionToShow function name to find |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
function backtrace_line_by_function($functionToShow = 'dp') |
50
|
|
|
{ |
51
|
|
|
$bt = debug_backtrace(); |
52
|
|
|
$lineInfo = ''; |
53
|
|
|
foreach($bt as $trace) { |
54
|
|
|
if (isset($trace['function']) && $trace['function'] === $functionToShow) { |
55
|
|
|
$lineInfo = '==> [' . (isset($trace['file']) ? $trace['file'] : ' unknown file '). ':' . (isset($trace['line']) ? $trace['line'] : ' unknown line ' ) . ']'; |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $lineInfo; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! function_exists('dp_string')) { |
64
|
|
|
/** |
65
|
|
|
* This generates a nice debug string and returns it. It will only |
66
|
|
|
* print to the screen if you are in debug mode. Otherwise it prints |
67
|
|
|
* to Neon::error log. |
68
|
|
|
* @see dp() |
69
|
|
|
* @param mixed $dump |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
function dp_string(...$dump) |
73
|
|
|
{ |
74
|
|
|
if (php_sapi_name() == 'cli') { |
75
|
|
|
// use symfony dump function |
76
|
|
|
dump(...$dump); |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
// only allow printing of debug data to the screen if in debug mode |
80
|
|
|
$arguments = func_get_args(); |
81
|
|
|
$str = ''; |
82
|
|
|
foreach ($arguments as $arg) { |
83
|
|
|
$debug = (is_object($arg) || is_array($arg)) |
84
|
|
|
? print_r($arg, true) : var_export($arg, true); |
85
|
|
|
$str .= "<pre>$debug</pre>"; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (neon()->debug) |
|
|
|
|
89
|
|
|
return $str; |
90
|
|
|
\Neon::error($str); |
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (! function_exists('dd')) { |
96
|
|
|
/** |
97
|
|
|
* This is the same as the dp (debug print) function but exits after |
98
|
|
|
* **Note:** If not in debug will print to log error and not exit |
99
|
|
|
* @see dp() |
100
|
|
|
* @param mixed $dump |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
function dd(...$dump) |
104
|
|
|
{ |
105
|
|
|
echo dp_string(...$dump); |
106
|
|
|
$backtrace = backtrace_line_by_function('dd'); |
107
|
|
|
if (neon()->debug) { |
|
|
|
|
108
|
|
|
echo $backtrace; |
109
|
|
|
exit; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
\Neon::error($backtrace); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (! function_exists('debug_message')) { |
116
|
|
|
/** |
117
|
|
|
* This is the same as the dp_string (debug print) function however it returns the string |
118
|
|
|
* The main difference is that it will only output a message if neon is in debug mode |
119
|
|
|
* Therefore it is safe to leave in the source code on error pages |
120
|
|
|
* @see dp() |
121
|
|
|
* @param mixed $dump |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
function debug_message(...$dump) |
125
|
|
|
{ |
126
|
|
|
// security feature to ensure debug is not displayed unless in debug mode |
127
|
|
|
if (!neon()->debug) |
|
|
|
|
128
|
|
|
return ''; |
129
|
|
|
return dp_string(...$dump); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
if (! function_exists('neon')) { |
135
|
|
|
/** |
136
|
|
|
* Shortcut method to ```\Neon::$app``` |
137
|
|
|
* @param string $name |
138
|
|
|
* @return \neon\core\ApplicationWeb |
139
|
|
|
*/ |
140
|
|
|
function neon($name = null) |
141
|
|
|
{ |
142
|
786 |
|
return \Neon::app($name); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (! function_exists('setting')) { |
147
|
|
|
/** |
148
|
|
|
* @param string $app the app responsible for this setting |
149
|
|
|
* @param string $name the setting name |
150
|
|
|
* @param mixed $default the default property to return if the setting does not exist |
151
|
|
|
* @param boolean $refresh to force a refresh - will trigger a reload of the settings - this is only necessary if |
152
|
|
|
* the setting has been set in the current session |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
function setting($app, $name, $default=null, $refresh=false) |
156
|
|
|
{ |
157
|
3 |
|
return neon()->getSettingsManager()->get($app, $name, $default, $refresh); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (! function_exists('set_setting')) { |
162
|
|
|
/** |
163
|
|
|
* @param string $app the app responsible for this setting |
164
|
|
|
* @param string $name the setting name |
165
|
|
|
* @param mixed $value the setting value |
166
|
|
|
* @return boolean whether successfully saved |
167
|
|
|
*/ |
168
|
|
|
function set_setting($app, $name, $value) |
169
|
|
|
{ |
170
|
|
|
return neon()->getSettingsManager()->set($app, $name, $value); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (! function_exists('url')) { |
175
|
|
|
/** |
176
|
|
|
* Alias of \neon\core\helpers\Url::to |
177
|
|
|
* @see \neon\core\helpers\Url::to() |
178
|
|
|
* |
179
|
|
|
* @param array|string $url the parameter to be used to generate a valid URL |
180
|
|
|
* @param boolean|string $absolute the URI scheme to use in the generated URL: |
181
|
|
|
* |
182
|
|
|
* - `false` (default): generating a relative URL. |
183
|
|
|
* - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::hostInfo]]. |
184
|
|
|
* |
185
|
|
|
* @return string the generated URL |
186
|
|
|
* @throws InvalidParamException a relative route is given while there is no active controller |
187
|
|
|
*/ |
188
|
|
|
function url($url = '', $absolute = false) |
189
|
|
|
{ |
190
|
3 |
|
return \neon\core\helpers\Page::url($url, $absolute); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (! function_exists('url_base')) { |
195
|
|
|
/** |
196
|
|
|
* Returns the base URL of the current request. |
197
|
|
|
* @param boolean|string $scheme the URI scheme to use in the returned base URL: |
198
|
|
|
* |
199
|
|
|
* - `false`: returning the base URL without host info. |
200
|
|
|
* - `true` (default): returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::hostInfo]]. |
201
|
|
|
* - string: returning an absolute base URL with the specified scheme (either `http` or `https`). |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
function url_base($scheme = true) |
205
|
|
|
{ |
206
|
|
|
return \neon\core\helpers\Url::base($scheme); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (! function_exists('to_bytes')) { |
211
|
|
|
/** |
212
|
|
|
* Returns the number of bytes as integer from a strings like `128K` | `128M` | `2G` | `1T` | `1P` |
213
|
|
|
* |
214
|
|
|
* @param string $string A bytes string like 128M |
215
|
|
|
* @return int |
216
|
|
|
*/ |
217
|
|
|
function to_bytes($string) { |
218
|
39 |
|
return \neon\core\helpers\Str::toBytes($string); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (! function_exists('format_bytes')) { |
223
|
|
|
/** |
224
|
|
|
* Output a formatted size like '128M' from bytes. |
225
|
|
|
* |
226
|
|
|
* @param int $size |
227
|
|
|
* @param int $precision |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
function format_bytes($size, $precision = 2) { |
231
|
|
|
return \neon\core\helpers\Str::formatBytes($size, $precision); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (! function_exists('truthy')) { |
236
|
|
|
/** |
237
|
|
|
* Php if statement handles truthy and falsey string conversions by default e.g. evaluating a '0' string to false |
238
|
|
|
* however a string of 'false' is evaluated as true. This function resolves that difference. This should be used |
239
|
|
|
* in caution in places where you are expecting a boolean. |
240
|
|
|
* @param string $value - the value to test |
241
|
|
|
* @return boolean |
242
|
|
|
*/ |
243
|
|
|
function truthy($value) |
244
|
|
|
{ |
245
|
3 |
|
if ($value && strtolower($value) !== "false") |
246
|
3 |
|
return true; |
247
|
3 |
|
return false; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (! function_exists('env')) { |
252
|
|
|
/** |
253
|
|
|
* Gets the value of an environment variable. |
254
|
|
|
* Interprets 'true', 'false', 'empty' and 'null' strings appropriately |
255
|
|
|
* |
256
|
|
|
* @param string $key |
257
|
|
|
* @param mixed $default |
258
|
|
|
* @return mixed |
259
|
|
|
*/ |
260
|
|
|
function env($key, $default = null) |
261
|
|
|
{ |
262
|
66 |
|
$value = getenv($key); |
263
|
|
|
|
264
|
66 |
|
if ($value === false) { |
265
|
3 |
|
return $default; |
266
|
|
|
} |
267
|
66 |
|
switch (strtolower($value)) { |
268
|
66 |
|
case 'true': case 'on': case 'yes': |
269
|
9 |
|
return true; |
270
|
57 |
|
case 'false': case 'off': case 'no': case 'none': |
271
|
12 |
|
return false; |
272
|
45 |
|
case 'empty': |
273
|
3 |
|
return ''; |
274
|
42 |
|
case 'null': |
275
|
3 |
|
return; |
276
|
|
|
} |
277
|
|
|
|
278
|
39 |
|
if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) { |
279
|
9 |
|
return substr($value, 1, -1); |
280
|
|
|
} |
281
|
|
|
|
282
|
30 |
|
return $value; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (! function_exists('neon_secret')) { |
287
|
|
|
/** |
288
|
|
|
* Gets the value of NEON_SECRET creating it if missing |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
function neon_secret() { |
292
|
|
|
return Env::getNeonSecret(); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if (! function_exists('neon_encryption_key')) { |
297
|
|
|
/** |
298
|
|
|
* Gets the value of NEON_SECRET creating it if missing |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
function neon_encryption_key() { |
302
|
|
|
return Env::getNeonEncryptionKey(); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if (! function_exists('value')) { |
307
|
|
|
/** |
308
|
|
|
* Return the value of the given value. |
309
|
|
|
* If the $value is a Closure, it will first be executed then its result returned |
310
|
|
|
* |
311
|
|
|
* @param mixed $value |
312
|
|
|
* @return mixed |
313
|
|
|
*/ |
314
|
|
|
function value($value) |
315
|
|
|
{ |
316
|
192 |
|
return $value instanceof Closure ? $value() : $value; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if (! function_exists('collect')) { |
321
|
|
|
/** |
322
|
|
|
* Create a collection from the given value. |
323
|
|
|
* |
324
|
|
|
* @param mixed $value |
325
|
|
|
* @return \neon\core\helpers\Collection |
326
|
|
|
*/ |
327
|
|
|
function collect($value = null) |
328
|
|
|
{ |
329
|
33 |
|
return new Collection($value); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if (! function_exists('now')) { |
334
|
|
|
/** |
335
|
|
|
* Create a new Carbon instance for the current time. |
336
|
|
|
* |
337
|
|
|
* @param \DateTimeZone|string|null $tz |
338
|
|
|
* @return \Carbon\Carbon |
339
|
|
|
*/ |
340
|
|
|
function now($tz = null) |
341
|
|
|
{ |
342
|
|
|
return Carbon::now($tz); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if (! function_exists('profile_begin')) { |
347
|
|
|
/** |
348
|
|
|
* @param string $name |
349
|
|
|
*/ |
350
|
|
|
function profile_begin($name, $category='neon') |
351
|
|
|
{ |
352
|
141 |
|
\Neon::beginProfile($name, $category); |
353
|
141 |
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (! function_exists('profile_end')) { |
357
|
|
|
/** |
358
|
|
|
* @param string $name |
359
|
|
|
*/ |
360
|
|
|
function profile_end($name=null, $category='neon') |
361
|
|
|
{ |
362
|
141 |
|
\Neon::endProfile($name, $category); |
363
|
141 |
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if (! function_exists('encrypt')) { |
367
|
|
|
/** |
368
|
|
|
* @param string $data |
369
|
|
|
* @return string - encrypted data |
370
|
|
|
*/ |
371
|
|
|
function encrypt($data) |
372
|
|
|
{ |
373
|
|
|
return Str::base64UrlEncode(neon()->security->encryptByKey($data, Env::getNeonEncryptionKey())); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
if (! function_exists('decrypt')) { |
378
|
|
|
/** |
379
|
|
|
* @param string $data |
380
|
|
|
* @return string - encrypted data |
381
|
|
|
*/ |
382
|
|
|
function decrypt($data) |
383
|
|
|
{ |
384
|
|
|
return neon()->security->decryptByKey(Str::base64UrlDecode($data), Env::getNeonEncryptionKey()); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if (! function_exists('get_alias')) { |
389
|
|
|
/** |
390
|
|
|
* @param string $path |
391
|
|
|
* @return string - resolved path |
392
|
|
|
*/ |
393
|
|
|
function get_alias($path) |
394
|
|
|
{ |
395
|
|
|
return neon()->getAlias($path); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
if (! function_exists('on_url')) { |
400
|
|
|
/** |
401
|
|
|
* This function is useful for checking if links should be active, |
402
|
|
|
* |
403
|
|
|
* Note: can accept multiple urls - will return true if any one urls matches the current url |
404
|
|
|
* @example |
405
|
|
|
* ```php |
406
|
|
|
* // current url === /two |
407
|
|
|
* isUrl('/one', '/two'); |
408
|
|
|
* // => true |
409
|
|
|
* ``` |
410
|
|
|
* |
411
|
|
|
* @param string|array $urls,... accepts multiple parameters representing urls to check - will return true if any match |
412
|
|
|
* @return boolean - whether the request is on the current url |
413
|
|
|
*/ |
414
|
|
|
function on_url(...$urls) |
415
|
|
|
{ |
416
|
|
|
return \neon\core\helpers\Url::isUrl(...$urls); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
if (! function_exists('_t')) { |
421
|
|
|
/** |
422
|
|
|
* Translates a message to the target language |
423
|
|
|
* This function is a wrapper to the \Neon::t method |
424
|
|
|
* |
425
|
|
|
* You can add parameters to a translation message that will be substituted with the corresponding value after |
426
|
|
|
* translation. The format for this is to use curly brackets around the parameter name as you can see in the following example: |
427
|
|
|
* |
428
|
|
|
* ```php |
429
|
|
|
* $username = 'Neon'; |
430
|
|
|
* echo t('Hello, {username}!', ['username' => $username]); |
431
|
|
|
* ``` |
432
|
|
|
* Further formatting of message parameters is supported using the [PHP intl extensions](http://www.php.net/manual/en/intro.intl.php) |
433
|
|
|
* message formatter. See [[\yii\i18n\I18N::translate()]] for more details. |
434
|
|
|
* |
435
|
|
|
* @param string $text the message to be translated. |
436
|
|
|
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
437
|
|
|
* @param string $category if provided will save the text to that category. Defaults to 'neon' |
438
|
|
|
* @return string the translated message. |
439
|
|
|
*/ |
440
|
|
|
function _t($text, $params=[], $category='neon') |
441
|
|
|
{ |
442
|
|
|
return \Neon::t($category, $text, $params); |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
if (! function_exists('faker')) { |
446
|
|
|
/** |
447
|
|
|
* Generate a faker object |
448
|
|
|
* @return \Faker\Generator; |
449
|
|
|
*/ |
450
|
|
|
function faker() |
451
|
|
|
{ |
452
|
3 |
|
static $faker = null; |
453
|
3 |
|
if ($faker === null) |
454
|
3 |
|
$faker = \Faker\Factory::create(); |
455
|
3 |
|
return $faker; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
if (! function_exists('user_is')) { |
459
|
|
|
/** |
460
|
|
|
* @return boolean whether the user is/has a particular role |
461
|
|
|
*/ |
462
|
|
|
function user_is($role) |
463
|
|
|
{ |
464
|
|
|
return neon()->user->is($role); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
if (! function_exists('user_can')) { |
469
|
|
|
/** |
470
|
|
|
* Whether the user can perform a permission |
471
|
|
|
* @param $permission |
472
|
|
|
* @return bool |
473
|
|
|
*/ |
474
|
|
|
function user_can($permission) |
475
|
|
|
{ |
476
|
|
|
return neon()->user->can($permission); |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
if (! function_exists('is_route')) { |
481
|
|
|
/** |
482
|
|
|
* Test whether the current application is on a particular route |
483
|
|
|
* This is url rewrite safe! However you must reference the module/controller/action and not the url directly |
484
|
|
|
* - sometimes these are very similar but they can all be overriden |
485
|
|
|
* For e.g. cms routes are on_route('/cms/render/page', ['nice_id'=>'home']) |
486
|
|
|
* @param string $pattern |
487
|
|
|
* @param array $params |
488
|
|
|
*/ |
489
|
|
|
function is_route($pattern, $params=[]) |
490
|
|
|
{ |
491
|
|
|
return neon()->request->isRoute($pattern, $params); |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|