@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @var array; |
50 | 50 | */ |
51 | - protected $dependencies = [ ]; |
|
51 | + protected $dependencies = []; |
|
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Hold the handlers. |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @var array |
59 | 59 | */ |
60 | - protected $handlers = [ ]; |
|
60 | + protected $handlers = []; |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Instantiate DependencyManager object. |
@@ -70,8 +70,8 @@ discard block |
||
70 | 70 | * @throws InvalidArgumentException If no dependency handlers were |
71 | 71 | * specified. |
72 | 72 | */ |
73 | - public function __construct( ConfigInterface $config ) { |
|
74 | - $this->processConfig( $config ); |
|
73 | + public function __construct(ConfigInterface $config) { |
|
74 | + $this->processConfig($config); |
|
75 | 75 | $this->init_handlers(); |
76 | 76 | $this->init_dependencies(); |
77 | 77 | } |
@@ -82,10 +82,10 @@ discard block |
||
82 | 82 | * @since 0.1.0 |
83 | 83 | */ |
84 | 84 | protected function init_handlers() { |
85 | - $keys = [ self::KEY_SCRIPTS, self::KEY_STYLES ]; |
|
86 | - foreach ( $keys as $key ) { |
|
87 | - if ( $this->hasConfigKey( $key ) ) { |
|
88 | - $this->add_handler( $key ); |
|
85 | + $keys = [self::KEY_SCRIPTS, self::KEY_STYLES]; |
|
86 | + foreach ($keys as $key) { |
|
87 | + if ($this->hasConfigKey($key)) { |
|
88 | + $this->add_handler($key); |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | } |
@@ -97,12 +97,12 @@ discard block |
||
97 | 97 | * |
98 | 98 | * @param string $dependency The dependency type for which to add a handler. |
99 | 99 | */ |
100 | - protected function add_handler( $dependency ) { |
|
101 | - if ( $this->hasConfigKey( $dependency ) ) { |
|
102 | - $handler = $this->hasConfigKey( self::KEY_HANDLERS, $dependency ) |
|
103 | - ? $this->getConfigKey( self::KEY_HANDLERS, $dependency ) |
|
104 | - : $this->get_default_handler( $dependency ); |
|
105 | - if ( $handler ) { |
|
100 | + protected function add_handler($dependency) { |
|
101 | + if ($this->hasConfigKey($dependency)) { |
|
102 | + $handler = $this->hasConfigKey(self::KEY_HANDLERS, $dependency) |
|
103 | + ? $this->getConfigKey(self::KEY_HANDLERS, $dependency) |
|
104 | + : $this->get_default_handler($dependency); |
|
105 | + if ($handler) { |
|
106 | 106 | $this->handlers[] = $handler; |
107 | 107 | } |
108 | 108 | } |
@@ -116,8 +116,8 @@ discard block |
||
116 | 116 | * @param string $dependency The dependency that needs a handler. |
117 | 117 | * @return string|null Class name of the handler. Null if none. |
118 | 118 | */ |
119 | - protected function get_default_handler( $dependency ) { |
|
120 | - switch ( $dependency ) { |
|
119 | + protected function get_default_handler($dependency) { |
|
120 | + switch ($dependency) { |
|
121 | 121 | case self::KEY_STYLES: |
122 | 122 | return self::DEFAULT_STYLE_HANDLER; |
123 | 123 | case self::KEY_SCRIPTS: |
@@ -133,10 +133,10 @@ discard block |
||
133 | 133 | * @since 0.1.0 |
134 | 134 | */ |
135 | 135 | protected function init_dependencies() { |
136 | - array_walk( $this->handlers, |
|
137 | - function ( $handler, $dependency_type ) use ( $config ) { |
|
138 | - if ( ! empty( $config[ $dependency_type ] ) ) { |
|
139 | - $this->dependencies[ $dependency_type ] = $config[ $dependency_type ]; |
|
136 | + array_walk($this->handlers, |
|
137 | + function($handler, $dependency_type) use ($config) { |
|
138 | + if ( ! empty($config[$dependency_type])) { |
|
139 | + $this->dependencies[$dependency_type] = $config[$dependency_type]; |
|
140 | 140 | } |
141 | 141 | } ); |
142 | 142 | } |
@@ -148,9 +148,9 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @param mixed $context Optional. The context to pass to the dependencies. |
150 | 150 | */ |
151 | - public function register( $context = null ) { |
|
152 | - array_walk( $this->dependencies, |
|
153 | - [ $this, 'register_dependency_type' ], $context ); |
|
151 | + public function register($context = null) { |
|
152 | + array_walk($this->dependencies, |
|
153 | + [$this, 'register_dependency_type'], $context); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
@@ -160,9 +160,9 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @param mixed $context Optional. The context to pass to the dependencies. |
162 | 162 | */ |
163 | - public function enqueue( $context = null ) { |
|
164 | - array_walk( $this->dependencies, |
|
165 | - [ $this, 'enqueue_dependency_type' ], $context ); |
|
163 | + public function enqueue($context = null) { |
|
164 | + array_walk($this->dependencies, |
|
165 | + [$this, 'enqueue_dependency_type'], $context); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -175,9 +175,9 @@ discard block |
||
175 | 175 | * @param mixed $context Optional. The context to pass to the |
176 | 176 | * dependencies. |
177 | 177 | */ |
178 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
178 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
179 | 179 | $context['dependency_type'] = $dependency_type; |
180 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
180 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
@@ -190,9 +190,9 @@ discard block |
||
190 | 190 | * @param mixed $context Optional. The context to pass to the |
191 | 191 | * dependencies. |
192 | 192 | */ |
193 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
193 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
194 | 194 | $context['dependency_type'] = $dependency_type; |
195 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
195 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -206,16 +206,16 @@ discard block |
||
206 | 206 | * Contains the type of the dependency at key |
207 | 207 | * 'dependency_type'. |
208 | 208 | */ |
209 | - protected function register_dependency( $dependency, $dependency_key, $context ) { |
|
209 | + protected function register_dependency($dependency, $dependency_key, $context) { |
|
210 | 210 | /** @var \BrightNucleus\Contract\Registerable $handler */ |
211 | 211 | $handler = new $this->handlers[$context['dependency_type']]; |
212 | - $handler->register( $dependency ); |
|
213 | - \add_action( 'wp_enqueue_scripts', |
|
214 | - [ $this, 'enqueue' ] ); |
|
215 | - \add_action( 'admin_enqueue_scripts', |
|
216 | - [ $this, 'enqueue' ] ); |
|
217 | - if ( array_key_exists( 'localize', $dependency ) ) { |
|
218 | - $this->localize( $dependency, $context ); |
|
212 | + $handler->register($dependency); |
|
213 | + \add_action('wp_enqueue_scripts', |
|
214 | + [$this, 'enqueue']); |
|
215 | + \add_action('admin_enqueue_scripts', |
|
216 | + [$this, 'enqueue']); |
|
217 | + if (array_key_exists('localize', $dependency)) { |
|
218 | + $this->localize($dependency, $context); |
|
219 | 219 | } |
220 | 220 | } |
221 | 221 | |
@@ -229,13 +229,13 @@ discard block |
||
229 | 229 | * Contains the type of the dependency at key |
230 | 230 | * 'dependency_type'. |
231 | 231 | */ |
232 | - protected function localize( $dependency, $context ) { |
|
232 | + protected function localize($dependency, $context) { |
|
233 | 233 | $localize = $dependency['localize']; |
234 | 234 | $data = $localize['data']; |
235 | - if ( is_callable( $data ) ) { |
|
236 | - $data = $data( $context ); |
|
235 | + if (is_callable($data)) { |
|
236 | + $data = $data($context); |
|
237 | 237 | } |
238 | - \wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
238 | + \wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | /** |
@@ -249,13 +249,13 @@ discard block |
||
249 | 249 | * Contains the type of the dependency at key |
250 | 250 | * 'dependency_type'. |
251 | 251 | */ |
252 | - protected function enqueue_dependency( $dependency, $dependency_key, $context ) { |
|
253 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
252 | + protected function enqueue_dependency($dependency, $dependency_key, $context) { |
|
253 | + if ( ! $this->is_needed($dependency, $context)) { |
|
254 | 254 | return; |
255 | 255 | } |
256 | 256 | /** @var \BrightNucleus\Contract\Enqueueable $handler */ |
257 | 257 | $handler = new $this->handlers[$dependency_type]; |
258 | - $handler->enqueue( $dependency ); |
|
258 | + $handler->enqueue($dependency); |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | /** |
@@ -269,15 +269,15 @@ discard block |
||
269 | 269 | * 'dependency_type'. |
270 | 270 | * @return bool Whether it is needed or not. |
271 | 271 | */ |
272 | - protected function is_needed( $dependency, $context ) { |
|
273 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
272 | + protected function is_needed($dependency, $context) { |
|
273 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
274 | 274 | ? $dependency['is_needed'] |
275 | 275 | : null; |
276 | 276 | |
277 | - if ( null === $is_needed ) { |
|
277 | + if (null === $is_needed) { |
|
278 | 278 | return true; |
279 | 279 | } |
280 | 280 | |
281 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
281 | + return is_callable($is_needed) && $is_needed($context); |
|
282 | 282 | } |
283 | 283 | } |