Passed
Pull Request — 2.x (#821)
by Antonio Carlos
06:55
created

Twill::devAsset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.8666
1
<?php
2
3
namespace A17\Twill\Services\Assets;
4
5
use Illuminate\Support\Facades\Cache;
6
7
class Twill
8
{
9
    function asset($file)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
    {
11
        return $this->devAsset($file) ?? $this->twillAsset($file);
12
    }
13
14
    public function twillAsset($file)
15
    {
16
        $manifest = $this->readManifest();
17
18
        if (isset($manifest[$file])) {
19
            return asset($manifest[$file]);
20
        }
21
22
        return asset(
23
            '/' . config('twill.public_directory', 'twill') . '/' . $file,
24
        );
25
    }
26
27
    public function getManifestFilename()
28
    {
29
        $fileName =
30
            public_path(config('twill.public_directory', 'twill')) .
31
            '/' .
32
            config('twill.manifest_file', 'twill-manifest.json');
33
34
        if (file_exists($fileName)) {
35
            return $fileName;
36
        }
37
38
        return base_path(
39
            'vendor/area17/twill/dist/assets/admin/twill-manifest.json',
40
        );
41
    }
42
43
    public function devAsset($file)
44
    {
45
        if (!$this->devMode()) {
46
            return null;
47
        }
48
49
        $devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');
50
51
        try {
52
            $manifest = $this->readJson(
53
                $devServerUrl .
54
                    '/' .
55
                    config('twill.manifest_file', 'twill-manifest.json'),
56
            );
57
        } catch (\Exception $e) {
58
            throw new \Exception(
59
                'Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.',
60
            );
61
        }
62
63
        return $devServerUrl . ($manifest[$file] ?? '/' . $file);
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    private function readManifest()
70
    {
71
        try {
72
            return Cache::rememberForever('twill-manifest', function () {
73
                return $this->readJson($this->getManifestFilename());
74
            });
75
        } catch (\Exception $e) {
76
            throw new \Exception(
77
                'Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.',
78
            );
79
        }
80
    }
81
82
    private function readJson($fileName)
83
    {
84
        return json_decode(file_get_contents($fileName), true);
85
    }
86
87
    private function devMode()
88
    {
89
        return 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

89
        return app()->/** @scrutinizer ignore-call */ environment('local', 'development') &&
Loading history...
90
            config('twill.dev_mode', false);
91
    }
92
}
93