-
Notifications
You must be signed in to change notification settings - Fork 25
/
TransformationEngine.php
175 lines (152 loc) · 5.76 KB
/
TransformationEngine.php
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
<?php
namespace AmpProject\Optimizer;
use AmpProject\Dom\Document;
use AmpProject\RemoteGetRequest;
use AmpProject\RemoteRequest\CurlRemoteGetRequest;
use AmpProject\RemoteRequest\TemporaryFileCachedRemoteGetRequest;
use AmpProject\Validator\Spec;
use ReflectionClass;
use ReflectionException;
/**
* Transformation engine that accepts HTML and returns optimized HTML.
*
* @package ampproject/amp-toolbox
*/
final class TransformationEngine
{
/**
* Internal storage for the configuration settings.
*
* @var Configuration
*/
private $configuration;
/**
* Transport to use for remote requests.
*
* @var RemoteGetRequest
*/
private $remoteRequest;
/**
* Collection of transformers that were initialized.
*
* @var Transformer[]
*/
private $transformers;
/**
* Validator Spec instance to use.
*
* @var Spec
*/
private $spec;
/**
* Instantiate a TransformationEngine object.
*
* @param Configuration|null $configuration Optional. Configuration data to use for setting up the transformers.
* @param RemoteGetRequest|null $remoteRequest Optional. Transport to use for remote requests. Defaults to the
* CurlRemoteGetRequest implementation shipped with the library.
* @param Spec|null $spec Optional. Validator spec instance to use.
*/
public function __construct(
?Configuration $configuration = null,
?RemoteGetRequest $remoteRequest = null,
?Spec $spec = null
) {
$this->configuration = $configuration ?? new DefaultConfiguration();
$this->remoteRequest = $remoteRequest;
$this->spec = $spec;
$this->initializeTransformers();
}
/**
* Apply transformations to the provided DOM document.
*
* @param Document $document DOM document to apply the transformations to.
* @param ErrorCollection $errors Collection of errors that are collected during transformation.
* @return void
*/
public function optimizeDom(Document $document, ErrorCollection $errors)
{
foreach ($this->transformers as $transformer) {
$transformer->transform($document, $errors);
}
}
/**
* Apply transformations to the provided string of HTML markup.
*
* @param string $html HTML markup to apply the transformations to.
* @param ErrorCollection $errors Collection of errors that are collected during transformation.
* @return string Optimized HTML string.
*/
public function optimizeHtml($html, ErrorCollection $errors)
{
$dom = Document::fromHtml($html);
$this->optimizeDom($dom, $errors);
return $dom->saveHTML();
}
/**
* Initialize the array of transformers to use.
*/
private function initializeTransformers()
{
$this->transformers = [];
foreach ($this->configuration->get(Configuration::KEY_TRANSFORMERS) as $transformerClass) {
$this->transformers[$transformerClass] = new $transformerClass(
...$this->getTransformerDependencies($transformerClass)
);
}
}
/**
* Get the dependencies of a transformer and put them in the correct order.
*
* @param string $transformerClass Class of the transformer to get the dependencies for.
* @return array Array of dependencies in the order as they appear in the transformer's constructor.
* @throws ReflectionException If the transformer could not be reflected upon.
*/
private function getTransformerDependencies($transformerClass)
{
$constructor = (new ReflectionClass($transformerClass))->getConstructor();
if ($constructor === null) {
return [];
}
$dependencies = [];
foreach ($constructor->getParameters() as $parameter) {
$dependencyType = null;
// The use of `ReflectionParameter::getClass()` is deprecated in PHP 8, and is superseded
// by `ReflectionParameter::getType()`. See https://github.com/php/php-src/pull/5209.
if (PHP_VERSION_ID >= 70100) {
if ($parameter->getType()) {
/** @var \ReflectionNamedType $returnType */
$returnType = $parameter->getType();
$dependencyType = new ReflectionClass($returnType->getName());
}
} else {
$dependencyType = $parameter->getClass();
}
if ($dependencyType === null) {
// No type provided, so we pass `null` in the hopes that the argument is optional.
$dependencies[] = null;
continue;
}
if (is_a($dependencyType->name, TransformerConfiguration::class, true)) {
$dependencies[] = $this->configuration->getTransformerConfiguration($transformerClass);
continue;
}
if (is_a($dependencyType->name, RemoteGetRequest::class, true)) {
if ($this->remoteRequest === null) {
$this->remoteRequest = new TemporaryFileCachedRemoteGetRequest(new CurlRemoteGetRequest());
}
$dependencies[] = $this->remoteRequest;
continue;
}
if (is_a($dependencyType->name, Spec::class, true)) {
if ($this->spec === null) {
$this->spec = new Spec();
}
$dependencies[] = $this->spec;
continue;
}
// Unknown dependency type, so we pass `null` in the hopes that the argument is optional.
$dependencies[] = null;
}
return $dependencies;
}
}