-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDefaultConfiguration.php
164 lines (147 loc) · 6.01 KB
/
DefaultConfiguration.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
<?php
namespace AmpProject\Optimizer;
use AmpProject\Optimizer\Exception\InvalidConfigurationValue;
use AmpProject\Optimizer\Exception\UnknownConfigurationClass;
use AmpProject\Optimizer\Exception\UnknownConfigurationKey;
/**
* Configuration object that validates and stores configuration settings.
*
* @package ampproject/amp-toolbox
*/
class DefaultConfiguration implements Configuration
{
/**
* Associative array of already validated configuration settings.
*
* @var array
*/
protected $configuration;
/**
* Associative array mapping the transformer classes to their configuration classes.
*
* This can be extended by third-parties via:
*
* @see registerConfigurationClass()
*
* @var array
*/
protected $transformerConfigurationClasses = [
Transformer\AmpRuntimeCss::class => Configuration\AmpRuntimeCssConfiguration::class,
Transformer\AutoExtensions::class => Configuration\AutoExtensionsConfiguration::class,
Transformer\OptimizeAmpBind::class => Configuration\OptimizeAmpBindConfiguration::class,
Transformer\OptimizeHeroImages::class => Configuration\OptimizeHeroImagesConfiguration::class,
Transformer\PreloadHeroImage::class => Configuration\PreloadHeroImageConfiguration::class,
Transformer\RewriteAmpUrls::class => Configuration\RewriteAmpUrlsConfiguration::class,
Transformer\TransformedIdentifier::class => Configuration\TransformedIdentifierConfiguration::class,
Transformer\OptimizeViewport::class => Configuration\OptimizeViewportConfiguration::class,
Transformer\MinifyHtml::class => Configuration\MinifyHtmlConfiguration::class,
Transformer\AmpStoryCssOptimizer::class => Configuration\AmpStoryCssOptimizerConfiguration::class,
];
/**
* Instantiate a Configuration object.
*
* @param array $configurationData Optional. Associative array of configuration data to use. This will be merged
* with the default configuration and take precedence.
*/
public function __construct($configurationData = [])
{
$this->configuration = array_merge(
static::DEFAULTS,
$this->validateConfigurationKeys($configurationData)
);
}
/**
* Register a new configuration class to use for a given transformer.
*
* @param string $transformerClass FQCN of the transformer to register a configuration class for.
* @param string $configurationClass FQCN of the configuration to use.
*/
public function registerConfigurationClass($transformerClass, $configurationClass)
{
$this->transformerConfigurationClasses[$transformerClass] = $configurationClass;
}
/**
* Validate an array of configuration settings.
*
* @param array $configurationData Associative array of configuration data to validate.
* @return array Associative array of validated configuration data.
*/
protected function validateConfigurationKeys($configurationData)
{
foreach ($configurationData as $key => $value) {
$configurationData[$key] = $this->validate($key, $value);
}
return $configurationData;
}
/**
* Validate an individual configuration setting.
*
* @param string $key Key of the configuration setting.
* @param mixed $value Value of the configuration setting.
* @return mixed Validated value for the provided configuration setting.
* @throws InvalidConfigurationValue If the configuration value could not be validated.
*/
protected function validate($key, $value)
{
switch ($key) {
case Configuration::KEY_TRANSFORMERS:
if (! is_array($value)) {
throw InvalidConfigurationValue::forInvalidValueType(
Configuration::KEY_TRANSFORMERS,
'array',
gettype($value)
);
}
foreach ($value as $index => $entry) {
if (! is_string($entry)) {
throw InvalidConfigurationValue::forInvalidSubValueType(
Configuration::KEY_TRANSFORMERS,
$index,
'string',
gettype($entry)
);
}
}
}
return $value;
}
/**
* Check whether the configuration has a given setting.
*
* @param string $key Configuration key to look for.
* @return bool Whether the requested configuration key was found or not.
*/
public function has($key)
{
return array_key_exists($key, $this->configuration);
}
/**
* Get the value for a given key from the configuration.
*
* @param string $key Configuration key to get the value for.
* @return mixed Configuration value for the requested key.
* @throws UnknownConfigurationKey If the key was not found.
*/
public function get($key)
{
if (! array_key_exists($key, $this->configuration)) {
throw UnknownConfigurationKey::fromKey($key);
}
return $this->configuration[$key];
}
/**
* Get the transformer-specific configuration for the requested transformer.
*
* @param string $transformer FQCN of the transformer to get the configuration for.
* @return TransformerConfiguration Transformer-specific configuration.
*/
public function getTransformerConfiguration($transformer)
{
if (! array_key_exists($transformer, $this->transformerConfigurationClasses)) {
throw UnknownConfigurationClass::fromTransformerClass($transformer);
}
$configuration = $this->has($transformer) ? $this->get($transformer) : [];
$configurationClass = $this->transformerConfigurationClasses[$transformer];
return new $configurationClass($configuration);
}
}