Passed
Push — master ( f8ab54...3447e3 )
by Alain
04:34
created

DependencyManager::localize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 6
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 array
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 ] = $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
	 * @param string $handle  Optional. The dependency handle to enqueue. If
0 ignored issues
show
Documentation introduced by
Should the type for parameter $handle not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
192
	 *                        this is not null, only the single matching
193
	 *                        dependency is enqueued.
194
	 * @return void|bool If a handle was passed in, returns whether the handle
195
	 *                        was found or not, otherwise returns void.
196
	 */
197
	public function enqueue( $context = null, $handle = null ) {
198
		$context = $this->validate_context( $context );
199
200
		if ( $handle ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $handle of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
201
			list( $dependency_key, $dependency ) = $this->get_dependency_array( $handle );
202
			if ( $dependency ) {
203
204
				$this->enqueue_dependency(
205
					$dependency,
206
					$dependency_key,
207
					$context
208
				);
209
210
				$this->maybe_localize( $dependency, $context );
211
212
				return true;
213
			}
214
			return false;
215
		}
216
217
		array_walk( $this->dependencies,
218
			[ $this, 'enqueue_dependency_type' ], $context );
219
	}
220
221
	/**
222
	 * Get the matching dependency for a given handle.
223
	 *
224
	 * @since 0.2.2
225
	 *
226
	 * @param string $handle The dependency handle to search for.
227
	 * @return array Array containing the dependency key as well as the
228
	 *                       dependency array itself.
229
	 */
230
	protected function get_dependency_array( $handle ) {
231
		foreach ( $this->dependencies as $dependency_type ) {
232
			$dependency_key = array_search( $handle, $dependency_type, true );
233
			if ( $dependency_key ) {
234
				return [ $dependency_key, $dependency_type[ $dependency_key ] ];
235
			}
236
		}
237
		// Handle not found, return an empty array.
238
		return [ '', null ];
239
	}
240
241
	/**
242
	 * Enqueue a single dependency.
243
	 *
244
	 * @since 0.1.0
245
	 *
246
	 * @param array  $dependency     Configuration data of the dependency.
247
	 * @param string $dependency_key Config key of the dependency.
248
	 * @param mixed  $context        Optional. Context to pass to the
249
	 *                               dependencies. Contains the type of the
250
	 *                               dependency at key
251
	 *                               'dependency_type'.
252
	 */
253
	protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) {
254
		if ( ! $this->is_needed( $dependency, $context ) ) {
255
			return;
256
		}
257
		/** @var \BrightNucleus\Contract\Enqueueable $handler */
258
		$handler = new $this->handlers[$context['dependency_type']];
259
		$handler->enqueue( $dependency );
260
	}
261
262
	/**
263
	 * Check whether a specific dependency is needed.
264
	 *
265
	 * @since 0.1.0
266
	 *
267
	 * @param array $dependency Configuration of the dependency to check.
268
	 * @param mixed $context    Context to pass to the dependencies.
269
	 *                          Contains the type of the dependency at key
270
	 *                          'dependency_type'.
271
	 * @return bool Whether it is needed or not.
272
	 */
273
	protected function is_needed( $dependency, $context ) {
274
		$is_needed = array_key_exists( 'is_needed', $dependency )
275
			? $dependency['is_needed']
276
			: null;
277
278
		if ( null === $is_needed ) {
279
			return true;
280
		}
281
282
		return is_callable( $is_needed ) && $is_needed( $context );
283
	}
284
285
	/**
286
	 * Localize the script of a given dependency.
287
	 *
288
	 * @since 0.1.0
289
	 *
290
	 * @param array $dependency The dependency to localize the script of.
291
	 * @param mixed $context    Contextual data to pass to the callback.
292
	 *                          Contains the type of the dependency at key
293
	 *                          'dependency_type'.
294
	 */
295
	protected function maybe_localize( $dependency, $context ) {
296
		if ( ! array_key_exists( 'localize', $dependency ) ) {
297
			return;
298
		}
299
300
		$localize = $dependency['localize'];
301
		$data     = $localize['data'];
302
		if ( is_callable( $data ) ) {
303
			$data = $data( $context );
304
		}
305
306
		\wp_localize_script( $dependency['handle'], $localize['name'], $data );
307
	}
308
309
	/**
310
	 * Enqueue all dependencies of a specific type.
311
	 *
312
	 * @since 0.1.0
313
	 *
314
	 * @param array  $dependencies    The dependencies to enqueue.
315
	 * @param string $dependency_type The type of the dependencies.
316
	 * @param mixed  $context         Optional. The context to pass to the
317
	 *                                dependencies.
318
	 */
319
	protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) {
320
		$context['dependency_type'] = $dependency_type;
321
		array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context );
322
	}
323
324
	/**
325
	 * Register all dependencies of a specific type.
326
	 *
327
	 * @since 0.1.0
328
	 *
329
	 * @param array  $dependencies    The dependencies to register.
330
	 * @param string $dependency_type The type of the dependencies.
331
	 * @param mixed  $context         Optional. The context to pass to the
332
	 *                                dependencies.
333
	 */
334
	protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) {
335
		$context['dependency_type'] = $dependency_type;
336
		array_walk( $dependencies, [ $this, 'register_dependency' ], $context );
337
	}
338
339
	/**
340
	 * Register a single dependency.
341
	 *
342
	 * @since 0.1.0
343
	 *
344
	 * @param array  $dependency     Configuration data of the dependency.
345
	 * @param string $dependency_key Config key of the dependency.
346
	 * @param mixed  $context        Optional. Context to pass to the
347
	 *                               dependencies. Contains the type of the
348
	 *                               dependency at key
349
	 *                               'dependency_type'.
350
	 */
351
	protected function register_dependency( $dependency, $dependency_key, $context = null ) {
352
		/** @var \BrightNucleus\Contract\Registerable $handler */
353
		$handler = new $this->handlers[$context['dependency_type']];
354
		$handler->register( $dependency );
355
356
		if ( $this->enqueue_immediately ) {
357
			$this->register_enqueue_hooks( $dependency, $context );
358
		}
359
	}
360
361
	/**
362
	 * Register the enqueueing to WordPress hooks.
363
	 *
364
	 * @since 0.2.2
365
	 *
366
	 * @param array $dependency Configuration data of the dependency.
367
	 * @param mixed $context    Optional. Context to pass to the dependencies.
368
	 *                          Contains the type of the dependency at key
369
	 *                          'dependency_type'.
370
	 */
371
	protected function register_enqueue_hooks( $dependency, $context = null ) {
372
		$priority = $this->get_priority( $dependency );
0 ignored issues
show
Bug introduced by
The method get_priority() does not seem to exist on object<BrightNucleus\Dep...ency\DependencyManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
373
374
		foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) {
375
			\add_action( $hook, [ $this, 'enqueue' ], $priority, 1 );
376
		}
377
378
		$this->maybe_localize( $dependency, $context );
379
	}
380
}
381