Completed
Push — master ( 07703f...53d2c5 )
by Stefano
16s queued 14s
created

AssetsRevisions::getStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2020 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\WebTools\Utility;
16
17
use BEdita\WebTools\Utility\Asset\AssetStrategyInterface;
18
19
/**
20
 * Utility class to handle asset names with revisions/signatures.
21
 *
22
 * It can use different strategies to search assets.
23
 */
24
class AssetsRevisions
25
{
26
    /**
27
     * The asset strategy adopted.
28
     *
29
     * @var \BEdita\WebTools\Utility\Asset\AssetStrategyInterface
30
     */
31
    protected static $strategy = null;
32
33
    /**
34
     * Set an asset strategy to be used.
35
     *
36
     * @param \BEdita\WebTools\Utility\Asset\AssetStrategyInterface $strategy The asset strategy to use
37
     * @return void
38
     */
39
    public static function setStrategy(AssetStrategyInterface $strategy): void
40
    {
41
        static::$strategy = $strategy;
42
    }
43
44
    /**
45
     * Get the current asset strategy adopted.
46
     *
47
     * @return \BEdita\WebTools\Utility\Asset\AssetStrategyInterface|null
48
     */
49
    public static function getStrategy(): ?AssetStrategyInterface
50
    {
51
        return static::$strategy;
52
    }
53
54
    /**
55
     * Clear asset strategy.
56
     *
57
     * @return void
58
     */
59
    public static function clearStrategy(): void
60
    {
61
        static::$strategy = null;
62
    }
63
64
    /**
65
     * Retrieve asset name or an array of assets .
66
     * Return canonical asset name if no assets was found.
67
     *
68
     * @param string $name Canonical asset name
69
     * @param string $extension Optional extension to use to search asset, like 'js' or 'css'
70
     * @return string|array
71
     */
72
    public static function get(string $name, ?string $extension = null)
73
    {
74
        $strategy = static::getStrategy();
75
        if ($strategy === null) {
76
            return $name;
77
        }
78
79
        $asset = $strategy->get($name, $extension);
80
        if (!empty($asset)) {
81
            return $asset;
82
        }
83
84
        return $name;
85
    }
86
87
    /**
88
     * Retrieve asset names array via ::get() call
89
     *
90
     * @param array $names Canonical asset names
91
     * @param string $extension Optional extension to use to search asset, like 'js' or 'css'
92
     * @return array
93
     */
94
    public static function getMulti(array $names, ?string $extension = null): array
95
    {
96
        $assets = [];
97
        foreach ($names as $val) {
98
            $assets = array_merge($assets, (array)static::get($val, $extension));
99
        }
100
101
        return $assets;
102
    }
103
104
    /**
105
     * Load assets for adopted asset strategy.
106
     *
107
     * @param string $path Manifest file path
108
     * @return void
109
     * @throws \LogicException If startegy is not defined
110
     */
111
    public static function loadManifest(?string $path = null): void
112
    {
113
        $strategy = static::getStrategy();
114
        if ($strategy === null) {
115
            throw new \LogicException('Missing asset strategy');
116
        }
117
118
        $strategy->loadAssets($path);
119
    }
120
}
121