|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Dynamic property storage handler for WooCommerce order objects. |
| 4 | + * |
| 5 | + * Provides a PHP 8.2+ compatible way to store dynamic properties on order objects |
| 6 | + * while maintaining backwards compatibility with PHP 7.4+. |
| 7 | + * |
| 8 | + * @package SkyVerge/WooCommerce/Payment-Gateway/Classes |
| 9 | + * @since x.x.x |
| 10 | + */ |
| 11 | + |
| 12 | +namespace SkyVerge\WooCommerce\PluginFramework\v6_0_0\Payment_Gateway; |
| 13 | + |
| 14 | +/** |
| 15 | + * Dynamic property storage handler for WooCommerce order objects. |
| 16 | + * |
| 17 | + * This class provides a way to store dynamic properties on order objects without using |
| 18 | + * dynamic properties (deprecated in PHP 8.2+) while maintaining backwards compatibility |
| 19 | + * with PHP 7.4+. It uses WeakMap for PHP 8.0+ and falls back to dynamic properties |
| 20 | + * for PHP 7.4+. |
| 21 | + * |
| 22 | + * @since x.x.x |
| 23 | + * |
| 24 | + * @example |
| 25 | + * ```php |
| 26 | + * // Store properties |
| 27 | + * Dynamic_Props::set($order, 'customer_id', 123); |
| 28 | + * Dynamic_Props::set($order, 'payment_total', 99.99); |
| 29 | + * |
| 30 | + * // Retrieve properties |
| 31 | + * $customer_id = Dynamic_Props::get($order, 'customer_id'); |
| 32 | + * $total = Dynamic_Props::get($order, 'payment_total'); |
| 33 | + * ``` |
| 34 | + */ |
| 35 | +class Dynamic_Props { |
| 36 | + /** |
| 37 | + * Storage container for dynamic properties using WeakMap in PHP 8.0+. |
| 38 | + * |
| 39 | + * Uses WeakMap to store properties without memory leaks, as WeakMap allows garbage |
| 40 | + * collection of its keys when they're no longer referenced elsewhere. |
| 41 | + * |
| 42 | + * @since x.x.x |
| 43 | + * @var \WeakMap<object, \stdClass>|null |
| 44 | + */ |
| 45 | + private static ?\WeakMap $map = null; |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets a property on the order object. |
| 49 | + * |
| 50 | + * Stores a dynamic property either using WeakMap (PHP 8.0+) or direct property |
| 51 | + * access (PHP 7.4+). The storage method is automatically determined based on |
| 52 | + * PHP version and WeakMap availability. |
| 53 | + * |
| 54 | + * @since x.x.x |
| 55 | + * |
| 56 | + * @param \WC_Order $order The order object to store data on. |
| 57 | + * @param string $key The property key. |
| 58 | + * @param mixed $value The value to store. |
| 59 | + * @return void |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```php |
| 63 | + * Dynamic_Props::set($order, 'customer_id', 123); |
| 64 | + * Dynamic_Props::set($order, 'payment_total', '99.99'); |
| 65 | + * ``` |
| 66 | + */ |
| 67 | + public static function set( \WC_Order &$order, string $key, mixed $value ): void { |
| 68 | + if ( self::use_weak_map() ) { |
| 69 | + self::init_weak_map(); |
| 70 | + if ( ! isset( self::$map[ $order ] ) ) { |
| 71 | + self::$map[ $order ] = new \stdClass(); |
| 72 | + } |
| 73 | + self::$map[ $order ]->{ $key } = $value; |
| 74 | + } else { |
| 75 | + $order->{ $key } = $value; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets a property from the order object. |
| 81 | + * |
| 82 | + * Retrieves a stored dynamic property using the appropriate storage method |
| 83 | + * based on PHP version. Supports nested property access. |
| 84 | + * |
| 85 | + * @since x.x.x |
| 86 | + * |
| 87 | + * @param \WC_Order $order The order object to retrieve data from. |
| 88 | + * @param string $key The property key. |
| 89 | + * @param string $nested_key Optional. The nested property key. Default null. |
| 90 | + * @param mixed $default Optional. Default value if not found. Default null. |
| 91 | + * @return mixed The stored value or default if not found. |
| 92 | + * |
| 93 | + * @example |
| 94 | + * ```php |
| 95 | + * $customer_id = Dynamic_Props::get($order, 'customer_id'); |
| 96 | + * $total = Dynamic_Props::get($order, 'payment_total'); |
| 97 | + * $token = Dynamic_Props::get($order, 'payment', 'token', 'DEFAULT_TOKEN'); |
| 98 | + * ``` |
| 99 | + */ |
| 100 | + public static function get( \WC_Order $order, string $key, $nested_key = null, $default = null ): mixed { |
| 101 | + if ( self::use_weak_map() ) { |
| 102 | + self::init_weak_map(); |
| 103 | + if ( is_null( $nested_key ) ) { |
| 104 | + return self::$map[ $order ]->{ $key } ?? $default; |
| 105 | + } else { |
| 106 | + return self::$map[ $order ]->{ $key }->{ $nested_key } ?? $default; |
| 107 | + } |
| 108 | + } |
| 109 | + if ( is_null( $nested_key ) ) { |
| 110 | + return $order->{ $key } ?? $default; |
| 111 | + } else { |
| 112 | + return $order->{ $key }->{ $nested_key } ?? $default; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Unsets a property on the order object. |
| 118 | + * |
| 119 | + * Removes a stored dynamic property using the appropriate storage method |
| 120 | + * based on PHP version. |
| 121 | + * |
| 122 | + * @since x.x.x |
| 123 | + * |
| 124 | + * @param \WC_Order $order The order object to unset data from. |
| 125 | + * @param string $key The property key to remove. |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public static function unset( \WC_Order &$order, string $key ): void { |
| 129 | + if ( self::use_weak_map() ) { |
| 130 | + self::init_weak_map(); |
| 131 | + unset( self::$map[ $order ]->{ $key } ); |
| 132 | + } else { |
| 133 | + unset( $order->{ $key } ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Checks if Dynamic_Props class should be used based on the filter. |
| 139 | + * |
| 140 | + * @return bool True if Dynamic_Props class should be used, false otherwise. |
| 141 | + */ |
| 142 | + private static function use_dynamic_props_class(): bool { |
| 143 | + static $use_dynamic_props_class = null; |
| 144 | + if ( null === $use_dynamic_props_class ) { |
| 145 | + /** |
| 146 | + * Filters whether to use Dynamic_Props class for storing order data. |
| 147 | + * |
| 148 | + * @since x.x.x |
| 149 | + * |
| 150 | + * @var bool Whether to Dynamic_Props class for storing order data. |
| 151 | + */ |
| 152 | + $use_dynamic_props_class = apply_filters( 'sv_wc_plugin_framework_use_dynamic_props_class', false ); |
| 153 | + } |
| 154 | + return $use_dynamic_props_class; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Checks if WeakMap should be used based on PHP version. |
| 159 | + * |
| 160 | + * Determines whether to use WeakMap storage based on PHP version (8.0+) |
| 161 | + * and WeakMap class availability. Result is cached for performance. |
| 162 | + * |
| 163 | + * @since x.x.x |
| 164 | + * @return bool True if WeakMap should be used, false otherwise. |
| 165 | + */ |
| 166 | + private static function use_weak_map(): bool { |
| 167 | + static $use_weak_map = null; |
| 168 | + |
| 169 | + if ( null === $use_weak_map ) { |
| 170 | + $use_weak_map = version_compare( PHP_VERSION, '8.0', '>=' ) && self::use_dynamic_props_class(); |
| 171 | + } |
| 172 | + |
| 173 | + return $use_weak_map; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Initializes WeakMap storage if not already initialized. |
| 178 | + * |
| 179 | + * Ensures the WeakMap storage is initialized only once when needed. |
| 180 | + * This lazy initialization helps with performance and memory usage. |
| 181 | + * |
| 182 | + * @since x.x.x |
| 183 | + * @return void |
| 184 | + */ |
| 185 | + private static function init_weak_map(): void { |
| 186 | + if ( null === self::$map ) { |
| 187 | + self::$map = new \WeakMap(); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments