Completed
Push — master ( 341b2f...154d99 )
by Alberto
14s queued 12s
created

AssetHelper::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2019 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace BEdita\WebTools\View\Helper;
15
16
use Cake\Utility\Hash;
17
use Cake\View\Helper;
18
19
/**
20
 * Asset Helper to handle asset names with signatures
21
 */
22
class AssetHelper extends Helper
23
{
24
    /**
25
     * Array having asset names as keys and revved asset names as values
26
     *
27
     * @var array
28
     */
29
    protected $assets = [];
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function initialize(array $config): void
35
    {
36
        $manifestPath = Hash::get($config, 'manifestPath', CONFIG . 'rev-manifest.json');
37
        if (!file_exists($manifestPath)) {
38
            return;
39
        }
40
41
        $this->assets = json_decode(file_get_contents($manifestPath), true);
42
    }
43
44
    /**
45
     * Retrieve `revved` asset name if found in manifest or return canonical asset name otherwise
46
     *
47
     * @param string $name Canonical asset name (un-revved)
48
     * @return string
49
     */
50
    public function get(string $name): string
51
    {
52
        if (!empty($this->assets[$name])) {
53
            $name = (string)$this->assets[$name];
54
        }
55
56
        return $name;
57
    }
58
}
59