1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\mvc\assets; |
4
|
|
|
|
5
|
|
|
abstract class AbstractAsset |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
public $useLinks = true; |
9
|
|
|
|
10
|
|
|
public $publish = true; |
11
|
|
|
|
12
|
|
|
/** @var AbstractAsset[] */ |
13
|
|
|
protected $assetsBefore = []; |
14
|
|
|
|
15
|
|
|
/** @var AbstractAsset[] */ |
16
|
|
|
protected $assetsAfter = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
abstract public function getBasePath(): string; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
abstract public function css(): array; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
abstract public function js(): array; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
final public function internalCss(): array |
37
|
|
|
{ |
38
|
|
|
return $this->getInternal($this->css()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
final public function externalCss(): array |
45
|
|
|
{ |
46
|
|
|
return $this->getExternal($this->css()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
final public function internalJs(): array |
53
|
|
|
{ |
54
|
|
|
return $this->getInternal($this->js()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
final public function externalJs(): array |
61
|
|
|
{ |
62
|
|
|
return $this->getExternal($this->js()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getHash(): string |
69
|
|
|
{ |
70
|
|
|
return \md5(static::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getAssetsBefore(): array |
77
|
|
|
{ |
78
|
|
|
return $this->assetsBefore; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getAssetsAfter(): array |
85
|
|
|
{ |
86
|
|
|
return $this->assetsAfter; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param AbstractAsset $asset |
91
|
|
|
*/ |
92
|
|
|
public function addAssetBefore(AbstractAsset $asset) |
93
|
|
|
{ |
94
|
|
|
$this->assetsBefore[\get_class($asset)] = $asset; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param AbstractAsset $asset |
99
|
|
|
*/ |
100
|
|
|
public function addAssetAfter(AbstractAsset $asset) |
101
|
|
|
{ |
102
|
|
|
$this->assetsAfter[\get_class($asset)] = $asset; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $files |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
private function getInternal(array $files): array |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$result = []; |
113
|
|
|
foreach ($files as $file) { |
114
|
|
|
if ($this->isInternal($file)) { |
115
|
|
|
$result[] = $file; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $files |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
private function getExternal(array $files): array |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$result = []; |
129
|
|
|
foreach ($files as $file) { |
130
|
|
|
if (!$this->isInternal($file)) { |
131
|
|
|
$result[] = $file; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $file |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
private function isInternal(string $file): bool |
143
|
|
|
{ |
144
|
|
|
return (false === \strpos($file, 'http') && false === \strpos($file, '//')); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.