PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / htmlburger / wpemerge / src / ServiceProviders / ExtendsConfigTrait.php

ExtendsConfigTrait.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/htmlburger/wpemerge/src/ServiceProviders/ExtendsConfigTrait.php

68 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmerge
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2019 Atanas Angelov
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://wpemerge.com/
8 */
9
10 namespace WPEmerge\ServiceProviders;
11
12 use Pimple\Container;
13 use WPEmerge\Support\Arr;
14
15 /**
16 * Allows objects to extend the config.
17 */
18 trait ExtendsConfigTrait {
19 /**
20 * Recursively replace default values with the passed config.
21 * - If either value is not an array, the config value will be used.
22 * - If both are an indexed array, the config value will be used.
23 * - If either is a keyed array, array_replace will be used with config having priority.
24 *
25 * @param mixed $default
26 * @param mixed $config
27 * @return mixed
28 */
29 protected function replaceConfig( $default, $config ) {
30 if ( ! is_array( $default ) || ! is_array( $config ) ) {
31 return $config;
32 }
33
34 $default_is_indexed = array_keys( $default ) === range( 0, count( $default ) - 1 );
35 $config_is_indexed = array_keys( $config ) === range( 0, count( $config ) - 1 );
36
37 if ( $default_is_indexed && $config_is_indexed ) {
38 return $config;
39 }
40
41 $result = $default;
42
43 foreach ( $config as $key => $value ) {
44 $result[ $key ] = $this->replaceConfig( Arr::get( $default, $key ), $value );
45 }
46
47 return $result;
48 }
49
50 /**
51 * Extends the WP Emerge config in the container with a new key.
52 *
53 * @param Container $container
54 * @param string $key
55 * @param mixed $default
56 * @return void
57 */
58 public function extendConfig( $container, $key, $default ) {
59 $config = isset( $container[ WPEMERGE_CONFIG_KEY ] ) ? $container[ WPEMERGE_CONFIG_KEY ] : [];
60 $config = Arr::get( $config, $key, $default );
61
62 $container[ WPEMERGE_CONFIG_KEY ] = array_merge(
63 $container[ WPEMERGE_CONFIG_KEY ],
64 [$key => $this->replaceConfig( $default, $config )]
65 );
66 }
67 }
68