Passed
Pull Request — master (#87)
by
unknown
03:43
created

installed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
use Quantum\Libraries\Transformer\TransformerInterface;
16
use Quantum\Libraries\Transformer\TransformerManager;
17
use Quantum\Exceptions\StopExecutionException;
18
use Quantum\Libraries\Asset\AssetManager;
19
use Quantum\Exceptions\AppException;
20
use Quantum\Libraries\Csrf\Csrf;
21
use Quantum\Libraries\Storage\FileSystem;
22
23
/**
24
 * Generates the CSRF token
25
 * @return string|null
26
 * @throws \Quantum\Exceptions\AppException
27
 * @throws \Quantum\Exceptions\DatabaseException
28
 * @throws \Quantum\Exceptions\SessionException
29
 */
30
function csrf_token(): ?string
31
{
32
    $appKey = env('APP_KEY');
33
34
    if (!$appKey) {
35
        throw AppException::missingAppKey();
36
    }
37
38
    return Csrf::generateToken(session(), $appKey);
39
}
40
41
/**
42
 * _message
43
 * @param string $subject
44
 * @param string|array $params
45
 * @return string
46
 */
47
function _message(string $subject, $params): string
48
{
49
    if (is_array($params)) {
50
        return preg_replace_callback('/{%\d+}/', function () use (&$params) {
51
            return array_shift($params);
52
        }, $subject);
53
    } else {
54
        return preg_replace('/{%\d+}/', $params, $subject);
55
    }
56
}
57
58
/**
59
 * Validates base64 string
60
 * @param string $string
61
 * @return boolean
62
 */
63
function valid_base64(string $string): bool
64
{
65
    $decoded = base64_decode($string, true);
66
67
    // Check if there is no invalid character in string
68
    if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) {
69
        return false;
70
    }
71
72
    // Decode the string in strict mode and send the response
73
    if (!base64_decode($string, true)) {
74
        return false;
75
    }
76
77
    // Encode and compare it to original one
78
    if (base64_encode($decoded) != $string) {
79
        return false;
80
    }
81
82
    return true;
83
}
84
85
/**
86
 * Gets directory classes
87
 * @param string $path
88
 * @return array
89
 */
90
function get_directory_classes(string $path): array
91
{
92
    $class_names = [];
93
94
    if (is_dir($path)) {
95
        $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
96
        $phpFiles = new RegexIterator($allFiles, '/\.php$/');
97
98
        foreach ($phpFiles as $file) {
99
            $class = pathinfo($file->getFilename());
100
            array_push($class_names, $class['filename']);
101
        }
102
    }
103
104
    return $class_names;
105
}
106
107
/**
108
 * Gets the caller class
109
 * @param integer $index
110
 * @return string|null
111
 */
112
function get_caller_class(int $index = 2): ?string
113
{
114
    $caller = debug_backtrace();
115
    $caller = $caller[$index];
116
117
    return $caller['class'] ?? null;
118
}
119
120
/**
121
 * Gets the caller function
122
 * @param integer $index
123
 * @return string|null
124
 */
125
function get_caller_function(int $index = 2): ?string
126
{
127
    $caller = debug_backtrace();
128
    $caller = $caller[$index];
129
130
    return $caller['function'] ?? null;
131
}
132
133
/**
134
 *
135
 * @throws \Quantum\Exceptions\StopExecutionException
136
 */
137
138
/**
139
 * Stops the execution
140
 * @param \Closure|null $closure
141
 * @throws \Quantum\Exceptions\StopExecutionException
142
 */
143
function stop(Closure $closure = null)
144
{
145
    if ($closure) {
146
        $closure();
147
    }
148
149
    throw StopExecutionException::executionTerminated();
150
}
151
152
/**
153
 * Generates random number sequence
154
 * @param int $length
155
 * @return int
156
 */
157
function random_number(int $length = 10): int
158
{
159
    $randomString = '';
160
    for ($i = 0; $i < $length; $i++) {
161
        $randomString .= rand(0, 9);
162
    }
163
    return (int) $randomString;
164
}
165
166
/**
167
 * Slugify the string
168
 * @param string $text
169
 * @return string
170
 */
171
function slugify(string $text): string
172
{
173
    $text = trim($text, ' ');
174
    $text = preg_replace('/[^\p{L}\p{N}]/u', ' ', $text);
175
    $text = preg_replace('/\s+/', '-', $text);
176
    $text = trim($text, '-');
177
    $text = mb_strtolower($text);
178
179
    if (empty($text)) {
180
        return 'n-a';
181
    }
182
    return $text;
183
}
184
185
/**
186
 * Dumps the assets
187
 * @param string $type
188
 * @throws \Quantum\Exceptions\AssetException
189
 */
190
function assets(string $type)
191
{
192
    $assetTypes = [
193
        'css' => 1,
194
        'js' => 2
195
    ];
196
197
    AssetManager::getInstance()->dump($assetTypes[$type]);
198
}
199
200
/**
201
 * Checks if the entity is closure
202
 * @param mixed $entity
203
 * @return bool
204
 */
205
function is_closure($entity): bool
206
{
207
    return $entity instanceof \Closure;
208
}
209
210
/**
211
 * Transforms the data by given transformer signature
212
 * @param array $data
213
 * @param TransformerInterface $transformer
214
 * @return array
215
 */
216
function transform(array $data, TransformerInterface $transformer): array
217
{
218
    return TransformerManager::transform($data, $transformer);
219
}
220
221
/**
222
 * Checks if already installed
223
 * @return bool
224
 */
225
function installed(string $path): bool
226
{
227
    $fs = new FileSystem();
228
229
    if ($fs->exists($path)) {
230
        return true;
231
    }
232
233
    return false;
234
}
235