Passed
Push — master ( a48691...b214ed )
by Peter
02:45
created

AssetManager::getWebPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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\Filesystem\IFileFinder;
10
use League\Flysystem\FileNotFoundException;
11
use MatthiasMullie\Minify\CSS as CssMinifier;
12
use MatthiasMullie\Minify\JS as JsMinifier;
13
14
/**
15
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
16
 */
17
class AssetManager
18
{
19
    protected const EXT_CSS = '.css';
20
    protected const EXT_JS  = '.js';
21
22
    /** @var MinifierFactory */
23
    protected $minifierFactory;
24
25
    /** @var IFileFinder */
26
    protected $fileFinder;
27
28
    /** @var ICacheManager */
29
    protected $cacheManager;
30
31
    /** @var JsMinifier[] */
32
    protected $jsMinifiers = [];
33
34
    /** @var CssMinifier[] */
35
    protected $cssMinifiers = [];
36
37
    /** @var string */
38
    protected $cacheUrlBase;
39
40
    /**
41
     * AssetManager constructor.
42
     *
43
     * @param MinifierFactory $minifierFactory
44
     * @param IFileFinder     $fileFinder
45
     * @param ICacheManager   $cacheManager
46
     * @param string          $cacheUrlBase
47
     */
48
    public function __construct(
49
        MinifierFactory $minifierFactory,
50
        IFileFinder $fileFinder,
51
        ICacheManager $cacheManager,
52
        string $cacheUrlBase
53
    ) {
54
        $this->minifierFactory = $minifierFactory;
55
        $this->fileFinder      = $fileFinder;
56
        $this->cacheManager    = $cacheManager;
57
        $this->cacheUrlBase    = $cacheUrlBase;
58
    }
59
60
    /**
61
     * @param string $groupName
62
     * @param string $rawPath
63
     *
64
     * @throws \League\Flysystem\FileNotFoundException
65
     */
66
    public function addCss(string $groupName, string $rawPath)
67
    {
68
        $fixedPath = $rawPath;
69
        if ($this->getExtension($rawPath) !== static:: EXT_CSS) {
70
            $fixedPath = $rawPath . static::EXT_CSS;
71
        }
72
73
        $content = $this->fileFinder->read($fixedPath, $groupName);
74
75
        $this->getCssMinifier($groupName)->add($content);
76
    }
77
78
    /**
79
     * @param string $groupName
80
     * @param string $rawPath
81
     *
82
     * @throws \League\Flysystem\FileNotFoundException
83
     */
84
    public function addJs(string $groupName, string $rawPath)
85
    {
86
        $fixedPath = $rawPath;
87
        if ($this->getExtension($rawPath) !== static:: EXT_JS) {
88
            $fixedPath = $rawPath . static::EXT_JS;
89
        }
90
91
        $content = $this->fileFinder->read($fixedPath, $groupName);
92
93
        $this->getJsMinifier($groupName)->add($content);
94
    }
95
96
    /**
97
     * @param string $path
98
     *
99
     * @return string
100
     */
101
    protected function getExtension(string $path): string
102
    {
103
        $dotrpos = strrpos($path, '.');
104
105
        if ($dotrpos === false) {
106
            return '';
107
        }
108
109
        return substr($path, $dotrpos);
110
    }
111
112
    /**
113
     * @param string $groupName
114
     * @param string $content
115
     */
116
    public function addCssContent(string $groupName, string $content)
117
    {
118
        $this->getCssMinifier($groupName)->add($content);
119
    }
120
121
    /**
122
     * @param string $groupName
123
     * @param string $content
124
     */
125
    public function addJsContent(string $groupName, string $content)
126
    {
127
        $this->getJsMinifier($groupName)->add($content);
128
    }
129
130
    /**
131
     * @param string $groupName
132
     *
133
     * @return string
134
     * @throws \League\Flysystem\FileExistsException
135
     */
136
    public function renderCss(string $groupName): string
137
    {
138
        $content   = $this->getCssMinifier($groupName)->minify();
139
        $cachePath = $groupName . static::EXT_CSS;
140
141
        $this->cacheManager->write($cachePath, $content);
142
143
        return $content;
144
    }
145
146
    /**
147
     * @param string $groupName
148
     *
149
     * @return string
150
     * @throws \League\Flysystem\FileExistsException
151
     */
152
    public function renderJs(string $groupName): string
153
    {
154
        $content   = $this->getJsMinifier($groupName)->minify();
155
        $cachePath = $groupName . static::EXT_JS;
156
157
        $this->cacheManager->write($cachePath, $content);
158
159
        return $content;
160
    }
161
162
    /**
163
     * @param string $cachePath
164
     *
165
     * @return string|null
166
     * @throws \League\Flysystem\FileExistsException
167
     * @throws \League\Flysystem\FileNotFoundException
168
     */
169
    public function renderRaw(string $cachePath): ?string
170
    {
171
        $content = $this->fileFinder->read($cachePath);
172
        if (null === $content) {
173
            return null;
174
        }
175
176
        $this->cacheManager->write($cachePath, $content);
177
178
        return $content;
179
    }
180
181
    /**
182
     * @param string $groupName
183
     *
184
     * @return string
185
     * @throws \League\Flysystem\FileExistsException
186
     * @throws \League\Flysystem\FileNotFoundException
187
     */
188
    public function ensureCssWebPath(string $groupName): string
189
    {
190
        $cachePath = $groupName . static::EXT_CSS;
191
192
        if (!$this->cacheManager->has($cachePath)) {
193
            $this->renderCss($groupName);
194
        }
195
196
        return $this->getWebPath($cachePath);
197
    }
198
199
    /**
200
     * @param string $groupName
201
     *
202
     * @return string
203
     * @throws \League\Flysystem\FileExistsException
204
     * @throws \League\Flysystem\FileNotFoundException
205
     */
206
    public function ensureJsWebPath(string $groupName): string
207
    {
208
        $cachePath = $groupName . static::EXT_JS;
209
210
        if (!$this->cacheManager->has($cachePath)) {
211
            $this->renderJs($groupName);
212
        }
213
214
        return $this->getWebPath($cachePath);
215
    }
216
217
    /**
218
     * @param string $cachePath
219
     *
220
     * @return string
221
     * @throws \League\Flysystem\FileExistsException
222
     * @throws \League\Flysystem\FileNotFoundException
223
     */
224
    public function ensureImgWebPath(string $cachePath): string
225
    {
226
        if (!$this->cacheManager->has($cachePath)) {
227
            if ($this->renderRaw($cachePath) === null) {
228
                throw new FileNotFoundException($cachePath);
229
            }
230
        }
231
232
        return $this->getWebPath($cachePath);
233
    }
234
235
    /**
236
     * @param string $cachePath
237
     *
238
     * @return string
239
     */
240
    protected function getWebPath(string $cachePath): string
241
    {
242
        $path = $this->cacheManager->getWebPath($cachePath);
243
        if (!$path) {
244
            return $path;
245
        }
246
247
        return sprintf(
248
            '%s%s%s',
249
            rtrim($this->cacheUrlBase, DIRECTORY_SEPARATOR),
250
            DIRECTORY_SEPARATOR,
251
            ltrim($path, DIRECTORY_SEPARATOR)
252
        );
253
    }
254
255
    /**
256
     * @param string $key
257
     *
258
     * @return CssMinifier
259
     */
260
    protected function getCssMinifier(string $key): CssMinifier
261
    {
262
        if (!array_key_exists($key, $this->cssMinifiers)) {
263
            $this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier();
264
        }
265
266
        return $this->cssMinifiers[$key];
267
    }
268
269
    /**
270
     * @param string $key
271
     *
272
     * @return JsMinifier
273
     */
274
    protected function getJsMinifier(string $key): JsMinifier
275
    {
276
        if (!array_key_exists($key, $this->jsMinifiers)) {
277
            $this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier();
278
        }
279
280
        return $this->jsMinifiers[$key];
281
    }
282
}
283