Passed
Push — master ( c4dd8e...c0d976 )
by Alain
42:45 queued 20:11
created

DependencyManager::init_dependency_type()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
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->init_dependency_type( $dependency_type );
152
				}
153
			} );
154
	}
155
156
	/**
157
	 * Initialize the dependencies of a given type.
158
	 *
159
	 * @since 0.2.2
160
	 *
161
	 * @param string $type The type of dependency to initialize.
162
	 * @return array Array of dependency configurations.
163
	 */
164
	protected function init_dependency_type( $type ) {
165
		$array = [ ];
166
		$data  = $this->getConfigKey( $type );
167
		foreach ( $data as $dependency ) {
168
			$handle           = array_key_exists( 'handle',
169
				$dependency ) ? $dependency['handle'] : '';
170
			$array[ $handle ] = $dependency;
171
		}
172
		return $array;
173
	}
174
175
	/**
176
	 * Register all dependencies.
177
	 *
178
	 * @since 0.1.0
179
	 *
180
	 * @param mixed $context Optional. The context to pass to the dependencies.
181
	 */
182
	public function register( $context = null ) {
183
		$context = $this->validate_context( $context );
184
		array_walk( $this->dependencies,
185
			[ $this, 'register_dependency_type' ], $context );
186
	}
187
188
	/**
189
	 * Validate the context to make sure it is an array.
190
	 *
191
	 * @since 0.2.1
192
	 *
193
	 * @param mixed $context The context as passed in by WordPress.
194
	 * @return array Validated context.
195
	 */
196
	protected function validate_context( $context ) {
197
		if ( is_string( $context ) ) {
198
			return [ 'wp_context' => $context ];
199
		}
200
		return (array) $context;
201
	}
202
203
	/**
204
	 * Enqueue all dependencies.
205
	 *
206
	 * @since 0.1.0
207
	 *
208
	 * @param mixed $context  Optional. The context to pass to the
209
	 *                        dependencies.
210
	 */
211
	public function enqueue( $context = null ) {
212
		$context = $this->validate_context( $context );
213
214
		array_walk( $this->dependencies,
215
			[ $this, 'enqueue_dependency_type' ], $context );
216
	}
217
218
	/**
219
	 * Enqueue a single dependency retrieved by its handle.
220
	 *
221
	 * @since 0.2.2
222
	 *
223
	 * @param string $handle   The dependency handle to enqueue.
224
	 * @param mixed  $context  Optional. The context to pass to the
225
	 *                         dependencies.
226
	 * @param bool   $fallback Whether to fall back to dependencies registered
227
	 *                         outside of DependencyManager. Defaults to false.
228
	 * @return bool Returns whether the handle was found or not.
229
	 */
230
	public function enqueue_handle( $handle, $context = null, $fallback = false ) {
231
		list( $dependency_type, $dependency ) = $this->get_dependency_array( $handle );
232
		$context['dependency_type'] = $dependency_type;
233
		if ( $dependency ) {
234
235
			$this->enqueue_dependency(
236
				$dependency,
237
				$handle,
238
				$context
239
			);
240
241
			$this->maybe_localize( $dependency, $context );
242
243
			return true;
244
		}
245
246
		if ( $fallback ) {
247
			foreach ( $this->handlers as $handler ) {
248
				$handler->maybe_enqueue( $handle );
249
			}
250
		}
251
		return false;
252
	}
253
254
	/**
255
	 * Get the matching dependency for a given handle.
256
	 *
257
	 * @since 0.2.2
258
	 *
259
	 * @param string $handle The dependency handle to search for.
260
	 * @return array Array containing the dependency key as well as the
261
	 *                       dependency array itself.
262
	 */
263
	protected function get_dependency_array( $handle ) {
264
		foreach ( $this->dependencies as $type => $dependencies ) {
265
			if ( array_key_exists( $handle, $dependencies ) ) {
266
				return [ $type, $dependencies[ $handle ] ];
267
			}
268
		}
269
		// Handle not found, return an empty array.
270
		return [ '', null ];
271
	}
272
273
	/**
274
	 * Enqueue a single dependency.
275
	 *
276
	 * @since 0.1.0
277
	 *
278
	 * @param array  $dependency     Configuration data of the dependency.
279
	 * @param string $dependency_key Config key of the dependency.
280
	 * @param mixed  $context        Optional. Context to pass to the
281
	 *                               dependencies. Contains the type of the
282
	 *                               dependency at key
283
	 *                               'dependency_type'.
284
	 */
285
	protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) {
286
		if ( ! $this->is_needed( $dependency, $context ) ) {
287
			return;
288
		}
289
		$handler = $this->handlers[ $context['dependency_type'] ];
290
		$handler->enqueue( $dependency );
291
	}
292
293
	/**
294
	 * Check whether a specific dependency is needed.
295
	 *
296
	 * @since 0.1.0
297
	 *
298
	 * @param array $dependency Configuration of the dependency to check.
299
	 * @param mixed $context    Context to pass to the dependencies.
300
	 *                          Contains the type of the dependency at key
301
	 *                          'dependency_type'.
302
	 * @return bool Whether it is needed or not.
303
	 */
304
	protected function is_needed( $dependency, $context ) {
305
		$is_needed = array_key_exists( 'is_needed', $dependency )
306
			? $dependency['is_needed']
307
			: null;
308
309
		if ( null === $is_needed ) {
310
			return true;
311
		}
312
313
		return is_callable( $is_needed ) && $is_needed( $context );
314
	}
315
316
	/**
317
	 * Localize the script of a given dependency.
318
	 *
319
	 * @since 0.1.0
320
	 *
321
	 * @param array $dependency The dependency to localize the script of.
322
	 * @param mixed $context    Contextual data to pass to the callback.
323
	 *                          Contains the type of the dependency at key
324
	 *                          'dependency_type'.
325
	 */
326
	protected function maybe_localize( $dependency, $context ) {
327
		if ( ! array_key_exists( 'localize', $dependency ) ) {
328
			return;
329
		}
330
331
		$localize = $dependency['localize'];
332
		$data     = $localize['data'];
333
		if ( is_callable( $data ) ) {
334
			$data = $data( $context );
335
		}
336
337
		\wp_localize_script( $dependency['handle'], $localize['name'], $data );
338
	}
339
340
	/**
341
	 * Enqueue all dependencies of a specific type.
342
	 *
343
	 * @since 0.1.0
344
	 *
345
	 * @param array  $dependencies    The dependencies to enqueue.
346
	 * @param string $dependency_type The type of the dependencies.
347
	 * @param mixed  $context         Optional. The context to pass to the
348
	 *                                dependencies.
349
	 */
350
	protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) {
351
		$context['dependency_type'] = $dependency_type;
352
		array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context );
353
	}
354
355
	/**
356
	 * Register all dependencies of a specific type.
357
	 *
358
	 * @since 0.1.0
359
	 *
360
	 * @param array  $dependencies    The dependencies to register.
361
	 * @param string $dependency_type The type of the dependencies.
362
	 * @param mixed  $context         Optional. The context to pass to the
363
	 *                                dependencies.
364
	 */
365
	protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) {
366
		$context['dependency_type'] = $dependency_type;
367
		array_walk( $dependencies, [ $this, 'register_dependency' ], $context );
368
	}
369
370
	/**
371
	 * Register a single dependency.
372
	 *
373
	 * @since 0.1.0
374
	 *
375
	 * @param array  $dependency     Configuration data of the dependency.
376
	 * @param string $dependency_key Config key of the dependency.
377
	 * @param mixed  $context        Optional. Context to pass to the
378
	 *                               dependencies. Contains the type of the
379
	 *                               dependency at key
380
	 *                               'dependency_type'.
381
	 */
382
	protected function register_dependency( $dependency, $dependency_key, $context = null ) {
383
		$handler = $this->handlers[ $context['dependency_type'] ];
384
		$handler->register( $dependency );
385
386
		if ( $this->enqueue_immediately ) {
387
			$this->register_enqueue_hooks( $dependency, $context );
388
		}
389
	}
390
391
	/**
392
	 * Register the enqueueing to WordPress hooks.
393
	 *
394
	 * @since 0.2.2
395
	 *
396
	 * @param array $dependency Configuration data of the dependency.
397
	 * @param mixed $context    Optional. Context to pass to the dependencies.
398
	 *                          Contains the type of the dependency at key
399
	 *                          'dependency_type'.
400
	 */
401
	protected function register_enqueue_hooks( $dependency, $context = null ) {
402
		$priority = $this->get_priority( $dependency );
403
404
		foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) {
405
			\add_action( $hook, [ $this, 'enqueue' ], $priority, 1 );
406
		}
407
408
		$this->maybe_localize( $dependency, $context );
409
	}
410
411
	/**
412
	 * Get the priority of a dependency.
413
	 *
414
	 * @since 0.2.2
415
	 *
416
	 * @param array $dependency Configuration data of the dependency.
417
	 * @return int Priority to use.
418
	 */
419
	protected function get_priority( $dependency ) {
420
		if ( array_key_exists( 'priority', $dependency ) ) {
421
			return intval( $dependency['priority'] );
422
		}
423
		return 10;
424
	}
425
}
426