MixExtension::readManifest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Iulyanp\ElixirMixBundle\Twig;
4
5
/**
6
 * Class MixExtension.
7
 */
8
class MixExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
9
{
10
    const MANIFEST = 'mix-manifest.json';
11
    /**
12
     * @var string
13
     */
14
    protected $webDir;
15
16
    /**
17
     * MixExtension constructor.
18
     *
19
     * @param string $webDir
20
     */
21
    public function __construct($webDir)
22
    {
23
        $this->webDir = $webDir;
24
    }
25
26
    /**
27
     * @return \Twig_SimpleFunction[]
28
     */
29
    public function getFunctions()
30
    {
31
        return [
32
            new \Twig_SimpleFunction('mix', [$this, 'mix']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
        ];
34
    }
35
36
    /**
37
     * Get the mix file from the manifest
38
     *
39
     * @param string $asset The needed asset
40
     *
41
     * @throws \Exception
42
     * @return string
43
     */
44
    public function mix($asset)
45
    {
46
        $manifest = $this->readManifest();
47
48
        if (!array_key_exists($asset, $manifest)) {
49
            throw new \Exception(
50
                sprintf(
51
                    'The "%s" key could not be found in the manifest file. %s',
52
                    $asset,
53
                    'Please pass just the asset filename as a parameter to the mix() method.'
54
                )
55
            );
56
        }
57
58
        return file_exists($this->webDir.'/hot')
59
            ? "http://localhost:3000{$manifest[$asset]}"
60
            : $manifest[$asset];
61
    }
62
63
    /**
64
     * Returns the name of the extension.
65
     *
66
     * @return string The extension name
67
     */
68
    public function getName()
69
    {
70
        return 'mix';
71
    }
72
73
    /**
74
     * Read the manifest file if exists
75
     *
76
     * @return array
77
     * @throws \Exception
78
     */
79
    private function readManifest()
80
    {
81
        static $manifest;
82
83
        if (!$manifest) {
84
            $manifestPath = sprintf('%s/%s', $this->webDir, self::MANIFEST);
85
86
            if (!file_exists($manifestPath)) {
87
                throw new \Exception(
88
                    'The Laravel Mix manifest file does not exist. ' .
89
                    'Please run "npm run webpack" and try again.'
90
                );
91
            }
92
93
            $manifest = json_decode(file_get_contents($manifestPath), true);
94
        }
95
96
        return $manifest;
97
    }
98
}
99