| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Utils; |
| 13 |
|
| 14 |
use ArrayAccess; |
| 15 |
use Exception; |
| 16 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\AccessException; |
| 17 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\InvalidArgumentException; |
| 18 |
use WindPressDeps\Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; |
| 19 |
use WindPressDeps\Symfony\Component\PropertyAccess\PropertyAccess; |
| 20 |
use WIND_PRESS; |
| 21 |
/** |
| 22 |
* Accessor for the plugin config. |
| 23 |
* |
| 24 |
* @since 3.0.0 |
| 25 |
*/ |
| 26 |
class Config |
| 27 |
{ |
| 28 |
/** |
| 29 |
* Stores the instance of PropertyAccessor, implementing a Singleton pattern. |
| 30 |
*/ |
| 31 |
private static ?\WindPressDeps\Symfony\Component\PropertyAccess\PropertyAccessorInterface $propertyAccessor = null; |
| 32 |
public static function propertyAccessor() |
| 33 |
{ |
| 34 |
if (!isset(self::$propertyAccessor)) { |
| 35 |
self::$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); |
| 36 |
} |
| 37 |
return self::$propertyAccessor; |
| 38 |
} |
| 39 |
/** |
| 40 |
* Gets a value at the end of the property path of the config. |
| 41 |
* |
| 42 |
* @param string $path The property path to read |
| 43 |
* @param mixed $defaultValue The value to return if the property path does not exist |
| 44 |
* |
| 45 |
* @return mixed The value at the end of the property path |
| 46 |
*/ |
| 47 |
public static function get($path, $defaultValue = null) |
| 48 |
{ |
| 49 |
$options = json_decode(get_option(WIND_PRESS::WP_OPTION . '_options', '{}'), null, 512, \JSON_THROW_ON_ERROR); |
| 50 |
$options = apply_filters('f!windpress/utlis/config:options', $options); |
| 51 |
try { |
| 52 |
return self::propertyAccessor()->getValue($options, $path); |
| 53 |
} catch (Exception $exception) { |
| 54 |
return $defaultValue; |
| 55 |
} |
| 56 |
} |
| 57 |
/** |
| 58 |
* Sets a value at the end of the property path of the config. |
| 59 |
* |
| 60 |
* @param string $path The property path to modify |
| 61 |
* @param mixed $value The value to set at the end of the property path |
| 62 |
* |
| 63 |
* @throws InvalidArgumentException If the property path is invalid |
| 64 |
* @throws AccessException If a property/index does not exist or is not public |
| 65 |
* @throws UnexpectedTypeException If a value within the path is neither object nor array |
| 66 |
*/ |
| 67 |
public static function set($path, $value) |
| 68 |
{ |
| 69 |
$options = json_decode(get_option(WIND_PRESS::WP_OPTION . '_options', '{}'), null, 512, \JSON_THROW_ON_ERROR); |
| 70 |
$options = apply_filters('f!windpress/utlis/config:options', $options); |
| 71 |
if (self::propertyAccessor()->isWritable($options, $path)) { |
| 72 |
self::propertyAccessor()->setValue($options, $path, $value); |
| 73 |
} else { |
| 74 |
self::data_set($options, $path, $value); |
| 75 |
} |
| 76 |
update_option(WIND_PRESS::WP_OPTION . '_options', wp_json_encode($options, \JSON_THROW_ON_ERROR)); |
| 77 |
} |
| 78 |
/** |
| 79 |
* Set an item on an array or object using dot notation. |
| 80 |
* |
| 81 |
* @param mixed $target |
| 82 |
* @param string|array $key |
| 83 |
* @param mixed $value |
| 84 |
* @param bool $overwrite |
| 85 |
* @return mixed |
| 86 |
* |
| 87 |
* @see https://github.com/laravel/framework/blob/a84c4f41d3fb1c57684bb417b1f0858300e769d0/src/Illuminate/Collections/helpers.php#L109 |
| 88 |
*/ |
| 89 |
public static function data_set(&$target, $key, $value, $overwrite = \true) |
| 90 |
{ |
| 91 |
$segments = is_array($key) ? $key : explode('.', $key); |
| 92 |
if (($segment = array_shift($segments)) === '*') { |
| 93 |
if (!self::array_accessible($target)) { |
| 94 |
$target = []; |
| 95 |
} |
| 96 |
if ($segments) { |
| 97 |
foreach ($target as &$inner) { |
| 98 |
self::data_set($inner, $segments, $value, $overwrite); |
| 99 |
} |
| 100 |
} elseif ($overwrite) { |
| 101 |
foreach ($target as &$inner) { |
| 102 |
$inner = $value; |
| 103 |
} |
| 104 |
} |
| 105 |
} elseif (self::array_accessible($target)) { |
| 106 |
if ($segments) { |
| 107 |
if (!self::array_exists($target, $segment)) { |
| 108 |
$target[$segment] = []; |
| 109 |
} |
| 110 |
self::data_set($target[$segment], $segments, $value, $overwrite); |
| 111 |
} elseif ($overwrite || !self::array_exists($target, $segment)) { |
| 112 |
$target[$segment] = $value; |
| 113 |
} |
| 114 |
} elseif (is_object($target)) { |
| 115 |
if ($segments) { |
| 116 |
if (!isset($target->{$segment})) { |
| 117 |
$target->{$segment} = []; |
| 118 |
} |
| 119 |
self::data_set($target->{$segment}, $segments, $value, $overwrite); |
| 120 |
} elseif ($overwrite || !isset($target->{$segment})) { |
| 121 |
$target->{$segment} = $value; |
| 122 |
} |
| 123 |
} else { |
| 124 |
$target = []; |
| 125 |
if ($segments) { |
| 126 |
self::data_set($target[$segment], $segments, $value, $overwrite); |
| 127 |
} elseif ($overwrite) { |
| 128 |
$target[$segment] = $value; |
| 129 |
} |
| 130 |
} |
| 131 |
return $target; |
| 132 |
} |
| 133 |
/** |
| 134 |
* Determine if the given key exists in the provided array. |
| 135 |
* |
| 136 |
* @param \ArrayAccess|array $array |
| 137 |
* @param string|int $key |
| 138 |
* @return bool |
| 139 |
* |
| 140 |
* @see https://github.com/laravel/framework/blob/a84c4f41d3fb1c57684bb417b1f0858300e769d0/src/Illuminate/Collections/Arr.php#L164 |
| 141 |
*/ |
| 142 |
public static function array_exists($array, $key) |
| 143 |
{ |
| 144 |
if ($array instanceof ArrayAccess) { |
| 145 |
return $array->offsetExists($key); |
| 146 |
} |
| 147 |
if (is_float($key)) { |
| 148 |
$key = (string) $key; |
| 149 |
} |
| 150 |
return array_key_exists($key, $array); |
| 151 |
} |
| 152 |
/** |
| 153 |
* Determine whether the given value is array accessible. |
| 154 |
* |
| 155 |
* @param mixed $value |
| 156 |
* @return bool |
| 157 |
* |
| 158 |
* @see https://github.com/laravel/framework/blob/a84c4f41d3fb1c57684bb417b1f0858300e769d0/src/Illuminate/Collections/Arr.php#L21 |
| 159 |
*/ |
| 160 |
public static function array_accessible($value) |
| 161 |
{ |
| 162 |
return is_array($value) || $value instanceof ArrayAccess; |
| 163 |
} |
| 164 |
} |
| 165 |
|