1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\loaders; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\values\FullyQualifiedName; |
6
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
7
|
|
|
use EventEspresso\core\services\collections\CollectionInterface; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CachingLoader |
16
|
|
|
* caches objects returned by the decorated loader |
17
|
|
|
* |
18
|
|
|
* @package Event Espresso |
19
|
|
|
* @author Brent Christensen |
20
|
|
|
*/ |
21
|
|
|
class CachingLoader extends CachingLoaderDecorator |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string $identifier |
26
|
|
|
*/ |
27
|
|
|
protected $identifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CollectionInterface $cache |
31
|
|
|
*/ |
32
|
|
|
protected $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ObjectIdentifier |
36
|
|
|
*/ |
37
|
|
|
private $object_identifier; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* CachingLoader constructor. |
42
|
|
|
* |
43
|
|
|
* @param LoaderDecoratorInterface $loader |
44
|
|
|
* @param CollectionInterface $cache |
45
|
|
|
* @param ObjectIdentifier $object_identifier |
46
|
|
|
* @param string $identifier |
47
|
|
|
* @throws InvalidDataTypeException |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
LoaderDecoratorInterface $loader, |
51
|
|
|
CollectionInterface $cache, |
52
|
|
|
ObjectIdentifier $object_identifier, |
53
|
|
|
$identifier = '' |
54
|
|
|
) { |
55
|
|
|
parent::__construct($loader); |
56
|
|
|
$this->cache = $cache; |
57
|
|
|
$this->object_identifier = $object_identifier; |
58
|
|
|
$this->setIdentifier($identifier); |
59
|
|
|
if ($this->identifier !== '') { |
60
|
|
|
// to only clear this cache, and assuming an identifier has been set, simply do the following: |
61
|
|
|
// do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER'); |
62
|
|
|
// where "IDENTIFIER" = the string that was set during construction |
63
|
|
|
add_action( |
64
|
|
|
"AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
65
|
|
|
array($this, 'reset') |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
// to clear ALL caches, simply do the following: |
69
|
|
|
// do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
70
|
|
|
add_action( |
71
|
|
|
'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
72
|
|
|
array($this, 'reset') |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function identifier() |
81
|
|
|
{ |
82
|
|
|
return $this->identifier; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $identifier |
88
|
|
|
* @throws InvalidDataTypeException |
89
|
|
|
*/ |
90
|
|
|
private function setIdentifier($identifier) |
91
|
|
|
{ |
92
|
|
|
if (! is_string($identifier)) { |
93
|
|
|
throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
94
|
|
|
} |
95
|
|
|
$this->identifier = $identifier; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param FullyQualifiedName|string $fqcn |
101
|
|
|
* @param mixed $object |
102
|
|
|
* @return bool |
103
|
|
|
* @throws InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function share($fqcn, $object) |
106
|
|
|
{ |
107
|
|
|
if ($object instanceof $fqcn) { |
108
|
|
|
return $this->cache->add($object, md5($fqcn)); |
109
|
|
|
} |
110
|
|
|
throw new InvalidArgumentException( |
111
|
|
|
sprintf( |
112
|
|
|
esc_html__( |
113
|
|
|
'The supplied class name "%1$s" must match the class of the supplied object.', |
114
|
|
|
'event_espresso' |
115
|
|
|
), |
116
|
|
|
$fqcn |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param FullyQualifiedName|string $fqcn |
124
|
|
|
* @param array $arguments |
125
|
|
|
* @param bool $shared |
126
|
|
|
* @param array $interfaces |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function load($fqcn, $arguments = array(), $shared = true, array $interfaces = array()) |
130
|
|
|
{ |
131
|
|
|
$fqcn = ltrim($fqcn, '\\'); |
132
|
|
|
// caching can be turned off via the following code: |
133
|
|
|
// add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
134
|
|
|
if ( |
135
|
|
|
apply_filters( |
136
|
|
|
'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
137
|
|
|
false, |
138
|
|
|
$this |
139
|
|
|
) |
140
|
|
|
) { |
141
|
|
|
// even though $shared might be true, caching could be bypassed for whatever reason, |
142
|
|
|
// so we don't want the core loader to cache anything, therefore caching is turned off |
143
|
|
|
return $this->loader->load($fqcn, $arguments, false); |
144
|
|
|
} |
145
|
|
|
$object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments); |
146
|
|
|
if ($this->cache->has($object_identifier)) { |
147
|
|
|
return $this->cache->get($object_identifier); |
148
|
|
|
} |
149
|
|
|
$object = $this->loader->load($fqcn, $arguments, $shared); |
150
|
|
|
if ($object instanceof $fqcn) { |
151
|
|
|
$this->cache->add($object, $object_identifier); |
152
|
|
|
} |
153
|
|
|
return $object; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* empties cache and calls reset() on loader if method exists |
159
|
|
|
*/ |
160
|
|
|
public function reset() |
161
|
|
|
{ |
162
|
|
|
$this->clearCache(); |
163
|
|
|
$this->loader->reset(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* unsets and detaches ALL objects from the cache |
169
|
|
|
* |
170
|
|
|
* @since $VID:$ |
171
|
|
|
*/ |
172
|
|
|
public function clearCache() |
173
|
|
|
{ |
174
|
|
|
$this->cache->trashAndDetachAll(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
// End of file CachingLoader.php |
178
|
|
|
// Location: EventEspresso\core\services\loaders/CachingLoader.php |
179
|
|
|
|