File tree 7 files changed +105
-2
lines changed
7 files changed +105
-2
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace AmpProject \Optimizer \Exception ;
4
+
5
+ use DomainException ;
6
+
7
+ /**
8
+ * Exception thrown when an invalid transformer sequence is provided
9
+ * or missed transformer which required by another transformer.
10
+ *
11
+ * @package ampproject/amp-toolbox
12
+ */
13
+ final class InvalidTransformersSequence extends DomainException implements AmpOptimizerException
14
+ {
15
+
16
+ /**
17
+ * @param string $transformerClass
18
+ * @param array $missingTags
19
+ * @return \AmpProject\Optimizer\Exception\InvalidTransformersSequence
20
+ */
21
+ public static function forTransformer ($ transformerClass , array $ missingTags )
22
+ {
23
+ $ tagList = implode (', ' , $ missingTags );
24
+ $ message = "Transformer ' {$ transformerClass }' must be after transformers that provide ' {$ tagList }' tags. " ;
25
+
26
+ return new self ($ message );
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -88,6 +88,8 @@ private function initializeTransformers()
88
88
{
89
89
$ this ->transformers = [];
90
90
91
+ TransformersSequenceValidator::validate ($ this ->configuration ->get (Configuration::KEY_TRANSFORMERS ));
92
+
91
93
foreach ($ this ->configuration ->get (Configuration::KEY_TRANSFORMERS ) as $ transformerClass ) {
92
94
$ this ->transformers [$ transformerClass ] = new $ transformerClass (
93
95
...$ this ->getTransformerDependencies ($ transformerClass )
Original file line number Diff line number Diff line change 17
17
use AmpProject \Optimizer \ImageDimensions ;
18
18
use AmpProject \Optimizer \Transformer ;
19
19
use AmpProject \Optimizer \TransformerConfiguration ;
20
+ use AmpProject \Optimizer \TransformerProvides ;
20
21
use AmpProject \RequestDestination ;
21
22
use AmpProject \Tag ;
22
23
use AmpProject \Url ;
42
43
*
43
44
* @package ampproject/amp-toolbox
44
45
*/
45
- final class PreloadHeroImage implements Transformer
46
+ final class PreloadHeroImage implements Transformer, TransformerProvides
46
47
{
47
48
49
+ const TAG = 'preload_hero_image ' ;
50
+
48
51
/**
49
52
* Class(es) to apply to a serverside-rendered image element.
50
53
*
@@ -158,6 +161,11 @@ public function __construct(TransformerConfiguration $configuration)
158
161
$ this ->configuration = $ configuration ;
159
162
}
160
163
164
+ public static function provides ()
165
+ {
166
+ return [self ::TAG ];
167
+ }
168
+
161
169
/**
162
170
* Apply transformations to the provided DOM document.
163
171
*
Original file line number Diff line number Diff line change 17
17
use AmpProject \Optimizer \Exception \InvalidArgument ;
18
18
use AmpProject \Optimizer \Exception \InvalidHtmlAttribute ;
19
19
use AmpProject \Optimizer \Transformer ;
20
+ use AmpProject \Optimizer \TransformerRequires ;
20
21
use AmpProject \Role ;
21
22
use AmpProject \Tag ;
22
23
use DOMAttr ;
37
38
*
38
39
* @package ampproject/amp-toolbox
39
40
*/
40
- final class ServerSideRendering implements Transformer
41
+ final class ServerSideRendering implements Transformer, TransformerRequires
41
42
{
42
43
44
+ const TAG = 'server_side_rendering ' ;
45
+
43
46
/**
44
47
* List of layouts that support server-side rendering.
45
48
*
@@ -94,6 +97,11 @@ final class ServerSideRendering implements Transformer
94
97
*/
95
98
private $ customCss ;
96
99
100
+ public static function requires ()
101
+ {
102
+ return [PreloadHeroImage::TAG ];
103
+ }
104
+
97
105
/**
98
106
* Apply transformations to the provided DOM document.
99
107
*
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace AmpProject \Optimizer ;
4
+
5
+ interface TransformerProvides
6
+ {
7
+
8
+ /**
9
+ * Return the collection of dependencies that this transformer provides.
10
+ *
11
+ * @return string[]
12
+ */
13
+ public static function provides ();
14
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace AmpProject \Optimizer ;
4
+
5
+ interface TransformerRequires
6
+ {
7
+
8
+ /**
9
+ * Return the collection of dependencies that this transformer requires.
10
+ *
11
+ * @return string[]
12
+ */
13
+ public static function requires ();
14
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace AmpProject \Optimizer ;
4
+
5
+ use AmpProject \Optimizer \Exception \InvalidTransformersSequence ;
6
+
7
+ class TransformersSequenceValidator
8
+ {
9
+
10
+ /**
11
+ * @param string[] $transformers
12
+ */
13
+ public static function validate (array $ transformers )
14
+ {
15
+ $ providedTags = [];
16
+ foreach (array_values ($ transformers ) as $ transformerClass ) {
17
+ if ($ transformerClass instanceof TransformerRequires) {
18
+ $ missingTags = array_diff ($ transformerClass ::requires (), $ providedTags );
19
+ if ($ missingTags ) {
20
+ throw InvalidTransformersSequence::forTransformer ($ transformerClass , $ missingTags );
21
+ }
22
+ }
23
+
24
+ if ($ transformerClass instanceof TransformerProvides) {
25
+ $ providedTags = array_merge ($ providedTags , $ transformerClass ::provides ());
26
+ }
27
+ }
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments