Completed
Pull Request — master (#12)
by Stefano
04:01
created

AssetHelper::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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
21
 */
22
class AssetHelper extends Helper
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public $helpers = ['Html'];
28
29
    /**
30
     * Array having asset names as keys and revved asset names as values
31
     *
32
     * @var array
33
     */
34
    protected $assets = [];
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function initialize(array $config): void
40
    {
41
        $manifestPath = Hash::get($config, 'manifestPath', CONFIG . 'rev-manifest.json');
42
        if (!file_exists($manifestPath)) {
43
            return;
44
        }
45
46
        $this->assets = json_decode(file_get_contents($manifestPath), true);
47
    }
48
49
    /**
50
     * Retrieve revved asset name if found in manifest or return canonical asset name otherwise
51
     *
52
     * @param string $name Canonical asset name (un-revved)
53
     * @return string
54
     */
55
    public function get(string $name): string
56
    {
57
        if (!empty($this->assets[$name])) {
58
            $name = (string)$this->assets[$name];
59
        }
60
61
        return $name;
62
    }
63
}
64