Passed
Push — master ( e1e679...09e224 )
by Quentin
10:29
created

twillAsset()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 11.9895

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 5
nop 1
dl 0
loc 36
ccs 9
cts 20
cp 0.45
crap 11.9895
rs 8.9297
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Facades\Cache;
4
5
if (!function_exists('revAsset')) {
6
    /**
7
     * @param string $file
8
     * @return string
9
     */
10
    function revAsset($file)
11
    {
12
        if (!app()->environment('local', 'development')) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

12
        if (!app()->/** @scrutinizer ignore-call */ environment('local', 'development')) {
Loading history...
13
            try {
14
                $manifest = Cache::rememberForever('rev-manifest', function () {
15
                    return json_decode(file_get_contents(config('twill.frontend.rev_manifest_path')), true);
16
                });
17
18
                if (isset($manifest[$file])) {
19
                    return (rtrim(config('twill.frontend.dist_assets_path'), '/') . '/') . $manifest[$file];
20
                }
21
22
            } catch (\Exception $e) {
23
                return '/' . $file;
24
            }
25
        }
26
27
        return (rtrim(config('twill.frontend.dev_assets_path'), '/') . '/') . $file;
28
    }
29
}
30
31
if (!function_exists('twillAsset')) {
32
    /**
33
     * @param string $file
34
     * @return string
35
     */
36
    function twillAsset($file)
37
    {
38 49
        if (app()->environment('local', 'development') && config('twill.dev_mode', false)) {
39
            $devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');
40
41
            try {
42
                $manifest = json_decode(file_get_contents(
43
                    $devServerUrl
44
                    . '/'
45
                    . config('twill.manifest_file', 'twill-manifest.json')
46
                ), true);
47
48
            } catch (\Exception $e) {
49
                throw new \Exception('Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.');
50
            }
51
52
            return $devServerUrl . ($manifest[$file] ?? ('/' . $file));
53
        }
54
55
        try {
56
            $manifest = Cache::rememberForever('twill-manifest', function () {
57 49
                return json_decode(file_get_contents(
58 49
                    public_path(config('twill.public_directory', 'twill'))
59 49
                    . '/'
60 49
                    . config('twill.manifest_file', 'twill-manifest.json')
61 49
                ), true);
62 49
            });
63
        } catch (\Exception $e) {
64
            throw new \Exception('Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.');
65
        }
66
67 49
        if (isset($manifest[$file])) {
68 49
            return $manifest[$file];
69
        }
70
71
        return '/' . config('twill.public_directory', 'twill') . '/' . $file;
72
    }
73
}
74
75
if (!function_exists('icon')) {
76
    /**
77
     * ARIA roles memo: 'presentation' means merely decoration. Otherwise, use role="img".
78
     *
79
     * @param string $name
80
     * @param array $opts
81
     * @return string
82
     */
83
    function icon($name, $opts = [])
84
    {
85
        $title = isset($opts['title']) ? ' title="' . htmlentities($opts['title'], ENT_QUOTES, 'UTF-8') . '" ' : '';
86
        $role = isset($opts['role']) ? ' role="' . htmlentities($opts['role'], ENT_QUOTES, 'UTF-8') . '" ' : ' role="presentation" ';
87
        $css_class = isset($opts['css_class']) ? htmlentities($opts['css_class'], ENT_QUOTES, 'UTF-8') : '';
88
        $svg_link = config('twill.frontend.svg_sprites_use_hash_only') ? "#icon--$name" : revAsset(config('twill.frontend.svg_sprites_path')) . "#icon--$name";
89
        return "<svg class=\"icon--$name $css_class\" $title $role><use xlink:href=\"" . $svg_link . "\"></use></svg>";
90
    }
91
}
92