Configuration::getUmask()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle;
6
7
use Laminas\Stdlib;
8
use Traversable;
9
10
use function array_key_exists;
11
use function array_map;
12
use function explode;
13
use function implode;
14
use function ltrim;
15
use function method_exists;
16
use function preg_match;
17
use function rtrim;
18
use function strtolower;
19
use function trim;
20
21
class Configuration
22
{
23
    /**
24
     * Debug option that is passed to Assetic.
25
     */
26
    protected bool $debug = false;
27
28
    /**
29
     * Combine option giving the opportunity not to combine the assets in debug mode.
30
     */
31
    protected bool $combine = true;
32
33
    /**
34
     * Should build assets on request.
35
     */
36
    protected bool $buildOnRequest = true;
37
38
    /**
39
     * Full path to public directory where assets will be generated.
40
     */
41
    protected ?string $webPath = null;
42
43
    /**
44
     * Full path to cache directory.
45
     */
46
    protected ?string $cachePath = null;
47
48
    /**
49
     * Is cache enabled.
50
     */
51
    protected bool $cacheEnabled = false;
52
53
    /**
54
     * The base url.
55
     *
56
     * By default this value is set from "\Laminas\Http\PhpEnvironment\Request::getBaseUrl()"
57
     *
58
     * Example:
59
     * <code>
60
     * http://example.com/
61
     * </code>
62
     */
63
    protected ?string $baseUrl = null;
64
65
    /**
66
     * The base path.
67
     *
68
     * By default this value is set from "\Laminas\Http\PhpEnvironment\Request::getBasePath()"
69
     *
70
     * Example:
71
     * <code>
72
     * <baseUrl>/~jdo/
73
     * </code>
74
     */
75
    protected ?string $basePath = null;
76
77
    /**
78
     * Asset will be save on disk, only when it's modification time was changed
79
     */
80
    protected bool $writeIfChanged = true;
81
82
    /**
83
     * Default options.
84
     */
85
    protected array $default = [
86
        'assets'  => [],
87
        'options' => [],
88
    ];
89
90
    /**
91
     * Map of routes names and assets configuration.
92
     */
93
    protected array $routes = [];
94
95
    /**
96
     * Map of modules names and assets configuration.
97
     */
98
    protected array $modules = [];
99
100
    /**
101
     * Map of controllers names and assets configuration.
102
     */
103
    protected array $controllers = [];
104
105
    /**
106
     * Map of strategies that will be choose to setup Assetic\AssetInterface
107
     * for particular Laminas\View\Renderer\RendererInterface
108
     */
109
    protected array $rendererToStrategy = [];
110
111 4
    /**
112
     * List of error types occurring in EVENT_DISPATCH_ERROR that will use
113 4
     * this module to render assets.
114 2
     */
115 2
    protected array $acceptableErrors = [];
116
117 2
    /**
118
     * Umask
119
     */
120
    protected ?int $umask = null;
121
122 2
    public function __construct(?iterable $config = null)
123
    {
124 2
        if (null !== $config) {
125
            if ($config instanceof Traversable) {
126
                $this->processArray(Stdlib\ArrayUtils::iteratorToArray($config));
127 1
            } else {
128
                $this->processArray($config);
129 1
            }
130
        }
131
    }
132 2
133
    public function isDebug(): bool
134 2
    {
135
        return $this->debug;
136
    }
137 1
138
    public function setDebug(bool $flag): void
139 1
    {
140
        $this->debug = $flag;
141
    }
142 1
143
    public function isCombine(): bool
144 1
    {
145
        return $this->combine;
146
    }
147
148
    public function setCombine(bool $flag): void
149
    {
150 2
        $this->combine = $flag;
151
    }
152 2
153 1
    public function setWebPath(?string $path): void
154
    {
155
        $this->webPath = $path;
156 1
    }
157 1
158
    /**
159
     * @throws Exception\RuntimeException
160 1
     */
161
    public function getWebPath(?string $file = null): ?string
162
    {
163 1
        if (null === $this->webPath) {
164
            throw new Exception\RuntimeException('Web path is not set');
165 1
        }
166
167
        if (null !== $file) {
168 2
            return rtrim($this->webPath, '/\\') . '/' . ltrim($file, '/\\');
169
        }
170 2
171
        return $this->webPath;
172
    }
173 1
174
    public function setCachePath(?string $path): void
175 1
    {
176
        $this->cachePath = $path;
177
    }
178 2
179
    public function getCachePath(): ?string
180 2
    {
181
        return $this->cachePath;
182
    }
183 1
184
    public function setCacheEnabled(bool $cacheEnabled): void
185 1
    {
186 1
        $this->cacheEnabled = $cacheEnabled;
187
    }
188
189 1
    public function getCacheEnabled(): bool
190 1
    {
191
        return $this->cacheEnabled;
192
    }
193 1
194
    public function setDefault(array $default): void
195
    {
196 2
        if (! isset($default['assets'])) {
197
            $default['assets'] = [];
198 2
        }
199
200
        if (! isset($default['options'])) {
201 1
            $default['options'] = [];
202
        }
203 1
204
        $this->default = $default;
205
    }
206 1
207
    public function getDefault(): array
208 1
    {
209
        return $this->default;
210
    }
211 3
212
    public function setRoutes(array $routes): void
213 3
    {
214 3
        $this->routes = $routes;
215
    }
216
217 3
    public function getRoutes(): array
218 3
    {
219 3
        return $this->routes;
220 3
    }
221
222
    public function getRoute(string $name, ?array $default = null): ?array
223
    {
224
        $assets       = [];
225 3
        $routeMatched = false;
226
227
        // Merge all assets configuration for which regular expression matches route
228 1
        foreach ($this->routes as $spec => $config) {
229
            if (preg_match('(^' . $spec . '$)i', $name)) {
230 1
                $routeMatched = true;
231
                $assets       = Stdlib\ArrayUtils::merge($assets, (array) $config);
232
            }
233 1
        }
234
235 1
        // Only return default if none regular expression matched
236
        return $routeMatched ? $assets : $default;
237
    }
238 1
239
    public function setControllers(array $controllers): void
240 1
    {
241
        $this->controllers = $controllers;
242
    }
243 1
244
    public function getControllers(): array
245 1
    {
246 1
        return $this->controllers;
247 1
    }
248
249
    public function getController(string $name, ?array $default = null): ?array
250
    {
251 1
        return array_key_exists($name, $this->controllers) ? $this->controllers[$name] : $default;
252
    }
253 1
254 1
    public function setModules(array $modules): void
255
    {
256
        $this->modules = [];
257 1
        foreach ($modules as $name => $options) {
258
            $this->addModule($name, $options);
259 1
        }
260
    }
261
262 1
    public function addModule(string $name, array $options): void
263
    {
264 1
        $lowername                 = strtolower($name);
265 1
        $this->modules[$lowername] = $options;
266
    }
267
268 1
    public function getModules(): array
269
    {
270 1
        return $this->modules;
271
    }
272
273 1
    public function getModule(string $name, ?array $default = null): ?array
274
    {
275 1
        $lowername = strtolower($name);
276 1
        return array_key_exists($lowername, $this->modules) ? $this->modules[$lowername] : $default;
277
    }
278 1
279
    public function detectBaseUrl(): bool
280
    {
281 2
        return null === $this->baseUrl || 'auto' === $this->baseUrl;
282
    }
283 2
284
    public function setBaseUrl(?string $baseUrl): void
285
    {
286 1
        if (null !== $baseUrl && 'auto' !== $baseUrl) {
287
            $baseUrl = rtrim($baseUrl, '/') . '/';
288 1
        }
289 1
        $this->baseUrl = $baseUrl;
290 1
    }
291
292 1
    public function getBaseUrl(): ?string
293
    {
294
        return $this->baseUrl;
295 2
    }
296
297 2
    public function setBasePath(?string $basePath): void
298
    {
299
        if (null !== $basePath) {
300 2
            $basePath  = trim($basePath, '/');
301
            $basePath .= '/';
302 2
        }
303 2
        $this->basePath = $basePath;
304 2
    }
305
306
    public function getBasePath(): ?string
307
    {
308 2
        return $this->basePath;
309
    }
310 2
311 2
    protected function processArray(iterable $config): void
312 2
    {
313 2
        foreach ($config as $key => $value) {
314 1
            $setter = $this->assembleSetterNameFromConfigKey($key);
315 1
            $this->{$setter}($value);
316
        }
317
    }
318
319
    protected function assembleSetterNameFromConfigKey(string $key): string
320
    {
321 2
        $parts  = explode('_', $key);
322
        $parts  = array_map('ucfirst', $parts);
323
        $setter = 'set' . implode('', $parts);
324 1
        if (! method_exists($this, $setter)) {
325
            throw new Exception\BadMethodCallException(
326 1
                'The configuration key "' . $key . '" does not '
327
                    . 'have a matching ' . $setter . ' setter method '
328
                    . 'which must be defined'
329 1
            );
330
        }
331 1
332
        return $setter;
333
    }
334 1
335
    public function setRendererToStrategy(array $strategyForRenderer): void
336 1
    {
337
        $this->rendererToStrategy = $strategyForRenderer;
338
    }
339 2
340
    public function addRendererToStrategy(string $rendererClass, string $strategyClass): void
341 2
    {
342
        $this->rendererToStrategy[$rendererClass] = $strategyClass;
343
    }
344 2
345
    public function getStrategyNameForRenderer(string $rendererName, ?string $default = null): ?string
346 2
    {
347
        return array_key_exists($rendererName, $this->rendererToStrategy)
348
            ? $this->rendererToStrategy[$rendererName] : $default;
349 1
    }
350
351 1
    public function setAcceptableErrors(array $acceptableErrors): void
352
    {
353
        $this->acceptableErrors = $acceptableErrors;
354 1
    }
355
356 1
    public function getAcceptableErrors(): array
357
    {
358
        return $this->acceptableErrors;
359 1
    }
360
361 1
    public function getUmask(): ?int
362
    {
363
        return $this->umask;
364 1
    }
365
366 1
    public function setUmask(?int $umask): void
367
    {
368
        $this->umask = $umask;
369 1
    }
370
371 1
    public function setBuildOnRequest(bool $flag): void
372
    {
373
        $this->buildOnRequest = $flag;
374 1
    }
375
376 1
    public function getBuildOnRequest(): bool
377
    {
378
        return $this->buildOnRequest;
379
    }
380
381
    public function setWriteIfChanged(bool $flag): void
382
    {
383
        $this->writeIfChanged = $flag;
384
    }
385
386
    public function getWriteIfChanged(): bool
387
    {
388
        return $this->writeIfChanged;
389
    }
390
}
391