|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Properties; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Definitions\Definition; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Registry |
|
9
|
|
|
* @package ByTIC\Models\SmartProperties\Properties |
|
10
|
|
|
*/ |
|
11
|
|
|
class PropertiesRegistry |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $registry = []; |
|
14
|
|
|
|
|
15
|
|
|
public static function set($subject, $property, $value) |
|
16
|
|
|
{ |
|
17
|
|
|
$subjectName = static::subjectName($subject); |
|
18
|
|
|
$propertyName = static::propertyName($property); |
|
19
|
|
|
self::$registry[$subjectName][$propertyName] = $value; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $subject |
|
24
|
|
|
* @param $property |
|
25
|
|
|
* @return mixed|null |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function get($subject, $property) |
|
28
|
|
|
{ |
|
29
|
|
|
$subjectName = static::subjectName($subject); |
|
30
|
|
|
$propertyName = static::propertyName($property); |
|
31
|
|
|
|
|
32
|
|
|
if (!isset(self::$registry[$subjectName][$propertyName])) { |
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
$value = self::$registry[$subjectName][$propertyName]; |
|
36
|
|
|
if ($value instanceof \Closure) { |
|
37
|
|
|
self::$registry[$subjectName][$propertyName] = $value(); |
|
38
|
|
|
} |
|
39
|
|
|
return self::$registry[$subjectName][$propertyName]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $subject |
|
44
|
|
|
* @param $property |
|
45
|
|
|
* @param $init |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function getWithInit($subject, $property, $init) |
|
48
|
|
|
{ |
|
49
|
|
|
$return = static::get($subject, $property); |
|
50
|
|
|
if ($return) { |
|
51
|
|
|
return $return; |
|
52
|
|
|
} |
|
53
|
|
|
if ($init instanceof \Closure) { |
|
54
|
|
|
$init = $init(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$subjectName = static::subjectName($subject); |
|
58
|
|
|
$propertyName = static::propertyName($property); |
|
59
|
|
|
self::$registry[$subjectName][$propertyName] = $init; |
|
60
|
|
|
return $init; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $property |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected static function propertyName($property): string |
|
68
|
|
|
{ |
|
69
|
|
|
if ($property instanceof Definition) { |
|
70
|
|
|
return $property->getName(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
return (string)$property; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $subject |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static function subjectName($subject): string |
|
80
|
|
|
{ |
|
81
|
|
|
$propertyMemory = 'splObjectUnique'; |
|
82
|
|
|
if (isset($subject->{$propertyMemory})) { |
|
83
|
|
|
return $subject->{$propertyMemory}; |
|
84
|
|
|
} |
|
85
|
|
|
$id = uniqid(); |
|
86
|
|
|
$subject->{$propertyMemory} = $id; |
|
87
|
|
|
return $id; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|