AssetPathResolver::getManifest()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\WebpackEncoreResolver;
4
5
final class AssetPathResolver
6
{
7
    /**
8
     * @var string
9
     */
10
    private $directory;
11
12
    /**
13
     * @var array
14
     */
15
    private $entrypoints;
16
17
    /**
18
     * @var array
19
     */
20
    private static $filesMap = [];
21
22
    /**
23
     * @var array
24
     */
25
    private $manifest;
26
27
    /**
28
     * @var string
29
     */
30
    private static $DEFAULT_DIRECTORY;
31
32
    public function __construct($directory = null)
33
    {
34
        $this->directory =
35
            null !== $directory ? $directory : $this->getDefaultDirectory();
36
    }
37
38
    public static function setDefaultDirectory($directory)
39
    {
40
        self::$DEFAULT_DIRECTORY = $directory;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    private function getDefaultDirectory()
47
    {
48
        if (isset(self::$DEFAULT_DIRECTORY)) {
49
            return self::$DEFAULT_DIRECTORY;
50
        }
51
52
        $docRoot = isset($_SERVER['DOCUMENT_ROOT'])
53
            ? $_SERVER['DOCUMENT_ROOT']
54
            : \getcwd();
55
56
        return \rtrim($docRoot, \DIRECTORY_SEPARATOR) .
57
            \DIRECTORY_SEPARATOR .
58
            'build';
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    private function getEntrypoints()
65
    {
66
        if (null === $this->entrypoints) {
67
            $file =
68
                $this->directory . \DIRECTORY_SEPARATOR . 'entrypoints.json';
69
            $content = \file_get_contents($file);
70
            if (false === $content) {
71
                throw new \RuntimeException(
72
                    \sprintf('Unable to read file "%s"', $file)
73
                );
74
            }
75
76
            $json = \json_decode($content, true);
77
            if (\JSON_ERROR_NONE !== \json_last_error()) {
78
                throw new \RuntimeException(
79
                    \sprintf('Unable to decode json file "%s"', $file)
80
                );
81
            }
82
83
            $this->entrypoints = $json['entrypoints'];
84
        }
85
        return $this->entrypoints;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    private function getManifest()
92
    {
93
        if (null === $this->manifest) {
94
            $file = $this->directory . \DIRECTORY_SEPARATOR . 'manifest.json';
95
            $content = \file_get_contents($file);
96
            if (false === $content) {
97
                throw new \RuntimeException(
98
                    \sprintf('Unable to read file "%s"', $file)
99
                );
100
            }
101
102
            $json = \json_decode($content, true);
103
            if (\JSON_ERROR_NONE !== \json_last_error()) {
104
                throw new \RuntimeException(
105
                    \sprintf('Unable to decode json file "%s"', $file)
106
                );
107
            }
108
109
            $this->manifest = $json;
110
        }
111
112
        return $this->manifest;
113
    }
114
115
    /**
116
     * @param $entrypoint
117
     * @return array
118
     */
119
    public function getWebpackJsFiles($entrypoint)
120
    {
121
        if (!\array_key_exists($entrypoint, $this->getEntrypoints())) {
122
            throw new \InvalidArgumentException(
123
                \sprintf('Invalid entrypoint "%s"', $entrypoint)
124
            );
125
        }
126
127
        $files = $this->getEntrypoints()[$entrypoint];
128
        if (!\array_key_exists('js', $files)) {
129
            return [];
130
        }
131
132
        return $files['js'];
133
    }
134
135
    /**
136
     * @param $entrypoint
137
     * @return array
138
     */
139
    public function getWebpackCssFiles($entrypoint)
140
    {
141
        if (!\array_key_exists($entrypoint, $this->getEntrypoints())) {
142
            throw new \InvalidArgumentException(
143
                \sprintf('Invalid entrypoint "%s"', $entrypoint)
144
            );
145
        }
146
147
        $files = $this->getEntrypoints()[$entrypoint];
148
        if (!\array_key_exists('css', $files)) {
149
            return [];
150
        }
151
152
        return $files['css'];
153
    }
154
155
    /**
156
     * @param $entrypoint
157
     */
158
    public function renderWebpackScriptTags($entrypoint)
159
    {
160
        $files = $this->getWebpackJsFiles($entrypoint);
161
        foreach ($files as $file) {
162
            if (empty(static::$filesMap[$file])) {
0 ignored issues
show
Comprehensibility introduced by
Since BenTools\WebpackEncoreResolver\AssetPathResolver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
163
                printf('<script src="%s"></script>', $file);
164
                static::$filesMap[$file] = true;
0 ignored issues
show
Comprehensibility introduced by
Since BenTools\WebpackEncoreResolver\AssetPathResolver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
165
            }
166
        }
167
    }
168
169
    /**
170
     * @param $entrypoint
171
     */
172
    public function renderWebpackLinkTags($entrypoint)
173
    {
174
        $files = $this->getWebpackCssFiles($entrypoint);
175
        foreach ($files as $file) {
176
            if (empty(static::$filesMap[$file])) {
0 ignored issues
show
Comprehensibility introduced by
Since BenTools\WebpackEncoreResolver\AssetPathResolver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
177
                printf('<link rel="stylesheet" href="%s">', $file);
178
                static::$filesMap[$file] = true;
0 ignored issues
show
Comprehensibility introduced by
Since BenTools\WebpackEncoreResolver\AssetPathResolver is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
179
            }
180
        }
181
    }
182
183
    /**
184
     * @param $resource
185
     * @return string
186
     */
187
    public function getAssetPath($resource)
188
    {
189
        $withoutLeadingSlash = \ltrim($resource, '/');
190
        $manifest = $this->getManifest();
191
        if (isset($manifest[$resource])) {
192
            return $manifest[$resource];
193
        }
194
        if (isset($manifest[$withoutLeadingSlash])) {
195
            return $manifest[$withoutLeadingSlash];
196
        }
197
198
        return $resource;
199
    }
200
}
201