ElixirExtension::getAssetVersion()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Iulyanp\ElixirBundle\Twig;
4
5
/**
6
 * Class ElixirExtension.
7
 */
8
class ElixirExtension extends \Twig_Extension
9
{
10
    /** @var string */
11
    protected $webDir;
12
13
    /** @var string */
14
    protected $buildDir;
15
16
    /**
17
     * ElixirExtension constructor.
18
     *
19
     * @param string $webDir
20
     * @param string $buildDir
21
     */
22
    public function __construct($webDir, $buildDir)
23
    {
24
        $this->webDir = $webDir;
25
        $this->buildDir = $buildDir;
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    public function getFunctions()
32
    {
33
        return [
34
            new \Twig_SimpleFunction('elixir', [$this, 'getAssetVersion']),
35
        ];
36
    }
37
38
    /**
39
     * @param $asset
40
     *
41
     * @throws \Exception
42
     *
43
     * @return string
44
     */
45
    public function getAssetVersion($asset)
46
    {
47
        $asset = trim($asset, '/');
48
49
        $manifest = $this->readManifest();
50
51
        if (array_key_exists($asset, $manifest)) {
52
            return sprintf(
53
                '%s%s%s%s',
54
                DIRECTORY_SEPARATOR,
55
                $this->buildDir,
56
                DIRECTORY_SEPARATOR,
57
                $manifest[$asset]
58
            );
59
        }
60
61
        throw new \Exception(sprintf('File %s not defined in asset manifest.', $asset));
62
    }
63
64
    /**
65
     * Returns the name of the extension.
66
     *
67
     * @return string The extension name
68
     */
69
    public function getName()
70
    {
71
        return 'elixir';
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77
    private function readManifest()
78
    {
79
        static $manifest;
80
        static $manifestPath;
81
82
        $manifestFile = $this->checkManifestFileExists();
83
84
        if (is_null($manifest) || $manifestPath !== $this->buildDir) {
85
            $manifestPath = $this->buildDir;
86
87
            return json_decode(
88
                file_get_contents($manifestFile),
89
                true
90
            );
91
        }
92
93
        return $manifest;
94
    }
95
96
    /**
97
     * @throws \Exception
98
     *
99
     * @return string
100
     */
101
    private function checkManifestFileExists()
102
    {
103
        $manifestFile = sprintf(
104
            '%s%s%s%s%s',
105
            $this->webDir,
106
            DIRECTORY_SEPARATOR,
107
            $this->buildDir,
108
            DIRECTORY_SEPARATOR,
109
            'rev-manifest.json'
110
        );
111
112
        if (!file_exists($manifestFile)) {
113
            throw new \Exception(sprintf('File %s not defined in asset manifest.', $manifestFile));
114
        }
115
116
        return $manifestFile;
117
    }
118
}
119