Passed
Push — twig ( b45f57...8d8b45 )
by Arnaud
02:37
created

Asset::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Assets;
12
13
use Cecil\Builder;
14
use Cecil\Config;
15
use Cecil\Exception\Exception;
16
use Cecil\Util;
17
use MatthiasMullie\Minify;
18
use ScssPhp\ScssPhp\Compiler;
19
20
class Asset implements \ArrayAccess
21
{
22
    /** @var Builder */
23
    protected $builder;
24
    /** @var Config */
25
    protected $config;
26
    /** @var string[] */
27
    protected $pathinfo = [];
28
    /** @var array */
29
    protected $data = [];
30
    /** @var bool */
31
    protected $compiled = false;
32
    /** @var bool */
33
    protected $minified = false;
34
35
    /**
36
     * Creates an Asset from file.
37
     *
38
     * $options[
39
     *     'minify'     => true,
40
     *     'version'    => true,
41
     *     'attributes' => ['title' => 'Titre'],
42
     * ];
43
     *
44
     * @param Builder    $builder
45
     * @param string     $path
46
     * @param array|null $options
47
     */
48
    public function __construct(Builder $builder, string $path, array $options = null)
49
    {
50
        $this->builder = $builder;
51
        $this->config = $builder->getConfig();
52
        $path = '/'.ltrim($path, '/');
53
54
        if (false === $filePath = $this->findFile($path)) {
55
            throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path));
56
        }
57
58
        $this->pathinfo = pathinfo($path);
59
60
        // handles options
61
        $minify = (bool) $this->config->get('assets.minify.auto');
62
        $version = (bool) $this->config->get('assets.version.auto');
63
        $attributes = null;
64
        extract(is_array($options) ? $options : [], EXTR_IF_EXISTS);
65
66
        // set data
67
        $this->data['file'] = $filePath;
68
        $this->data['path'] = $path;
69
        $this->data['ext'] = $this->pathinfo['extension'];
70
        $this->data['type'] = explode('/', mime_content_type($filePath))[0];
71
        $this->data['content'] = '';
72
        $this->data['content'] = file_get_contents($filePath);
73
        $this->data['attributes'] = $attributes;
74
75
        // compiling
76
        if ((bool) $this->config->get('assets.sass.auto')) {
77
            $this->compile();
78
        }
79
        // versionning
80
        if ($version) {
81
            $this->version();
82
        }
83
        // minifying
84
        if ($minify) {
85
            $this->minify();
86
        }
87
    }
88
89
    /**
90
     * Returns Asset path.
91
     *
92
     * @return string
93
     */
94
    public function __toString(): string
95
    {
96
        return $this->data['path'];
97
    }
98
99
    /**
100
     * Versions a file.
101
     *
102
     * @return self
103
     */
104
    public function version(): self
105
    {
106
        if ($this->versioned) {
107
            return $this;
108
        }
109
110
        $version = $this->builder->time;
111
        if ($this->config->get('assets.version.strategy') == 'static') {
112
            $version = $this->config->get('assets.version.value');
113
        }
114
115
        $previousPath = $this->data['path'];
116
        $this->data['path'] = \sprintf(
117
            '%s.%s.%s',
118
            Util::joinPath($this->pathinfo['dirname'], $this->pathinfo['filename']),
119
            $version,
120
            $this->pathinfo['extension']
121
        );
122
123
        $this->save($previousPath);
124
125
        $this->versioned = true;
0 ignored issues
show
Bug Best Practice introduced by
The property versioned does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
126
127
        return $this;
128
    }
129
130
    /**
131
     * Compiles a SCSS.
132
     *
133
     * @return self
134
     */
135
    public function compile(): self
136
    {
137
        if ($this->compiled) {
138
            return $this;
139
        }
140
141
        if ($this->data['ext'] != 'scss') {
142
            return $this;
143
        }
144
145
        $previousPath = $this->data['path'];
146
        $this->data['path'] = preg_replace('/scss/m', 'css', $this->data['path']);
147
        $this->data['ext'] = 'css';
148
149
        $cache = new Cache($this->builder, 'assets');
150
        $cacheKey = $cache->createKeyFromAsset($this);
151
        if (!$cache->has($cacheKey)) {
152
            $scssPhp = new Compiler();
153
            $variables = $this->config->get('assets.sass.variables') ?? [];
154
            $scssDir = $this->config->get('assets.sass.dir') ?? [];
155
            $themes = $this->config->getTheme() ?? [];
156
            foreach ($scssDir as $dir) {
157
                $scssPhp->addImportPath(Util::joinPath($this->config->getStaticPath(), $dir));
158
                $scssPhp->addImportPath(Util::joinPath(dirname($this->data['file']), $dir));
159
                foreach ($themes as $theme) {
160
                    $scssPhp->addImportPath(Util::joinPath($this->config->getThemeDirPath($theme, "static/$dir")));
161
                }
162
            }
163
            $scssPhp->setVariables($variables);
164
            $scssPhp->setFormatter('ScssPhp\ScssPhp\Formatter\\'.ucfirst($this->config->get('assets.sass.style')));
165
            $this->data['content'] = $scssPhp->compile($this->data['content']);
166
            $cache->set($cacheKey, $this->data['content']);
167
        }
168
169
        $this->data['content'] = $cache->get($cacheKey, $this->data['content']);
170
171
        $this->save($previousPath);
172
173
        $this->compiled = true;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Minifying a CSS or a JS.
180
     *
181
     * @return self
182
     */
183
    public function minify(): self
184
    {
185
        if ($this->minified) {
186
            return $this;
187
        }
188
189
        if ($this->data['ext'] == 'scss') {
190
            $this->compile();
191
        }
192
193
        if ($this->data['ext'] != 'css' && $this->data['ext'] != 'js') {
194
            return $this;
195
        }
196
197
        $previousPath = $this->data['path'];
198
        $this->data['path'] = \sprintf('%s.min.%s', substr($this->data['path'], 0, -strlen('.'.$this->data['ext'])), $this->data['ext']);
199
200
        $cache = new Cache($this->builder, 'assets');
201
        $cacheKey = $cache->createKeyFromAsset($this);
202
        if (!$cache->has($cacheKey)) {
203
            switch ($this->data['ext']) {
204
                case 'css':
205
                    $minifier = new Minify\CSS($this->data['content']);
206
                    break;
207
                case 'js':
208
                    $minifier = new Minify\JS($this->data['content']);
209
                    break;
210
                default:
211
                    throw new Exception(sprintf('Not able to minify "%s"', $this->data['path']));
212
            }
213
            $this->data['content'] = $minifier->minify();
214
            $cache->set($cacheKey, $this->data['content']);
215
        }
216
        $this->data['content'] = $cache->get($cacheKey, $this->data['content']);
217
218
        $this->save($previousPath);
219
220
        $this->minified = true;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Saves file (and deletes previous file).
227
     *
228
     * @param string $previousPath
229
     *
230
     * @return void
231
     */
232
    public function save(string $previousPath = null): void
233
    {
234
        if (!$this->builder->getBuildOptions()['dry-run']) {
235
            Util::getFS()->dumpFile(Util::joinFile($this->config->getOutputPath(), $this->data['path']), $this->data['content']);
236
            if (!empty($previousPath)) {
237
                Util::getFS()->remove(Util::joinFile($this->config->getOutputPath(), $previousPath));
238
            }
239
        }
240
    }
241
242
    /**
243
     * Implements \ArrayAccess.
244
     */
245
    public function offsetSet($offset, $value)
246
    {
247
        if (!is_null($offset)) {
248
            $this->data[$offset] = $value;
249
        }
250
    }
251
252
    /**
253
     * Implements \ArrayAccess.
254
     */
255
    public function offsetExists($offset)
256
    {
257
        return isset($this->data[$offset]);
258
    }
259
260
    /**
261
     * Implements \ArrayAccess.
262
     */
263
    public function offsetUnset($offset)
264
    {
265
        unset($this->data[$offset]);
266
    }
267
268
    /**
269
     * Implements \ArrayAccess.
270
     */
271
    public function offsetGet($offset)
272
    {
273
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
274
    }
275
276
    /**
277
     * Try to find a static file (in site or theme(s)) if exists or returns false.
278
     *
279
     * @param string $path
280
     *
281
     * @return string|false
282
     */
283
    private function findFile(string $path)
284
    {
285
        $filePath = Util::joinFile($this->config->getStaticPath(), $path);
286
        if (Util::getFS()->exists($filePath)) {
287
            return $filePath;
288
        }
289
290
        // checks in each theme
291
        foreach ($this->config->getTheme() as $theme) {
292
            $filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path);
293
            if (Util::getFS()->exists($filePath)) {
294
                return $filePath;
295
            }
296
        }
297
298
        return false;
299
    }
300
}
301