Passed
Pull Request — master (#143)
by Arman
05:00 queued 02:30
created

AssetManager::publish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Asset;
16
17
use Quantum\Exceptions\AssetException;
18
use Quantum\Exceptions\LangException;
19
20
/**
21
 * Class AssetManager
22
 * @package Quantum\Libraries\Asset
23
 */
24
class AssetManager
25
{
26
27
    /**
28
     * CSS assets store
29
     */
30
    const CSS_STORE = 1;
31
32
    /**
33
     * JS assets store
34
     */
35
    const JS_STORE = 2;
36
37
    /**
38
     *  Asset store
39
     * @var Asset[]
40
     */
41
    private $store = [];
42
43
    /**
44
     * Published assets
45
     * @var array[]
46
     */
47
    private $published = [
48
        self::CSS_STORE => [],
49
        self::JS_STORE => []
50
    ];
51
52
    /**
53
     * Asset instance
54
     * @var AssetManager|null
55
     */
56
    private static $instance = null;
57
58
    private function __construct()
59
    {
60
    }
61
62
    /**
63
     * AssetManager instance
64
     * @return AssetManager|null
65
     */
66
    public static function getInstance(): ?AssetManager
67
    {
68
        if (self::$instance == null) {
69
            self::$instance = new self();
70
        }
71
72
        return self::$instance;
73
    }
74
75
    /**
76
     * Gets the asset by name
77
     * @param string $name
78
     * @return Asset|null
79
     */
80
    public function get(string $name): ?Asset
81
    {
82
        foreach ($this->store as $asset) {
83
            if ($asset->getName() == $name) {
84
                return $asset;
85
            }
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * Asset url
93
     * @param string $path
94
     * @return string
95
     */
96
    public function url(string $path): string
97
    {
98
        if (!parse_url($path, PHP_URL_HOST)) {
99
            return base_url() . '/assets/' . $path;
100
        }
101
102
        return $path;
103
    }
104
105
    /**
106
     * Register assets
107
     * @param Asset[] $assets
108
     * @throws AssetException
109
     * @throws LangException
110
     */
111
    public function register(array $assets)
112
    {
113
        foreach ($assets as $asset) {
114
            $this->registerAsset($asset);
115
        }
116
    }
117
118
    /**
119
     * Register single asset
120
     * @param Asset $asset
121
     * @throws AssetException
122
     * @throws LangException
123
     */
124
    public function registerAsset(Asset $asset)
125
    {
126
        if ($asset->getName()) {
127
            foreach ($this->store as $storedAsset) {
128
                if ($storedAsset->getName() == $asset->getName()) {
129
                    throw AssetException::nameInUse($asset->getName());
130
                }
131
            }
132
        }
133
134
        $this->store[] = $asset;
135
    }
136
137
    /**
138
     * @return void
139
     */
140
    public function flush() {
141
        $this->store = [];
142
    }
143
144
    /**
145
     * Dumps the assets
146
     * @param int $type
147
     * @throws AssetException
148
     * @throws LangException
149
     */
150
    public function dump(int $type): void
151
    {
152
        if (empty($this->published[$type])) {
153
            $this->publish();
154
        }
155
156
        if ($this->published && count($this->published[$type])) {
157
            foreach ($this->published[$type] as $asset) {
158
                echo $asset->tag();
159
            }
160
        }
161
    }
162
163
    /**
164
     * Publishes assets
165
     * @throws AssetException
166
     * @throws LangException
167
     */
168
    private function publish()
169
    {
170
        if (!empty($this->store)) {
171
            $this->setPriorityAssets();
172
            $this->setRegularAssets();
173
174
            ksort($this->published[self::CSS_STORE]);
175
            ksort($this->published[self::JS_STORE]);
176
        }
177
    }
178
179
    /**
180
     * Sets assets with ordered position
181
     * @throws AssetException
182
     * @throws LangException
183
     */
184
    private function setPriorityAssets()
185
    {
186
        foreach ($this->store as $asset) {
187
            if ($asset->getPosition() != -1) {
188
                if (isset($this->published[$asset->getType()][$asset->getPosition()])) {
189
                    throw AssetException::positionInUse($asset->getPosition(), $asset->getPath());
190
                }
191
192
                $this->published[$asset->getType()][$asset->getPosition()] = $asset;
193
            }
194
        }
195
    }
196
197
    /**
198
     * Sets assets without ordered position
199
     */
200
    private function setRegularAssets()
201
    {
202
        foreach ($this->store as $asset) {
203
            if ($asset->getPosition() == -1) {
204
                $this->setPosition($asset, 0);
205
            }
206
        }
207
    }
208
209
    /**
210
     * Sets the Position
211
     * @param Asset $asset
212
     * @param int $index
213
     */
214
    private function setPosition(Asset $asset, int $index)
215
    {
216
        if (isset($this->published[$asset->getType()][$index])) {
217
            $this->setPosition($asset, $index + 1);
218
        } else {
219
            $this->published[$asset->getType()][$index] = $asset;
220
        }
221
    }
222
}
223