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