1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Asset\PreProcessor;
use Magento\Framework\View\Asset\LocalInterface;
/**
* An object that's passed to preprocessors to carry current and original information for processing
* Encapsulates complexity of all necessary context and parameters
*
* @api
* @since 100.0.2
*/
class Chain
{
/**
* @var array
*/
private $compatibleTypes;
/**
* @var LocalInterface
*/
private $asset;
/**
* @var string
*/
private $origContent;
/**
* @var string
*/
protected $origContentType;
/**
* @var string
*/
private $content;
/**
* @var string
*/
private $contentType;
/**
* @var string
*/
protected $targetContentType;
/**
* @var null|string
*/
protected $targetAssetPath;
/**
* @var string
*/
protected $origAssetPath;
/**
* @param LocalInterface $asset
* @param string $origContent
* @param string $origContentType
* @param string $origAssetPath
* @param array $compatibleTypes
*/
public function __construct(
LocalInterface $asset,
$origContent,
$origContentType,
$origAssetPath,
array $compatibleTypes = []
) {
$this->asset = $asset;
$this->origContent = $origContent;
$this->content = $origContent;
$this->origContentType = $origContentType;
$this->contentType = $origContentType;
$this->targetContentType = $asset->getContentType();
$this->targetAssetPath = $asset->getPath();
$this->origAssetPath = $origAssetPath;
$this->compatibleTypes = $compatibleTypes;
}
/**
* Get asset object
*
* @return LocalInterface
*/
public function getAsset()
{
return $this->asset;
}
/**
* Get original content
*
* @return string
*/
public function getOrigContent()
{
return $this->origContent;
}
/**
* Get current content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set current content
*
* @param string $content
* @return void
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* Get original content type
*
* @return string
*/
public function getOrigContentType()
{
return $this->origContentType;
}
/**
* Get current content type
*
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Set current content type
*
* @param string $contentType
* @return void
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
}
/**
* Get the intended content type
*
* @return string
*/
public function getTargetContentType()
{
return $this->targetContentType;
}
/**
* Get the target asset path
*
* @return string
*/
public function getTargetAssetPath()
{
return $this->targetAssetPath;
}
/**
* Assert invariants
*
* Impose an integrity check to avoid generating mismatching content type and not leaving transient data behind
*
* @return void
* @throws \LogicException
*/
public function assertValid()
{
if ($this->contentType !== $this->targetContentType
&& empty($this->compatibleTypes[$this->targetContentType][$this->contentType])) {
throw new \LogicException(
"The requested asset type was '{$this->targetContentType}', but ended up with '{$this->contentType}'"
);
}
}
/**
* Whether the contents or type have changed during the lifetime of the object
*
* @return bool
*/
public function isChanged()
{
return $this->origContentType != $this->contentType || $this->origContent != $this->content;
}
/**
* @return string
* @codeCoverageIgnore
*/
public function getOrigAssetPath()
{
return $this->origAssetPath;
}
}