1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values\assets; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use EventEspresso\core\domain\DomainInterface; |
7
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BrowserAsset |
11
|
|
|
* An asset that gets sent to the browser such as a Javascript or CSS |
12
|
|
|
* |
13
|
|
|
* @package EventEspresso\core\domain\values\assets |
14
|
|
|
* @author Brent Christensen |
15
|
|
|
* @since 4.9.62.p |
16
|
|
|
*/ |
17
|
|
|
abstract class BrowserAsset extends Asset |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $source |
22
|
|
|
*/ |
23
|
|
|
private $source; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array $dependencies |
27
|
|
|
*/ |
28
|
|
|
private $dependencies; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string $version |
32
|
|
|
*/ |
33
|
|
|
private $version; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Asset constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $type |
40
|
|
|
* @param string $handle |
41
|
|
|
* @param string $source |
42
|
|
|
* @param array $dependencies |
43
|
|
|
* @param DomainInterface $domain |
44
|
|
|
* @throws InvalidDataTypeException |
45
|
|
|
*/ |
46
|
|
|
public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain) |
47
|
|
|
{ |
48
|
|
|
parent::__construct($type, $handle, $domain); |
49
|
|
|
$this->setSource($source); |
50
|
|
|
$this->setDependencies($dependencies); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 4.9.62.p |
56
|
|
|
*/ |
57
|
|
|
abstract public function enqueueAsset(); |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function dependencies() |
64
|
|
|
{ |
65
|
|
|
return $this->dependencies; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $dependencies |
71
|
|
|
*/ |
72
|
|
|
private function setDependencies(array $dependencies) |
73
|
|
|
{ |
74
|
|
|
$this->dependencies = $dependencies; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @since 4.9.62.p |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function hasDependencies() |
83
|
|
|
{ |
84
|
|
|
return count($this->dependencies) > 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function source() |
92
|
|
|
{ |
93
|
|
|
return $this->source; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $source |
99
|
|
|
* @throws InvalidDataTypeException |
100
|
|
|
*/ |
101
|
|
|
private function setSource($source) |
102
|
|
|
{ |
103
|
|
|
if (! is_string($source)) { |
104
|
|
|
throw new InvalidDataTypeException( |
105
|
|
|
'$source', |
106
|
|
|
$source, |
107
|
|
|
'string' |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
$this->source = $source; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
* @throws InvalidDataTypeException |
117
|
|
|
* @throws DomainException |
118
|
|
|
*/ |
119
|
|
|
public function version() |
120
|
|
|
{ |
121
|
|
|
// if version is NOT set and this asset was NOT built for distribution, |
122
|
|
|
// then set the version equal to the EE core plugin version |
123
|
|
|
if ($this->version === null && ! $this->isBuiltDistributionSource()) { |
124
|
|
|
$this->setVersion(); |
125
|
|
|
} |
126
|
|
|
return $this->version; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $version |
132
|
|
|
* @return BrowserAsset |
133
|
|
|
* @throws InvalidDataTypeException |
134
|
|
|
* @throws DomainException |
135
|
|
|
*/ |
136
|
|
|
public function setVersion($version = '') |
137
|
|
|
{ |
138
|
|
|
if (! is_string($version)) { |
139
|
|
|
throw new InvalidDataTypeException( |
140
|
|
|
'$version', |
141
|
|
|
$version, |
142
|
|
|
'string' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
if ( $version === '' ) { |
146
|
|
|
$version = $this->domain->version(); |
147
|
|
|
} |
148
|
|
|
$this->version = $version; |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isBuiltDistributionSource() { |
157
|
|
|
return substr($this->source, -8) === Asset::FILE_EXTENSION_DISTRIBUTION_JS |
158
|
|
|
|| substr($this->source, -9) === Asset::FILE_EXTENSION_DISTRIBUTION_CSS; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|