1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ObjectIdentifier |
9
|
|
|
* Builds a string representation of an object's FQCN and arguments |
10
|
|
|
* used for identifying whether two objects are functionally equivalent |
11
|
|
|
* |
12
|
|
|
* @package EventEspresso\core\services\loaders |
13
|
|
|
* @author Brent Christensen |
14
|
|
|
* @since $VID:$ |
15
|
|
|
*/ |
16
|
|
|
class ObjectIdentifier |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* used to separate the FQCN from the class's arguments identifier |
21
|
|
|
*/ |
22
|
|
|
const DELIMITER = '____'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ClassInterfaceCache $class_cache |
26
|
|
|
*/ |
27
|
|
|
private $class_cache; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ObjectIdentifier constructor. |
32
|
|
|
* |
33
|
|
|
* @param ClassInterfaceCache $class_cache |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ClassInterfaceCache $class_cache) |
36
|
|
|
{ |
37
|
|
|
$this->class_cache = $class_cache; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns true if the supplied $object_identifier contains |
43
|
|
|
* the delimiter used to separate an fqcn from the arguments hash |
44
|
|
|
* |
45
|
|
|
* @param string $object_identifier |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function hasArguments($object_identifier) |
49
|
|
|
{ |
50
|
|
|
// type casting to bool instead of using strpos() !== false |
51
|
|
|
// because an object identifier should never begin with the delimiter |
52
|
|
|
// therefore the delimiter should NOT be found at position 0 |
53
|
|
|
return (bool) strpos($object_identifier, ObjectIdentifier::DELIMITER); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns true if the supplied FQCN equals the supplied $object_identifier |
59
|
|
|
* OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier |
60
|
|
|
* AND that $object_identifier is for an object with arguments. |
61
|
|
|
* This allows a request for an object using a FQCN to match |
62
|
|
|
* a previously instantiated object with arguments |
63
|
|
|
* without having to know those arguments. |
64
|
|
|
* |
65
|
|
|
* @param string $fqcn |
66
|
|
|
* @param string $object_identifier |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function fqcnMatchesObjectIdentifier($fqcn, $object_identifier) |
70
|
|
|
{ |
71
|
|
|
return $fqcn === $object_identifier |
72
|
|
|
|| strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* build a string representation of an object's FQCN and arguments |
78
|
|
|
* |
79
|
|
|
* @param string $fqcn |
80
|
|
|
* @param array $arguments |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getIdentifier($fqcn, array $arguments = array()) |
84
|
|
|
{ |
85
|
|
|
// only build identifier from arguments if class is not ReservedInstanceInterface |
86
|
|
|
$identifier = ! $this->class_cache->hasInterface( |
87
|
|
|
$fqcn, |
88
|
|
|
'EventEspresso\core\interfaces\ReservedInstanceInterface' |
89
|
|
|
) |
90
|
|
|
? $this->getIdentifierForArguments($arguments) |
91
|
|
|
: ''; |
92
|
|
|
if (! empty($identifier)) { |
93
|
|
|
$fqcn .= ObjectIdentifier::DELIMITER . md5($identifier); |
94
|
|
|
} |
95
|
|
|
return $fqcn; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* build a string representation of a object's arguments |
101
|
|
|
* (mostly because Closures can't be serialized) |
102
|
|
|
* |
103
|
|
|
* @param array $arguments |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function getIdentifierForArguments(array $arguments) |
107
|
|
|
{ |
108
|
|
|
if (empty($arguments)) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
$identifier = ''; |
112
|
|
|
foreach ($arguments as $argument) { |
113
|
|
|
switch (true) { |
114
|
|
|
case is_object($argument): |
115
|
|
|
case $argument instanceof Closure: |
116
|
|
|
$identifier .= spl_object_hash($argument); |
117
|
|
|
break; |
118
|
|
|
case is_array($argument): |
119
|
|
|
$identifier .= $this->getIdentifierForArguments($argument); |
120
|
|
|
break; |
121
|
|
|
default: |
122
|
|
|
$identifier .= $argument; |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return $identifier; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|