Passed
Push — master ( 684bcb...2808dd )
by Sebastian
09:28
created

isCLI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AppUtils;
4
5
use DateInterval;
6
use Throwable;
7
8
/**
9
 * Parses the specified number, and returns a NumberInfo instance.
10
 *
11
 * @param NumberInfo|string|int|float $value
12
 * @param bool $forceNew
13
 * @return NumberInfo
14
 */
15
function parseNumber($value, bool $forceNew=false)
16
{
17
    if($value instanceof NumberInfo && $forceNew !== true) {
18
        return $value;
19
    }
20
    
21
    return new NumberInfo($value);
22
}
23
24
/**
25
 * Parses the specified variable, and allows accessing
26
 * information on it.
27
 * 
28
 * @param mixed $variable
29
 * @return VariableInfo
30
 */
31
function parseVariable($variable) : VariableInfo
32
{
33
    return new VariableInfo($variable);
34
}
35
36
/**
37
 * Like the native PHP function <code>parse_url</code>,
38
 * but with a friendly API and some enhancements and fixes 
39
 * for a few things that the native function handles poorly.
40
 * 
41
 * @param string $url The URL to parse.
42
 * @return URLInfo
43
 */
44
function parseURL(string $url) : URLInfo
45
{
46
    return new URLInfo($url);
47
}
48
49
/**
50
 * Creates a throwable info instance for the specified error,
51
 * which enables accessing additional information on it,
52
 * as well as serializing it to be able to persist it in storage.
53
 * 
54
 * @param Throwable $e
55
 * @return ConvertHelper_ThrowableInfo
56
 */
57
function parseThrowable(Throwable $e) : ConvertHelper_ThrowableInfo
58
{
59
    return ConvertHelper_ThrowableInfo::fromThrowable($e);
60
}
61
62
/**
63
 * Restores a throwable info instance from a previously 
64
 * serialized array.
65
 * 
66
 * @param array $serialized
67
 * @return ConvertHelper_ThrowableInfo
68
 */
69
function restoreThrowable(array $serialized) : ConvertHelper_ThrowableInfo
70
{
71
    return ConvertHelper_ThrowableInfo::fromSerialized($serialized);
72
}
73
74
/**
75
 * Creates an interval wrapper, that makes it a lot easier
76
 * to work with date intervals. It also solves
77
 *
78
 * @param DateInterval $interval
79
 * @return ConvertHelper_DateInterval
80
 */
81
function parseInterval(DateInterval $interval) : ConvertHelper_DateInterval
82
{
83
    return ConvertHelper_DateInterval::fromInterval($interval);
84
}
85
86
/**
87
 * Translation function used to translate some of the internal
88
 * strings: if the localization is installed, it will use this
89
 * to do the translation.
90
 * 
91
 * @return string
92
 */
93
function t() : string
94
{
95
    $args = func_get_args();
96
    
97
    // is the localization package installed?
98
    if(function_exists('\AppLocalize\t'))
99
    {
100
        return call_user_func_array('\AppLocalize\t', $args);
101
    }
102
    
103
    // simulate the translation function
104
    return strval(call_user_func_array('sprintf', $args));
105
}
106
107
/**
108
 * Creates a boolean value.
109
 * 
110
 * @param bool $initial The initial boolean value to use.
111
 * @return Value_Bool
112
 */
113
function valBool(bool $initial=false) : Value_Bool
114
{
115
    return new Value_Bool($initial);
116
}
117
118
/**
119
 * Creates a sticky true-based boolean value: starts out
120
 * as false, and if it is set to true, cannot be set to
121
 * false again afterwards.
122
 * 
123
 * @param bool $initial
124
 * @return Value_Bool_True
125
 */
126
function valBoolTrue(bool $initial=false) : Value_Bool_True
127
{
128
    return new Value_Bool_True($initial);
129
}
130
131
/**
132
 * Creates a sticky false-based boolean value: starts out
133
 * as true, and if it is set to false, cannot be set to
134
 * true again afterwards.
135
 * 
136
 * @param bool $initial
137
 * @return Value_Bool_False
138
 */
139
function valBoolFalse(bool $initial=true) : Value_Bool_False
140
{
141
    return new Value_Bool_False($initial);
142
}
143
144
/**
145
 * Creates a new StringBuilder instance.
146
 * 
147
 * @return StringBuilder
148
 */
149
function sb() : StringBuilder
150
{
151
    return new StringBuilder();
152
}
153
154
/**
155
 * Whether the current request is run via the command line.
156
 * @return bool
157
 */
158
function isCLI() : bool
159
{
160
    return php_sapi_name() === "cli";
161
}
162
163
/**
164
 * Initializes the utilities: this is called automatically
165
 * because this file is included in the files list in the
166
 * composer.json, guaranteeing it is always loaded.
167
 */
168
function init()
169
{
170
    if(!class_exists('\AppLocalize\Localization')) {
171
        return;
172
    }
173
    
174
    $installFolder = realpath(__DIR__.'/../');
175
    
176
    // Register the classes as a localization source,
177
    // so they can be found, and use the bundled localization
178
    // files.
179
    \AppLocalize\Localization::addSourceFolder(
180
        'application-utils',
181
        'Application Utils Package',
182
        'Composer Packages',
183
        $installFolder.'/localization',
184
        $installFolder.'/src'
185
    );
186
}
187
188
init();
189