Passed
Push — master ( cdcb0a...3da5b1 )
by Peter
02:52
created

AssetManager::addJsVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets;
6
7
use AbterPhp\Framework\Assets\CacheManager\ICacheManager;
8
use AbterPhp\Framework\Assets\Factory\Minifier as MinifierFactory;
9
use AbterPhp\Framework\Config\Routes;
10
use AbterPhp\Framework\Filesystem\IFileFinder;
11
use League\Flysystem\FileNotFoundException;
12
use MatthiasMullie\Minify\CSS as CssMinifier;
13
use MatthiasMullie\Minify\JS as JsMinifier;
14
15
/**
16
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
17
 */
18
class AssetManager
19
{
20
    protected const EXT_CSS = '.css';
21
    protected const EXT_JS  = '.js';
22
23
    /** @var MinifierFactory */
24
    protected $minifierFactory;
25
26
    /** @var IFileFinder */
27
    protected $fileFinder;
28
29
    /** @var ICacheManager */
30
    protected $cacheManager;
31
32
    /** @var JsMinifier[] */
33
    protected $jsMinifiers = [];
34
35
    /** @var CssMinifier[] */
36
    protected $cssMinifiers = [];
37
38
    /** @var UrlFixer */
39
    protected $urlFixer;
40
41
    /**
42
     * AssetManager constructor.
43
     *
44
     * @param MinifierFactory $minifierFactory
45
     * @param IFileFinder     $fileFinder
46
     * @param ICacheManager   $cacheManager
47
     * @param UrlFixer        $urlFixer
48
     */
49
    public function __construct(
50
        MinifierFactory $minifierFactory,
51
        IFileFinder $fileFinder,
52
        ICacheManager $cacheManager,
53
        UrlFixer $urlFixer
54
    ) {
55
        $this->minifierFactory = $minifierFactory;
56
        $this->fileFinder      = $fileFinder;
57
        $this->cacheManager    = $cacheManager;
58
        $this->urlFixer        = $urlFixer;
59
    }
60
61
    /**
62
     * @param string $groupName
63
     * @param string $rawPath
64
     *
65
     * @throws \League\Flysystem\FileNotFoundException
66
     */
67
    public function addCss(string $groupName, string $rawPath)
68
    {
69
        $fixedPath = $rawPath;
70
        if ($this->getExtension($rawPath) !== static:: EXT_CSS) {
71
            $fixedPath = $rawPath . static::EXT_CSS;
72
        }
73
74
        $content = $this->fileFinder->read($fixedPath, $groupName);
75
76
        $fixedContent = $this->urlFixer->fixCss($content, $fixedPath);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $content of AbterPhp\Framework\Assets\UrlFixer::fixCss() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
        $fixedContent = $this->urlFixer->fixCss(/** @scrutinizer ignore-type */ $content, $fixedPath);
Loading history...
77
78
        $this->getCssMinifier($groupName)->add($fixedContent);
79
    }
80
81
    /**
82
     * @param string $groupName
83
     * @param string $rawPath
84
     *
85
     * @throws \League\Flysystem\FileNotFoundException
86
     */
87
    public function addJs(string $groupName, string $rawPath)
88
    {
89
        $fixedPath = $rawPath;
90
        if ($this->getExtension($rawPath) !== static:: EXT_JS) {
91
            $fixedPath = $rawPath . static::EXT_JS;
92
        }
93
94
        $content = $this->fileFinder->read($fixedPath, $groupName);
95
96
        $this->getJsMinifier($groupName)->add($content);
97
    }
98
99
    /**
100
     * @param string $path
101
     *
102
     * @return string
103
     */
104
    protected function getExtension(string $path): string
105
    {
106
        $dotrpos = strrpos($path, '.');
107
108
        if ($dotrpos === false) {
109
            return '';
110
        }
111
112
        return substr($path, $dotrpos);
113
    }
114
115
    /**
116
     * @param string $groupName
117
     * @param string $content
118
     */
119
    public function addCssContent(string $groupName, string $content)
120
    {
121
        $this->getCssMinifier($groupName)->add($content);
122
    }
123
124
    /**
125
     * @param string $groupName
126
     * @param string $content
127
     */
128
    public function addJsContent(string $groupName, string $content)
129
    {
130
        $this->getJsMinifier($groupName)->add($content);
131
    }
132
133
    /**
134
     * @param string $groupName
135
     * @param string $name
136
     * @param mixed  $value
137
     */
138
    public function addJsVar(string $groupName, string $name, $value)
139
    {
140
        $this->getJsMinifier($groupName)->add(sprintf("var %s = %s;\n", $name, json_encode($value)));
141
    }
142
143
    /**
144
     * @param string $groupName
145
     *
146
     * @return string
147
     * @throws \League\Flysystem\FileExistsException
148
     */
149
    public function renderCss(string $groupName): string
150
    {
151
        $content   = $this->getCssMinifier($groupName)->minify();
152
        $cachePath = $groupName . static::EXT_CSS;
153
154
        $this->cacheManager->write($cachePath, $content);
155
156
        return $content;
157
    }
158
159
    /**
160
     * @param string $groupName
161
     *
162
     * @return string
163
     * @throws \League\Flysystem\FileExistsException
164
     */
165
    public function renderJs(string $groupName): string
166
    {
167
        $content   = $this->getJsMinifier($groupName)->minify();
168
        $cachePath = $groupName . static::EXT_JS;
169
170
        $this->cacheManager->write($cachePath, $content);
171
172
        return $content;
173
    }
174
175
    /**
176
     * @param string $cachePath
177
     *
178
     * @return string|null
179
     * @throws \League\Flysystem\FileExistsException
180
     * @throws \League\Flysystem\FileNotFoundException
181
     */
182
    public function renderRaw(string $cachePath): ?string
183
    {
184
        $content = $this->fileFinder->read($cachePath);
185
        if (null === $content) {
186
            return null;
187
        }
188
189
        $this->cacheManager->write($cachePath, $content);
190
191
        return $content;
192
    }
193
194
    /**
195
     * @param string $groupName
196
     *
197
     * @return string
198
     * @throws \League\Flysystem\FileExistsException
199
     * @throws \League\Flysystem\FileNotFoundException
200
     */
201
    public function ensureCssWebPath(string $groupName): string
202
    {
203
        $cachePath = $groupName . static::EXT_CSS;
204
205
        if (!$this->cacheManager->has($cachePath)) {
206
            $this->renderCss($groupName);
207
        }
208
209
        return $this->getWebPath($cachePath);
210
    }
211
212
    /**
213
     * @param string $groupName
214
     *
215
     * @return string
216
     * @throws \League\Flysystem\FileExistsException
217
     * @throws \League\Flysystem\FileNotFoundException
218
     */
219
    public function ensureJsWebPath(string $groupName): string
220
    {
221
        $cachePath = $groupName . static::EXT_JS;
222
223
        if (!$this->cacheManager->has($cachePath)) {
224
            $this->renderJs($groupName);
225
        }
226
227
        return $this->getWebPath($cachePath);
228
    }
229
230
    /**
231
     * @param string $cachePath
232
     *
233
     * @return string
234
     * @throws \League\Flysystem\FileExistsException
235
     * @throws \League\Flysystem\FileNotFoundException
236
     */
237
    public function ensureImgWebPath(string $cachePath): string
238
    {
239
        if (!$this->cacheManager->has($cachePath)) {
240
            if ($this->renderRaw($cachePath) === null) {
241
                throw new FileNotFoundException($cachePath);
242
            }
243
        }
244
245
        return $this->getWebPath($cachePath);
246
    }
247
248
    /**
249
     * @param string $cachePath
250
     *
251
     * @return string
252
     */
253
    protected function getWebPath(string $cachePath): string
254
    {
255
        $path = $this->cacheManager->getWebPath($cachePath);
256
        if (!$path) {
257
            return $path;
258
        }
259
260
        $cachePath = Routes::getCacheUrl();
261
        if (!$cachePath) {
262
            return $path;
263
        }
264
265
        return sprintf(
266
            '%s%s%s',
267
            $cachePath,
268
            DIRECTORY_SEPARATOR,
269
            ltrim($path, DIRECTORY_SEPARATOR)
270
        );
271
    }
272
273
    /**
274
     * @param string $key
275
     *
276
     * @return CssMinifier
277
     */
278
    protected function getCssMinifier(string $key): CssMinifier
279
    {
280
        if (!array_key_exists($key, $this->cssMinifiers)) {
281
            $this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier();
282
        }
283
284
        return $this->cssMinifiers[$key];
285
    }
286
287
    /**
288
     * @param string $key
289
     *
290
     * @return JsMinifier
291
     */
292
    protected function getJsMinifier(string $key): JsMinifier
293
    {
294
        if (!array_key_exists($key, $this->jsMinifiers)) {
295
            $this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier();
296
        }
297
298
        return $this->jsMinifiers[$key];
299
    }
300
}
301