Passed
Push — master ( c298dd...c4dd8e )
by Alain
24:29
created

DependencyManager::enqueue_handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 10
cp 0
rs 8.9197
cc 4
eloc 13
nc 3
nop 3
crap 20
1
<?php
2
/**
3
 * DependencyManager Class.
4
 *
5
 * @package   BrightNucleus\Dependency
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015 Alain Schlesser, Bright NucleusInterface
10
 */
11
12
namespace BrightNucleus\Dependency;
13
14
use BrightNucleus\Config\ConfigInterface;
15
use BrightNucleus\Config\ConfigTrait;
16
use BrightNucleus\Exception\InvalidArgumentException;
17
use BrightNucleus\Exception\RuntimeException;
18
19
/**
20
 * Register and enqueue dependencies that are listed in the config file.
21
 *
22
 * @since   0.1.0
23
 *
24
 * @package BrightNucleus\Dependency
25
 * @author  Alain Schlesser <[email protected]>
26
 */
27
class DependencyManager implements DependencyManagerInterface {
28
29
	use ConfigTrait;
30
31
	/*
32
	 * Default dependency handler implementations.
33
	 */
34
	const DEFAULT_SCRIPT_HANDLER = '\BrightNucleus\Dependency\ScriptHandler';
35
	const DEFAULT_STYLE_HANDLER  = '\BrightNucleus\Dependency\StyleHandler';
36
37
	/*
38
	 * Names of the configuration keys.
39
	 */
40
	const KEY_HANDLERS = 'handlers';
41
	const KEY_SCRIPTS  = 'scripts';
42
	const KEY_STYLES   = 'styles';
43
44
	/**
45
	 * Hold the dependencies, grouped by type.
46
	 *
47
	 * @since 0.1.0
48
	 *
49
	 * @var array;
50
	 */
51
	protected $dependencies = [ ];
52
53
	/**
54
	 * Hold the handlers.
55
	 *
56
	 * @since 0.1.0
57
	 *
58
	 * @var DependencyHandlerInterface[]
59
	 */
60
	protected $handlers = [ ];
61
62
	/**
63
	 * Whether to enqueue immediately upon registration.
64
	 *
65
	 * @since 0.2.2
66
	 *
67
	 * @var bool
68
	 */
69
	protected $enqueue_immediately;
70
71
	/**
72
	 * Instantiate DependencyManager object.
73
	 *
74
	 * @since 0.1.0
75
	 *
76
	 * @param ConfigInterface $config   ConfigInterface object that contains
77
	 *                                  dependency settings.
78
	 * @param bool            $enqueue  Optional. Whether to enqueue
79
	 *                                  immediately. Defaults to true.
80
	 * @throws RuntimeException If the config could not be processed.
81
	 * @throws InvalidArgumentException If no dependency handlers were
82
	 *                                  specified.
83
	 */
84
	public function __construct( ConfigInterface $config, $enqueue = true ) {
85
		$this->processConfig( $config );
86
		$this->enqueue_immediately = $enqueue;
87
		$this->init_handlers();
88
		$this->init_dependencies();
89
	}
90
91
	/**
92
	 * Initialize the dependency handlers.
93
	 *
94
	 * @since 0.1.0
95
	 */
96
	protected function init_handlers() {
97
		$keys = [ self::KEY_SCRIPTS, self::KEY_STYLES ];
98
		foreach ( $keys as $key ) {
99
			if ( $this->hasConfigKey( $key ) ) {
100
				$this->add_handler( $key );
101
			}
102
		}
103
	}
104
105
	/**
106
	 * Add a single dependency handler.
107
	 *
108
	 * @since 0.1.0
109
	 *
110
	 * @param string $dependency The dependency type for which to add a handler.
111
	 */
112
	protected function add_handler( $dependency ) {
113
		if ( $this->hasConfigKey( $dependency ) ) {
114
			$handler = $this->hasConfigKey( self::KEY_HANDLERS, $dependency )
115
				? $this->getConfigKey( self::KEY_HANDLERS, $dependency )
116
				: $this->get_default_handler( $dependency );
117
			if ( $handler ) {
118
				$this->handlers[ $dependency ] = new $handler;
119
			}
120
		}
121
	}
122
123
	/**
124
	 * Get the default handler class for a given type of dependency.
125
	 *
126
	 * @since 0.1.0
127
	 *
128
	 * @param string $dependency The dependency that needs a handler.
129
	 * @return string|null Class name of the handler. Null if none.
130
	 */
131
	protected function get_default_handler( $dependency ) {
132
		switch ( $dependency ) {
133
			case self::KEY_STYLES:
134
				return self::DEFAULT_STYLE_HANDLER;
135
			case self::KEY_SCRIPTS:
136
				return self::DEFAULT_SCRIPT_HANDLER;
137
			default:
138
				return null;
139
		}
140
	}
141
142
	/**
143
	 * Initialize the actual dependencies.
144
	 *
145
	 * @since 0.1.0
146
	 */
147
	protected function init_dependencies() {
148
		array_walk( $this->handlers,
149
			function ( $handler, $dependency_type ) {
150
				if ( $this->hasConfigKey( $dependency_type ) ) {
151
					$this->dependencies[ $dependency_type ] = $this->getConfigKey( $dependency_type );
152
				}
153
			} );
154
	}
155
156
	/**
157
	 * Register all dependencies.
158
	 *
159
	 * @since 0.1.0
160
	 *
161
	 * @param mixed $context Optional. The context to pass to the dependencies.
162
	 */
163
	public function register( $context = null ) {
164
		$context = $this->validate_context( $context );
165
		array_walk( $this->dependencies,
166
			[ $this, 'register_dependency_type' ], $context );
167
	}
168
169
	/**
170
	 * Validate the context to make sure it is an array.
171
	 *
172
	 * @since 0.2.1
173
	 *
174
	 * @param mixed $context The context as passed in by WordPress.
175
	 * @return array Validated context.
176
	 */
177
	protected function validate_context( $context ) {
178
		if ( is_string( $context ) ) {
179
			return [ 'wp_context' => $context ];
180
		}
181
		return (array) $context;
182
	}
183
184
	/**
185
	 * Enqueue all dependencies.
186
	 *
187
	 * @since 0.1.0
188
	 *
189
	 * @param mixed $context  Optional. The context to pass to the
190
	 *                        dependencies.
191
	 */
192
	public function enqueue( $context = null ) {
193
		$context = $this->validate_context( $context );
194
195
		array_walk( $this->dependencies,
196
			[ $this, 'enqueue_dependency_type' ], $context );
197
	}
198
199
	/**
200
	 * Enqueue a single dependency retrieved by its handle.
201
	 *
202
	 * @since 0.2.2
203
	 *
204
	 * @param string $handle   The dependency handle to enqueue.
205
	 * @param mixed  $context  Optional. The context to pass to the
206
	 *                         dependencies.
207
	 * @param bool   $fallback Whether to fall back to dependencies registered
208
	 *                         outside of DependencyManager. Defaults to false.
209
	 * @return bool Returns whether the handle was found or not.
210
	 */
211
	public function enqueue_handle( $handle, $context = null, $fallback = false ) {
212
		list( $dependency_key, $dependency ) = $this->get_dependency_array( $handle );
213
		if ( $dependency ) {
214
215
			$this->enqueue_dependency(
216
				$dependency,
217
				$dependency_key,
218
				$context
219
			);
220
221
			$this->maybe_localize( $dependency, $context );
222
223
			return true;
224
		}
225
226
		if ( $fallback ) {
227
			foreach ( $this->handlers as $handler ) {
228
				$handler->maybe_enqueue( $handle );
229
			}
230
		}
231
		return false;
232
	}
233
234
	/**
235
	 * Get the matching dependency for a given handle.
236
	 *
237
	 * @since 0.2.2
238
	 *
239
	 * @param string $handle The dependency handle to search for.
240
	 * @return array Array containing the dependency key as well as the
241
	 *                       dependency array itself.
242
	 */
243
	protected function get_dependency_array( $handle ) {
244
		foreach ( $this->dependencies as $dependency_type ) {
245
			$dependency_key = array_search( $handle, $dependency_type, true );
246
			if ( $dependency_key ) {
247
				return [ $dependency_key, $dependency_type[ $dependency_key ] ];
248
			}
249
		}
250
		// Handle not found, return an empty array.
251
		return [ '', null ];
252
	}
253
254
	/**
255
	 * Enqueue a single dependency.
256
	 *
257
	 * @since 0.1.0
258
	 *
259
	 * @param array  $dependency     Configuration data of the dependency.
260
	 * @param string $dependency_key Config key of the dependency.
261
	 * @param mixed  $context        Optional. Context to pass to the
262
	 *                               dependencies. Contains the type of the
263
	 *                               dependency at key
264
	 *                               'dependency_type'.
265
	 */
266
	protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) {
267
		if ( ! $this->is_needed( $dependency, $context ) ) {
268
			return;
269
		}
270
		$handler = $this->handlers[ $context['dependency_type'] ];
271
		$handler->enqueue( $dependency );
272
	}
273
274
	/**
275
	 * Check whether a specific dependency is needed.
276
	 *
277
	 * @since 0.1.0
278
	 *
279
	 * @param array $dependency Configuration of the dependency to check.
280
	 * @param mixed $context    Context to pass to the dependencies.
281
	 *                          Contains the type of the dependency at key
282
	 *                          'dependency_type'.
283
	 * @return bool Whether it is needed or not.
284
	 */
285
	protected function is_needed( $dependency, $context ) {
286
		$is_needed = array_key_exists( 'is_needed', $dependency )
287
			? $dependency['is_needed']
288
			: null;
289
290
		if ( null === $is_needed ) {
291
			return true;
292
		}
293
294
		return is_callable( $is_needed ) && $is_needed( $context );
295
	}
296
297
	/**
298
	 * Localize the script of a given dependency.
299
	 *
300
	 * @since 0.1.0
301
	 *
302
	 * @param array $dependency The dependency to localize the script of.
303
	 * @param mixed $context    Contextual data to pass to the callback.
304
	 *                          Contains the type of the dependency at key
305
	 *                          'dependency_type'.
306
	 */
307
	protected function maybe_localize( $dependency, $context ) {
308
		if ( ! array_key_exists( 'localize', $dependency ) ) {
309
			return;
310
		}
311
312
		$localize = $dependency['localize'];
313
		$data     = $localize['data'];
314
		if ( is_callable( $data ) ) {
315
			$data = $data( $context );
316
		}
317
318
		\wp_localize_script( $dependency['handle'], $localize['name'], $data );
319
	}
320
321
	/**
322
	 * Enqueue all dependencies of a specific type.
323
	 *
324
	 * @since 0.1.0
325
	 *
326
	 * @param array  $dependencies    The dependencies to enqueue.
327
	 * @param string $dependency_type The type of the dependencies.
328
	 * @param mixed  $context         Optional. The context to pass to the
329
	 *                                dependencies.
330
	 */
331
	protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) {
332
		$context['dependency_type'] = $dependency_type;
333
		array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context );
334
	}
335
336
	/**
337
	 * Register all dependencies of a specific type.
338
	 *
339
	 * @since 0.1.0
340
	 *
341
	 * @param array  $dependencies    The dependencies to register.
342
	 * @param string $dependency_type The type of the dependencies.
343
	 * @param mixed  $context         Optional. The context to pass to the
344
	 *                                dependencies.
345
	 */
346
	protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) {
347
		$context['dependency_type'] = $dependency_type;
348
		array_walk( $dependencies, [ $this, 'register_dependency' ], $context );
349
	}
350
351
	/**
352
	 * Register a single dependency.
353
	 *
354
	 * @since 0.1.0
355
	 *
356
	 * @param array  $dependency     Configuration data of the dependency.
357
	 * @param string $dependency_key Config key of the dependency.
358
	 * @param mixed  $context        Optional. Context to pass to the
359
	 *                               dependencies. Contains the type of the
360
	 *                               dependency at key
361
	 *                               'dependency_type'.
362
	 */
363
	protected function register_dependency( $dependency, $dependency_key, $context = null ) {
364
		$handler = $this->handlers[ $context['dependency_type'] ];
365
		$handler->register( $dependency );
366
367
		if ( $this->enqueue_immediately ) {
368
			$this->register_enqueue_hooks( $dependency, $context );
369
		}
370
	}
371
372
	/**
373
	 * Register the enqueueing to WordPress hooks.
374
	 *
375
	 * @since 0.2.2
376
	 *
377
	 * @param array $dependency Configuration data of the dependency.
378
	 * @param mixed $context    Optional. Context to pass to the dependencies.
379
	 *                          Contains the type of the dependency at key
380
	 *                          'dependency_type'.
381
	 */
382
	protected function register_enqueue_hooks( $dependency, $context = null ) {
383
		$priority = $this->get_priority( $dependency );
384
385
		foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) {
386
			\add_action( $hook, [ $this, 'enqueue' ], $priority, 1 );
387
		}
388
389
		$this->maybe_localize( $dependency, $context );
390
	}
391
392
	/**
393
	 * Get the priority of a dependency.
394
	 *
395
	 * @since 0.2.2
396
	 *
397
	 * @param array $dependency Configuration data of the dependency.
398
	 * @return int Priority to use.
399
	 */
400
	protected function get_priority( $dependency ) {
401
		if ( array_key_exists( 'priority', $dependency ) ) {
402
			return intval( $dependency['priority'] );
403
		}
404
		return 10;
405
	}
406
}
407