Issues (536)

src/Helpers/assets.php (17 issues)

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
if (! function_exists('css_url')) {
13
    /**
14
     * CSS URL
15
     *
16
     * Renvoie l'url d'un fichier css.
17
     *
18
     * @param string $name nom du fichier dont on veut avoir l'url
19
     */
20
    function css_url(string $name): string
21
    {
22
        $name = explode('?', $name)[0];
23
        $name = str_replace(site_url() . 'css/', '', htmlspecialchars($name));
24
25
        if (is_localfile($name)) {
26
            $name .= (! preg_match('#\.css$#i', $name) ? '.css' : '');
27
            $filename = WEBROOT . 'css' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
28
29
            return site_url() . 'css/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
30
        }
31
32
        return $name . (! preg_match('#\.css$#i', $name) ? '.css' : '');
33
    }
34
}
35
36
if (! function_exists('js_url')) {
37
    /**
38
     * JS URL
39
     *
40
     * Renvoie l'url d'un fichier js.
41
     *
42
     * @param string $name nom du fichier dont on veut avoir l'url
43
     */
44
    function js_url(string $name): string
45
    {
46
        $name = explode('?', $name)[0];
47
        $name = str_replace(site_url() . 'js/', '', htmlspecialchars($name));
48
49
        if (is_localfile($name)) {
50
            $name .= (! preg_match('#\.js$#i', $name) ? '.js' : '');
51
            $filename = WEBROOT . 'js' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52
53
            return site_url() . 'js/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
54
        }
55
56
        return $name . (! preg_match('#\.js$#i', $name) ? '.js' : '');
57
    }
58
}
59
60
if (! function_exists('lib_css_url')) {
61
    /**
62
     * LIB CSS URL
63
     *
64
     * Renvoie l'url d'un fichier css d'une librairie
65
     *
66
     * @param string $name nom du fichier dont on veut avoir l'url
67
     */
68
    function lib_css_url(string $name): string
69
    {
70
        $name     = explode('?', $name)[0];
71
        $site_url = site_url();
72
        $name     = str_replace(
73
            [$site_url . 'lib/', $site_url . 'vendor/', $site_url . 'plugins/'],
74
            '',
75
            htmlspecialchars($name)
76
        );
77
78
        if (is_localfile($name)) {
79
            $name .= (! preg_match('#\.css$#i', $name) ? '.css' : '');
80
            $paths = ['lib', 'vendor', 'plugins'];
81
82
            foreach ($paths as $path) {
83
                $filename = WEBROOT . $path . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
84
85
                if (file_exists($filename)) {
86
                    return $site_url . $path . '/' . $name . '?v=' . filemtime($filename);
87
                }
88
            }
89
90
            return $site_url . 'lib/' . $name;
91
        }
92
93
        return $name . (! preg_match('#\.css$#i', $name) ? '.css' : '');
94
    }
95
}
96
97
if (! function_exists('lib_js_url')) {
98
    /**
99
     * LIB JS URL
100
     *
101
     * Renvoie l'url d'un fichier js d'une librairy.
102
     *
103
     * @param string $name nom du fichier dont on veut avoir l'url
104
     */
105
    function lib_js_url(string $name): string
106
    {
107
        $name     = explode('?', $name)[0];
108
        $site_url = site_url();
109
        $name     = str_replace(
110
            [$site_url . 'lib/', $site_url . 'vendor/', $site_url . 'plugins/'],
111
            '',
112
            htmlspecialchars($name)
113
        );
114
115
        if (is_localfile($name)) {
116
            $name .= (! preg_match('#\.js$#i', $name) ? '.js' : '');
117
            $paths = ['lib', 'vendor', 'plugins'];
118
119
            foreach ($paths as $path) {
120
                $filename = WEBROOT . $path . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
122
                if (file_exists($filename)) {
123
                    return $site_url . $path . '/' . $name . '?v=' . filemtime($filename);
124
                }
125
            }
126
127
            return $site_url . 'lib/' . $name;
128
        }
129
130
        return $name . (! preg_match('#\.js$#i', $name) ? '.js' : '');
131
    }
132
}
133
134
if (! function_exists('lib_styles')) {
135
    /**
136
     * LIB_STYLES
137
     *
138
     * inclu une ou plusieurs feuilles de style css
139
     *
140
     * @param list<string>|string $name  nom du fichier dont on veut inserer
0 ignored issues
show
The type list was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
141
     * @param bool                $print Specifie si on affiche directement la sortie ou si on la retourne
142
     *
143
     * @return string|void
144
     */
145
    function lib_styles($name, bool $print = true)
146
    {
147
        $name   = (array) $name;
148
        $return = [];
149
150
        foreach ($name as $style) {
151
            if (is_string($style)) {
152
                $style = (! preg_match('#\.css$#i', $style) ? $style . '.css' : $style);
153
                if (is_file(WEBROOT . 'lib' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
154
                    $return[] = '<link rel="preload" type="text/css" href="' . lib_css_url($style) . '" as="style">
155
						<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" />';
156
                } elseif (is_localfile($style)) {
157
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
158
                    $return[] = '<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" /> -->';
159
                } else {
160
                    $return[] = '<link rel="preload" type="text/css" href="' . lib_css_url($style) . '" as="style">
161
						<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" />';
162
                }
163
            }
164
        }
165
166
        $output = implode("\n", $return);
167
168
        if (false === $print) {
169
            return $output;
170
        }
171
172
        echo $output;
173
    }
174
}
175
176
if (! function_exists('lib_scripts')) {
177
    /**
178
     * LIB_SCRIPTS
179
     *
180
     * inclu un ou plusieurs scripts js
181
     *
182
     * @param list<string>|string $name  nom du fichier dont on veut inserer
183
     * @param bool                $print Specifie si on affiche directement la sortie ou si on la retourne
184
     *
185
     * @return string|void
186
     */
187
    function lib_scripts($name, bool $print = true)
188
    {
189
        $name   = (array) $name;
190
        $return = [];
191
192
        foreach ($name as $script) {
193
            if (is_string($script)) {
194
                $script = (! preg_match('#\.js$#i', $script) ? $script . '.js' : $script);
195
                if (is_file(WEBROOT . 'lib' . DS . str_replace('/', DS, $script))) {
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
196
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script>';
197
                } elseif (is_localfile($script)) {
198
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
199
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script> -->';
200
                } else {
201
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script>';
202
                }
203
            }
204
        }
205
206
        $output = implode("\n", $return);
207
208
        if (false === $print) {
209
            return $output;
210
        }
211
212
        echo $output;
213
    }
214
}
215
216
if (! function_exists('styles')) {
217
    /**
218
     * STYLES
219
     *
220
     * inclu une ou plusieurs feuilles de style css
221
     *
222
     * @param list<string>|string $name  nom du fichier dont on veut inserer
223
     * @param bool                $print Specifie si on affiche directement la sortie ou si on la retourne
224
     *
225
     * @return string|void
226
     */
227
    function styles($name, bool $print = true)
228
    {
229
        $name   = (array) $name;
230
        $return = [];
231
232
        foreach ($name as $style) {
233
            if (is_string($style)) {
234
                $style = (! preg_match('#\.css$#i', $style) ? $style . '.css' : $style);
235
                if (is_file(WEBROOT . 'css' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
236
                    $return[] = '<link rel="preload" type="text/css" href="' . css_url($style) . '" as="style">
237
						<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" />';
238
                } elseif (is_localfile($style)) {
239
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
240
                    $return[] = '<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" /> -->';
241
                } else {
242
                    $return[] = '<link rel="preload" type="text/css" href="' . css_url($style) . '" as="style">
243
						<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" />';
244
                }
245
            }
246
        }
247
248
        $output = implode("\n", $return);
249
250
        if (false === $print) {
251
            return $output;
252
        }
253
254
        echo $output;
255
    }
256
}
257
258
if (! function_exists('scripts')) {
259
    /**
260
     * SCRIPTS
261
     *
262
     * inclu un ou plusieurs scripts js
263
     *
264
     * @param list<string>|string $name  nom du fichier dont on veut inserer
265
     * @param bool                $print Specifie si on affiche directement la sortie ou si on la retourne
266
     *
267
     * @return string|void
268
     */
269
    function scripts($name, bool $print = true)
270
    {
271
        $name   = (array) $name;
272
        $return = [];
273
274
        foreach ($name as $script) {
275
            if (is_string($script)) {
276
                $script = (! preg_match('#\.js$#i', $script) ? $script . '.js' : $script);
277
                if (is_file(WEBROOT . 'js' . DS . str_replace('/', DS, $script))) {
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
278
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script>';
279
                } elseif (is_localfile($script)) {
280
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
281
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script> -->';
282
                } else {
283
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script>';
284
                }
285
            }
286
        }
287
288
        $output = implode("\n", $return);
289
290
        if (false === $print) {
291
            return $output;
292
        }
293
294
        echo $output;
295
    }
296
}
297
298
if (! function_exists('less_url')) {
299
    /**
300
     * LESS URL
301
     *
302
     * Renvoie l'url d'un fichier less.
303
     *
304
     * @param string $name nom du fichier dont on veut avoir l'url
305
     */
306
    function less_url(string $name): string
307
    {
308
        $name = explode('?', $name)[0];
309
        $name = str_replace(site_url() . 'less/', '', htmlspecialchars($name));
310
311
        if (is_localfile($name)) {
312
            $name .= (! preg_match('#\.less$#i', $name) ? '.less' : '');
313
            $filename = WEBROOT . 'less' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
314
315
            return site_url() . 'less/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
316
        }
317
318
        return $name . (! preg_match('#\.less$#i', $name) ? '.less' : '');
319
    }
320
}
321
322
if (! function_exists('less_styles')) {
323
    /**
324
     * LESS_STYLES
325
     *
326
     * inclu une ou plusieurs feuilles de style less
327
     *
328
     * @param list<string>|string $name  nom du fichier dont on veut inserer
329
     * @param bool                $print Specifie si on affiche directement la sortie ou si on la retourne
330
     *
331
     * @return string|void
332
     */
333
    function less_styles($name, bool $print = true)
334
    {
335
        $name   = (array) $name;
336
        $return = [];
337
338
        foreach ($name as $style) {
339
            if (is_string($style)) {
340
                $style = (! preg_match('#\.less$#i', $style) ? $style . '.less' : $style);
341
                if (is_file(WEBROOT . 'less' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
342
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" />';
343
                } elseif (is_localfile($style)) {
344
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
345
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" /> -->';
346
                } else {
347
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" />';
348
                }
349
            }
350
        }
351
352
        $output = implode("\n", $return);
353
354
        if (false === $print) {
355
            return $output;
356
        }
357
358
        echo $output;
359
    }
360
}
361
362
if (! function_exists('img_url')) {
363
    /**
364
     * IMG URL
365
     *
366
     * Renvoie l'url d'une image
367
     *
368
     * @param string $name nom du fichier dont on veut avoir l'url
369
     */
370
    function img_url(?string $name, bool $add_version = true): string
371
    {
372
        if ($name === null || $name === '' || $name === '0') {
373
            return '';
374
        }
375
376
        $name = explode('?', $name)[0];
377
        $name = str_replace(site_url() . 'img/', '', htmlspecialchars($name));
378
379
        if (is_localfile($name)) {
380
            $filename = WEBROOT . 'img' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
381
382
            return site_url() . 'img/' . $name . ((file_exists($filename) && $add_version) ? '?v=' . filemtime($filename) : '');
383
        }
384
385
        return $name;
386
    }
387
}
388
389
if (! function_exists('img')) {
390
    /**
391
     * IMG
392
     *
393
     * Cree une image
394
     *
395
     * @param string $name nom du fichier dont on veut inserer
396
     * @param string $alt  texte alternatif
397
     *
398
     * @return string|void
399
     */
400
    function img(string $name, string $alt = '', array $options = [])
401
    {
402
        $return = '<img src="' . img_url($name) . '" alt="' . $alt . '"';
403
404
        $noprint = isset($options['print']) && $options['print'] === false;
405
        unset($options['print']);
406
407
        foreach ($options as $key => $value) {
408
            $return .= ' ' . $key . '="' . $value . '"';
409
        }
410
        $return .= ' />';
411
412
        if ($noprint === true) {
413
            return $return;
414
        }
415
416
        echo $return;
417
    }
418
}
419
420
if (! function_exists('docs_url')) {
421
    /**
422
     * DOCS URL
423
     *
424
     * Renvoie l'url d'un document
425
     *
426
     * @param string $name nom du fichier dont on veut avoir l'url
427
     */
428
    function docs_url(?string $name, bool $add_version = true): string
429
    {
430
        if ($name === null || $name === '' || $name === '0') {
431
            return '';
432
        }
433
434
        $name = explode('?', $name)[0];
435
        $name = str_replace(site_url() . 'docs/', '', htmlspecialchars($name));
436
437
        if (is_localfile($name)) {
438
            $filename = WEBROOT . 'docs' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
439
440
            return site_url() . 'docs/' . $name . ((file_exists($filename && $add_version)) ? '?v=' . filemtime($filename) : '');
0 ignored issues
show
$filename && $add_version of type boolean is incompatible with the type string expected by parameter $filename of file_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

440
            return site_url() . 'docs/' . $name . ((file_exists(/** @scrutinizer ignore-type */ $filename && $add_version)) ? '?v=' . filemtime($filename) : '');
Loading history...
441
        }
442
443
        return $name;
444
    }
445
}
446
447
if (! function_exists('videos_url')) {
448
    /**
449
     * VIDEOS URL
450
     *
451
     * Renvoie l'url d'une vidéo
452
     *
453
     * @param string $name nom du fichier dont on veut avoir l'url
454
     */
455
    function videos_url(?string $name, bool $add_version = true): string
456
    {
457
        if ($name === null || $name === '' || $name === '0') {
458
            return '';
459
        }
460
461
        $name = explode('?', $name)[0];
462
        $name = str_replace(site_url() . 'videos/', '', htmlspecialchars($name));
463
464
        if (is_localfile($name)) {
465
            $filename = WEBROOT . 'videos' . DS . $name;
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
466
467
            return site_url() . 'videos/' . $name . ((file_exists($filename) && $add_version) ? '?v=' . filemtime($filename) : '');
468
        }
469
470
        return $name;
471
    }
472
}
473
474
if (! function_exists('mix')) {
475
    /**
476
     * Obtenez le chemin d'accès à un fichier Mix versionné.
477
     *
478
     * @throws Exception
479
     */
480
    function mix(string $path, string $manifestDirectory = ''): string
481
    {
482
        static $manifests = [];
483
484
        $publicPath = trim(WEBROOT, '/\\');
0 ignored issues
show
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
485
486
        if ($path[0] !== '/') {
487
            $path = "/{$path}";
488
        }
489
490
        if ($manifestDirectory && $manifestDirectory[0] !== '/') {
491
            $manifestDirectory = "/{$manifestDirectory}";
492
        }
493
494
        $config = (object) config('mix');
495
496
        if (is_file($publicPath . $manifestDirectory . '/hot')) {
497
            $url = rtrim(file_get_contents($publicPath . $manifestDirectory . '/hot'));
498
499
            if (! empty($customUrl = $customUrl = $config->hot_proxy_url)) {
0 ignored issues
show
The assignment to $customUrl is dead and can be removed.
Loading history...
500
                return $customUrl . $path;
501
            }
502
503
            if (str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) {
504
                return explode(':', $url, 2)[1] . $path;
505
            }
506
507
            return "//localhost:3300{$path}";
508
        }
509
510
        $manifestPath = $publicPath . $manifestDirectory . '/mix-manifest.json';
511
512
        if (! isset($manifests[$manifestPath])) {
513
            if (! is_file($manifestPath)) {
514
                throw new Exception('Le manifeste Mix n\'existe pas.');
515
            }
516
517
            $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
518
        }
519
520
        $manifest = $manifests[$manifestPath];
521
522
        if (! isset($manifest[$path])) {
523
            $exception = new Exception("Impossible de localiser le fichier Mix: {$path}.");
524
525
            if (! BLITZ_DEBUG) {
526
                return $path;
527
            }
528
529
            throw $exception;
530
        }
531
532
        return $config->url . $manifestDirectory . $manifest[$path];
533
    }
534
}
535