1
|
|
|
<?php namespace Modules\Core\Foundation\Asset\Pipeline; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
4
|
|
|
use Modules\Core\Foundation\Asset\AssetNotFoundException; |
5
|
|
|
use Modules\Core\Foundation\Asset\Manager\AssetManager; |
6
|
|
|
|
7
|
|
|
class AsgardAssetPipeline implements AssetPipeline |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Collection |
11
|
|
|
*/ |
12
|
|
|
protected $css; |
13
|
|
|
/** |
14
|
|
|
* @var Collection |
15
|
|
|
*/ |
16
|
|
|
protected $js; |
17
|
|
|
|
18
|
|
|
public function __construct(AssetManager $assetManager) |
19
|
|
|
{ |
20
|
|
|
$this->css = new Collection(); |
21
|
|
|
$this->js = new Collection(); |
22
|
|
|
$this->assetManager = $assetManager; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add a javascript dependency on the view |
27
|
|
|
* @param string $dependency |
28
|
|
|
* @return $this |
29
|
|
|
* @throws AssetNotFoundException |
30
|
|
|
*/ |
31
|
|
|
public function requireJs($dependency) |
32
|
|
|
{ |
33
|
|
|
if (is_array($dependency)) { |
34
|
|
|
foreach ($dependency as $dependency) { |
35
|
|
|
$this->requireJs($dependency); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$assetPath = $this->assetManager->getJs($dependency); |
40
|
|
|
|
41
|
|
|
$this->guardForAssetNotFound($assetPath); |
42
|
|
|
|
43
|
|
|
$this->js->put($dependency, $assetPath); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add a CSS dependency on the view |
50
|
|
|
* @param string $dependency |
51
|
|
|
* @return $this |
52
|
|
|
* @throws AssetNotFoundException |
53
|
|
|
*/ |
54
|
|
|
public function requireCss($dependency) |
55
|
|
|
{ |
56
|
|
|
if (is_array($dependency)) { |
57
|
|
|
foreach ($dependency as $dependency) { |
58
|
|
|
$this->requireCss($dependency); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$assetPath = $this->assetManager->getCss($dependency); |
63
|
|
|
|
64
|
|
|
$this->guardForAssetNotFound($assetPath); |
65
|
|
|
|
66
|
|
|
$this->css->put($dependency, $assetPath); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add the dependency after another one |
73
|
|
|
* @param string $dependency |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function after($dependency) |
77
|
|
|
{ |
78
|
|
|
$this->insert($dependency, 'after'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add the dependency before another one |
83
|
|
|
* @param string $dependency |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function before($dependency) |
87
|
|
|
{ |
88
|
|
|
$this->insert($dependency, 'before'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Insert a dependency before or after in the right dependency array |
93
|
|
|
* @param string $dependency |
94
|
|
|
* @param string $offset |
95
|
|
|
*/ |
96
|
|
|
private function insert($dependency, $offset = 'before') |
97
|
|
|
{ |
98
|
|
|
$offset = $offset == 'before' ? 0 : 1; |
99
|
|
|
|
100
|
|
|
list($dependencyArray, $collectionName) = $this->findDependenciesForKey($dependency); |
101
|
|
|
list($key, $value) = $this->getLastKeyAndValueOf($dependencyArray); |
102
|
|
|
|
103
|
|
|
$pos = $this->getPositionInArray($dependency, $dependencyArray); |
104
|
|
|
|
105
|
|
|
$dependencyArray = array_merge( |
106
|
|
|
array_slice($dependencyArray, 0, $pos + $offset, true), |
107
|
|
|
[$key => $value], |
108
|
|
|
array_slice($dependencyArray, $pos, count($dependencyArray) - 1, true) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->$collectionName = new Collection($dependencyArray); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return all css files to include |
116
|
|
|
* @return \Illuminate\Support\Collection |
117
|
|
|
*/ |
118
|
|
|
public function allCss() |
119
|
|
|
{ |
120
|
|
|
return $this->css; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return all js files to include |
125
|
|
|
* @return \Illuminate\Support\Collection |
126
|
|
|
*/ |
127
|
|
|
public function allJs() |
128
|
|
|
{ |
129
|
|
|
return $this->js; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Find in which collection the given dependency exists |
134
|
|
|
* @param string $dependency |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
private function findDependenciesForKey($dependency) |
138
|
|
|
{ |
139
|
|
|
if ($this->css->get($dependency)) { |
140
|
|
|
return [$this->css->toArray(), 'css']; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return [$this->js->toArray(), 'js']; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the last key and value the given array |
148
|
|
|
* @param array $dependencyArray |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
private function getLastKeyAndValueOf(array $dependencyArray) |
152
|
|
|
{ |
153
|
|
|
$value = end($dependencyArray); |
154
|
|
|
$key = key($dependencyArray); |
155
|
|
|
reset($dependencyArray); |
156
|
|
|
|
157
|
|
|
return [$key, $value]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the position in the array of the given key |
162
|
|
|
* |
163
|
|
|
* @param $dependency |
164
|
|
|
* @param array $dependencyArray |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
|
|
private function getPositionInArray($dependency, array $dependencyArray) |
168
|
|
|
{ |
169
|
|
|
$pos = array_search($dependency, array_keys($dependencyArray)); |
170
|
|
|
|
171
|
|
|
return $pos; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* If asset was not found, throw an exception |
176
|
|
|
* @param string $assetPath |
177
|
|
|
* @throws AssetNotFoundException |
178
|
|
|
*/ |
179
|
|
|
private function guardForAssetNotFound($assetPath) |
180
|
|
|
{ |
181
|
|
|
if (is_null($assetPath)) { |
182
|
|
|
throw new AssetNotFoundException($assetPath); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: