1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppUtils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Error code for the CURL extension check. |
7
|
|
|
* |
8
|
|
|
* @var int |
9
|
|
|
* @see \AppUtils\requireCURL() |
10
|
|
|
*/ |
11
|
|
|
define('APPUTILS_ERROR_CURL_NOT_INSTALLED', 44001); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Parses the specified number, and returns a NumberInfo instance. |
15
|
|
|
* |
16
|
|
|
* @param mixed $value |
17
|
|
|
* @return \AppUtils\NumberInfo |
18
|
|
|
*/ |
19
|
|
|
function parseNumber($value, $forceNew=false) |
20
|
|
|
{ |
21
|
|
|
if($value instanceof NumberInfo && $forceNew !== true) { |
22
|
|
|
return $value; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return new NumberInfo($value); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Parses the specified variable, and allows accessing |
30
|
|
|
* information on it. |
31
|
|
|
* |
32
|
|
|
* @param mixed $variable |
33
|
|
|
* @return \AppUtils\VariableInfo |
34
|
|
|
*/ |
35
|
|
|
function parseVariable($variable) |
36
|
|
|
{ |
37
|
|
|
return new VariableInfo($variable); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Like the native PHP function <code>parse_url</code>, |
42
|
|
|
* but with a friendly API and some enhancements and fixes |
43
|
|
|
* for a few things that the native function handles poorly. |
44
|
|
|
* |
45
|
|
|
* @param string $url The URL to parse. |
46
|
|
|
* @return \AppUtils\URLInfo |
47
|
|
|
*/ |
48
|
|
|
function parseURL(string $url) : URLInfo |
49
|
|
|
{ |
50
|
|
|
return new URLInfo($url); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Creates a throwable info instance for the specified error, |
55
|
|
|
* which enables accessing additional information on it, |
56
|
|
|
* as well as serializing it to be able to persist it in storage. |
57
|
|
|
* |
58
|
|
|
* @param \Throwable $e |
59
|
|
|
* @return ConvertHelper_ThrowableInfo |
60
|
|
|
*/ |
61
|
|
|
function parseThrowable(\Throwable $e) : ConvertHelper_ThrowableInfo |
62
|
|
|
{ |
63
|
|
|
return ConvertHelper_ThrowableInfo::fromThrowable($e); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Restores a throwable info instance from a previously |
68
|
|
|
* serialized array. |
69
|
|
|
* |
70
|
|
|
* @param array $serialized |
71
|
|
|
* @return ConvertHelper_ThrowableInfo |
72
|
|
|
*/ |
73
|
|
|
function restoreThrowable(array $serialized) : ConvertHelper_ThrowableInfo |
74
|
|
|
{ |
75
|
|
|
return ConvertHelper_ThrowableInfo::fromSerialized($serialized); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Creates an interval wrapper, that makes it a lot easier |
80
|
|
|
* to work with date intervals. It also solves |
81
|
|
|
* |
82
|
|
|
* @param \DateInterval $interval |
83
|
|
|
* @return ConvertHelper_DateInterval |
84
|
|
|
*/ |
85
|
|
|
function parseInterval(\DateInterval $interval) : ConvertHelper_DateInterval |
86
|
|
|
{ |
87
|
|
|
return ConvertHelper_DateInterval::fromInterval($interval); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Translation function used to translate some of the internal |
92
|
|
|
* strings: if the localization is installed, it will use this |
93
|
|
|
* to do the translation. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
function t() |
98
|
|
|
{ |
99
|
|
|
$args = func_get_args(); |
100
|
|
|
|
101
|
|
|
// is the localization package installed? |
102
|
|
|
if(class_exists('\AppLocalize\Localization')) |
103
|
|
|
{ |
104
|
|
|
return call_user_func_array('\AppLocalize\t', $args); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// simulate the translation function |
108
|
|
|
return call_user_func_array('sprintf', $args); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Ensures that the CURL extension is available, and throws |
113
|
|
|
* an exception if not. |
114
|
|
|
* |
115
|
|
|
* @throws BaseException |
116
|
|
|
* @link \AppUtils\APPUTILS_ERROR_CURL_NOT_INSTALLED |
117
|
|
|
*/ |
118
|
|
|
function requireCURL() : void |
119
|
|
|
{ |
120
|
|
|
if(function_exists('curl_init')) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
throw new BaseException( |
125
|
|
|
'The CURL extension is not installed or not available.', |
126
|
|
|
null, |
127
|
|
|
APPUTILS_ERROR_CURL_NOT_INSTALLED |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Initializes the utilities: this is called automatically |
133
|
|
|
* because this file is included in the files list in the |
134
|
|
|
* composer.json, guaranteeing it is always loaded. |
135
|
|
|
*/ |
136
|
|
|
function init() |
137
|
|
|
{ |
138
|
|
|
if(!class_exists('\AppLocalize\Localization')) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$installFolder = realpath(__DIR__.'/../'); |
143
|
|
|
|
144
|
|
|
// Register the classes as a localization source, |
145
|
|
|
// so they can be found, and use the bundled localization |
146
|
|
|
// files. |
147
|
|
|
\AppLocalize\Localization::addSourceFolder( |
|
|
|
|
148
|
|
|
'application-utils', |
149
|
|
|
'Application Utils Package', |
150
|
|
|
'Composer Packages', |
151
|
|
|
$installFolder.'/localization', |
152
|
|
|
$installFolder.'/src' |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
init(); |
157
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths