1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values\assets; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\DomainInterface; |
6
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Asset |
10
|
|
|
* Value Object for providing details about a registrable asset |
11
|
|
|
* |
12
|
|
|
* @package EventEspresso\core\domain\values |
13
|
|
|
* @author Brent Christensen |
14
|
|
|
* @since 4.9.62.p |
15
|
|
|
*/ |
16
|
|
|
abstract class Asset |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* indicates the file extension for a build distribution CSS file |
21
|
|
|
*/ |
22
|
|
|
const FILE_EXTENSION_DISTRIBUTION_CSS = '.dist.css'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* indicates the file extension for a build distribution JS file |
26
|
|
|
*/ |
27
|
|
|
const FILE_EXTENSION_DISTRIBUTION_JS = '.dist.js'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Indicates the file extension for a build distribution dependencies json file. |
31
|
|
|
*/ |
32
|
|
|
const FILE_EXTENSION_DISTRIBUTION_DEPS = '.dist.deps.php'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* indicates a Cascading Style Sheet asset |
36
|
|
|
*/ |
37
|
|
|
const TYPE_CSS = 'css'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* indicates a Javascript asset |
41
|
|
|
*/ |
42
|
|
|
const TYPE_JS = 'js'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* indicates a JSON asset |
46
|
|
|
*/ |
47
|
|
|
CONST TYPE_JSON = 'json'; |
48
|
|
|
/** |
49
|
|
|
* indicates a PHP asset |
50
|
|
|
*/ |
51
|
|
|
CONST TYPE_PHP = 'php'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* indicates a Javascript manifest file |
55
|
|
|
*/ |
56
|
|
|
const TYPE_MANIFEST = 'manifest'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var DomainInterface $domain |
60
|
|
|
*/ |
61
|
|
|
protected $domain; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string $type |
65
|
|
|
*/ |
66
|
|
|
private $type; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string $handle |
70
|
|
|
*/ |
71
|
|
|
private $handle; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var bool $registered |
75
|
|
|
*/ |
76
|
|
|
private $registered = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var bool $enqueue_immediately |
80
|
|
|
*/ |
81
|
|
|
private $enqueue_immediately = false; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Asset constructor. |
86
|
|
|
* |
87
|
|
|
* @param $type |
88
|
|
|
* @param string $handle |
89
|
|
|
* @param DomainInterface $domain |
90
|
|
|
* @throws InvalidDataTypeException |
91
|
|
|
*/ |
92
|
|
|
public function __construct($type, $handle, DomainInterface $domain) |
93
|
|
|
{ |
94
|
|
|
$this->domain = $domain; |
95
|
|
|
$this->setType($type); |
96
|
|
|
$this->setHandle($handle); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function validAssetTypes() |
104
|
|
|
{ |
105
|
|
|
return array( |
106
|
|
|
Asset::TYPE_CSS, |
107
|
|
|
Asset::TYPE_JS, |
108
|
|
|
Asset::TYPE_MANIFEST, |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $type |
115
|
|
|
* @throws InvalidDataTypeException |
116
|
|
|
*/ |
117
|
|
|
private function setType($type) |
118
|
|
|
{ |
119
|
|
|
if (! in_array($type, $this->validAssetTypes(), true)) { |
120
|
|
|
throw new InvalidDataTypeException( |
121
|
|
|
'Asset::$type', |
122
|
|
|
$type, |
123
|
|
|
'one of the TYPE_* class constants on \EventEspresso\core\domain\values\Asset is required' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
$this->type = $type; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $handle |
132
|
|
|
* @throws InvalidDataTypeException |
133
|
|
|
*/ |
134
|
|
|
private function setHandle($handle) |
135
|
|
|
{ |
136
|
|
|
if (! is_string($handle)) { |
137
|
|
|
throw new InvalidDataTypeException( |
138
|
|
|
'$handle', |
139
|
|
|
$handle, |
140
|
|
|
'string' |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
$this->handle = $handle; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function assetNamespace() |
151
|
|
|
{ |
152
|
|
|
return $this->domain->assetNamespace(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function type() |
160
|
|
|
{ |
161
|
|
|
return $this->type; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function handle() |
169
|
|
|
{ |
170
|
|
|
return $this->handle; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function isRegistered() |
177
|
|
|
{ |
178
|
|
|
return $this->registered; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param bool $registered |
183
|
|
|
*/ |
184
|
|
|
public function setRegistered($registered = true) |
185
|
|
|
{ |
186
|
|
|
$this->registered = filter_var($registered, FILTER_VALIDATE_BOOLEAN); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function enqueueImmediately() |
194
|
|
|
{ |
195
|
|
|
return $this->enqueue_immediately; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param bool $enqueue_immediately |
201
|
|
|
*/ |
202
|
|
|
public function setEnqueueImmediately($enqueue_immediately = true) |
203
|
|
|
{ |
204
|
|
|
$this->enqueue_immediately = filter_var($enqueue_immediately, FILTER_VALIDATE_BOOLEAN); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|