|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Proxy; |
|
4
|
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Swoole\Server; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* DynamicPropertiesTrait solves the issue that PHP 8.2+ does not allow dynamic property creation. |
|
9
|
|
|
* Provides functionality for storing and accessing dynamic properties. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
trait DynamicPropertiesTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array Stores dynamically created properties (e.g., tables) */ |
|
|
|
|
|
|
14
|
|
|
protected array $dynamicProperties = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
|
|
|
|
|
17
|
|
|
* Dynamic property access |
|
18
|
|
|
*/ |
|
|
|
|
|
|
19
|
|
|
public function __get($name) |
|
20
|
|
|
{ |
|
21
|
|
|
// First check dynamic properties |
|
22
|
|
|
return $this->dynamicProperties[$name] ?? null; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
|
|
|
|
|
26
|
|
|
* Dynamic property setter |
|
27
|
|
|
*/ |
|
|
|
|
|
|
28
|
|
|
public function __set($name, $value) |
|
29
|
|
|
{ |
|
30
|
|
|
// If property name ends with 'Table', store it in dynamic properties |
|
31
|
|
|
if (str_ends_with($name, Server::TABLE_PROPERTY_SUFFIX)) { |
|
32
|
|
|
$this->dynamicProperties[$name] = $value; |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// For other properties, try to set directly on parent class |
|
37
|
|
|
// (Swoole's C extension classes don't have __set, so it won't recurse) |
|
38
|
|
|
try { |
|
39
|
|
|
$this->{$name} = $value; |
|
40
|
|
|
} catch (\Error $e) { |
|
|
|
|
|
|
41
|
|
|
// When PHP 8.2+ doesn't allow dynamic property creation, store in dynamic properties |
|
42
|
|
|
$this->dynamicProperties[$name] = $value; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* Check if property exists |
|
48
|
|
|
*/ |
|
|
|
|
|
|
49
|
|
|
public function __isset($name) |
|
50
|
|
|
{ |
|
51
|
|
|
if (array_key_exists($name, $this->dynamicProperties)) { |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Check parent class properties (Swoole's C extension class properties) |
|
56
|
|
|
return property_exists($this, $name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
|
|
|
|
|
60
|
|
|
* Unset property |
|
61
|
|
|
*/ |
|
|
|
|
|
|
62
|
|
|
public function __unset($name) |
|
63
|
|
|
{ |
|
64
|
|
|
unset($this->dynamicProperties[$name]); |
|
65
|
|
|
|
|
66
|
|
|
if (property_exists($this, $name)) { |
|
67
|
|
|
unset($this->{$name}); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|