PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / vendor / inpsyde / assets / src / AssetFactory.php

AssetFactory.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at vendor/inpsyde/assets/src/AssetFactory.php

266 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Assets package.
5 *
6 * (c) Inpsyde GmbH
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 declare(strict_types=1);
13
14 namespace Inpsyde\Assets;
15
16 use Inpsyde\Assets\Exception\InvalidArgumentException;
17 use Inpsyde\Assets\Loader\PhpFileLoader;
18 use Inpsyde\Assets\Loader\ArrayLoader;
19
20 /**
21 * Class AssetFactory
22 *
23 * @package Inpsyde\Assets
24 */
25 final class AssetFactory
26 {
27 /**
28 * @param array $config
29 *
30 * @return Asset
31 * @throws Exception\MissingArgumentException
32 * @throws Exception\InvalidArgumentException
33 *
34 * phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
35 * phpcs:disable Inpsyde.CodeQuality.NestingLevel.High
36 * phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
37 * @psalm-suppress MixedArgument, MixedMethodCall
38 */
39 public static function create(array $config): Asset
40 {
41 $config = self::validateConfig($config);
42
43 $location = $config['location'] ?? Asset::FRONTEND;
44 $handle = $config['handle'];
45 $url = $config['url'];
46
47 $class = (string) $config['type'];
48 if (!class_exists($class)) {
49 throw new Exception\InvalidArgumentException(
50 sprintf(
51 'The given class "%s" does not exists.',
52 $class
53 )
54 );
55 }
56
57 $asset = new $class($handle, $url, $location);
58 if (!$asset instanceof Asset) {
59 throw new Exception\InvalidArgumentException(
60 sprintf(
61 'The given class "%s" is not implementing %s',
62 $class,
63 Asset::class
64 )
65 );
66 }
67
68 $propertiesToMethod = [
69 'filePath' => 'withFilePath',
70 'version' => 'withVersion',
71 'location' => 'forLocation',
72 'enqueue' => 'canEnqueue',
73 'handler' => 'useHandler',
74 'condition' => 'withCondition',
75 'attributes' => 'withAttributes',
76 ];
77
78 if ($asset instanceof Script) {
79 foreach ($config['localize'] as $objectName => $data) {
80 $asset->withLocalize((string) $objectName, $data);
81 }
82
83 if (isset($config['translation'])) {
84 /** @var array{domain:string, path:?string} $translations */
85 $translations = $config['translation'];
86 $asset->withTranslation(
87 (string) $translations['domain'],
88 $translations['path'] ?? null
89 );
90 }
91
92 $inFooter = $config['inFooter'] ?? true;
93 $inFooter
94 ? $asset->isInFooter()
95 : $asset->isInHeader();
96
97 if (!empty($config['inline']['before']) && is_array($config['inline']['before'])) {
98 foreach ($config['inline']['before'] as $script) {
99 $asset->prependInlineScript((string) $script);
100 }
101 }
102
103 if (!empty($config['inline']['after']) && is_array($config['inline']['after'])) {
104 foreach ($config['inline']['after'] as $script) {
105 $asset->appendInlineScript((string) $script);
106 }
107 }
108 }
109
110 if ($asset instanceof Style) {
111 $propertiesToMethod['media'] = 'forMedia';
112 $propertiesToMethod['inlineStyles'] = 'withInlineStyles';
113 $propertiesToMethod['media'] = 'forMedia';
114 }
115
116 foreach ($propertiesToMethod as $key => $methodName) {
117 if (!isset($config[$key])) {
118 continue;
119 }
120 $asset->{$methodName}($config[$key]);
121 }
122
123 $dependencies = $config['dependencies'] ?? null;
124 if (is_array($dependencies)) {
125 $asset->withDependencies(...$dependencies);
126 } elseif (is_scalar($dependencies)) {
127 $asset->withDependencies((string) $dependencies);
128 }
129
130 return $asset;
131 }
132
133 /**
134 * @param array $config
135 *
136 * @return array
137 *
138 * @throws Exception\MissingArgumentException
139 */
140 private static function validateConfig(array $config): array
141 {
142 self::ensureRequiredConfigFields($config);
143 $config = self::normalizeVersionConfig($config);
144 $config = self::normalizeTranslationConfig($config);
145 $config = self::normalizeLocalizeConfig($config);
146
147 return $config;
148 }
149
150 private static function ensureRequiredConfigFields(array $config): void
151 {
152 $requiredFields = [
153 'type',
154 'url',
155 'handle',
156 ];
157
158 foreach ($requiredFields as $key) {
159 if (!isset($config[$key])) {
160 throw new Exception\MissingArgumentException(
161 sprintf(
162 'The given config <code>%s</code> is missing.',
163 $key
164 )
165 );
166 }
167 }
168 }
169
170 private static function normalizeVersionConfig(array $config): array
171 {
172 // some existing configurations uses time() as version parameter which leads to
173 // fatal errors since 2.5
174 if (isset($config['version'])) {
175 $config['version'] = (string) $config['version'];
176 }
177
178 return $config;
179 }
180
181 private static function normalizeTranslationConfig(array $config): array
182 {
183 if (!isset($config['translation'])) {
184 return $config;
185 }
186
187 if (is_string($config['translation'])) {
188 // backward compatibility
189 $config['translation'] = [
190 'domain' => $config['translation'],
191 'path' => null,
192 ];
193
194 return $config;
195 }
196
197 if (!is_array($config['translation'])) {
198 throw new InvalidArgumentException(
199 "Config key <code>translation</code> must be of type string or array"
200 );
201 }
202
203 if (!isset($config['translation']['domain'])) {
204 throw new Exception\MissingArgumentException(
205 'Config key <code>translation[domain]</code> is missing.'
206 );
207 }
208
209 if (!isset($config['translation']['path'])) {
210 $config['translation']['path'] = null;
211 }
212
213 return $config;
214 }
215
216 private static function normalizeLocalizeConfig(array $config): array
217 {
218 if (!isset($config['localize'])) {
219 $config['localize'] = [];
220
221 return $config;
222 }
223 if (is_callable($config['localize'])) {
224 $config['localize'] = $config['localize']();
225 }
226 if (!is_array($config['localize'])) {
227 throw new InvalidArgumentException(
228 'Config key <code>localize</code> must evaluate as an array'
229 );
230 }
231
232 return $config;
233 }
234
235 /**
236 * @param string $file
237 *
238 * @return array
239 *
240 * @throws Exception\FileNotFoundException
241 * @deprecated PhpArrayFileLoader::load(string $filePath)
242 *
243 */
244 public static function createFromFile(string $file): array
245 {
246 $loader = new PhpFileLoader();
247
248 return $loader->load($file);
249 }
250
251 /**
252 * @param array $data
253 *
254 * @return array
255 *
256 * @throws Exception\FileNotFoundException
257 * @deprecated ArrayLoader::load(array $data)
258 */
259 public static function createFromArray(array $data): array
260 {
261 $loader = new ArrayLoader();
262
263 return $loader->load($data);
264 }
265 }
266