1 | <?php |
||
21 | class CachingLoader extends LoaderDecorator |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var CollectionInterface $cache |
||
26 | */ |
||
27 | protected $cache; |
||
28 | |||
29 | /** |
||
30 | * @var string $identifier |
||
31 | */ |
||
32 | protected $identifier; |
||
33 | |||
34 | |||
35 | |||
36 | /** |
||
37 | * CachingLoader constructor. |
||
38 | * |
||
39 | * @param LoaderDecoratorInterface $loader |
||
40 | * @param CollectionInterface $cache |
||
41 | * @param string $identifier |
||
42 | * @throws InvalidDataTypeException |
||
43 | */ |
||
44 | public function __construct(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '') |
||
45 | { |
||
46 | parent::__construct($loader); |
||
47 | $this->cache = $cache; |
||
48 | $this->setIdentifier($identifier); |
||
49 | if ($this->identifier !== '') { |
||
50 | // to only clear this cache, and assuming an identifier has been set, simply do the following: |
||
51 | // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER'); |
||
52 | // where "IDENTIFIER" = the string that was set during construction |
||
53 | add_action( |
||
54 | "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}", |
||
55 | array($this, 'reset') |
||
56 | ); |
||
57 | } |
||
58 | // to clear ALL caches, simply do the following: |
||
59 | // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache'); |
||
60 | add_action( |
||
61 | 'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache', |
||
62 | array($this, 'reset') |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | public function identifier() |
||
72 | { |
||
73 | return $this->identifier; |
||
74 | } |
||
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * @param string $identifier |
||
80 | * @throws InvalidDataTypeException |
||
81 | */ |
||
82 | private function setIdentifier($identifier) |
||
83 | { |
||
84 | if ( ! is_string($identifier)) { |
||
85 | throw new InvalidDataTypeException('$identifier', $identifier, 'string'); |
||
86 | } |
||
87 | $this->identifier = $identifier; |
||
88 | } |
||
89 | |||
90 | |||
91 | |||
92 | /** |
||
93 | * @param string $fqcn |
||
94 | * @param array $arguments |
||
95 | * @param bool $shared |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function load($fqcn, $arguments = array(), $shared = true) |
||
99 | { |
||
100 | $fqcn = ltrim($fqcn, '\\'); |
||
101 | // caching can be turned off via the following code: |
||
102 | // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true'); |
||
103 | if( |
||
104 | apply_filters( |
||
105 | 'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', |
||
106 | false, |
||
107 | $this |
||
108 | ) |
||
109 | ){ |
||
110 | // even though $shared might be true, caching should be bypassed for whatever reason, |
||
111 | // so we don't want the core loader to cache anything, therefore caching is turned off |
||
112 | return $this->loader->load($fqcn, $arguments, false); |
||
113 | } |
||
114 | $identifier = md5($fqcn . $this->getIdentifierForArgument($arguments)); |
||
115 | if($this->cache->has($identifier)){ |
||
116 | return $this->cache->get($identifier); |
||
117 | } |
||
118 | $object = $this->loader->load($fqcn, $arguments, $shared); |
||
119 | if($object instanceof $fqcn){ |
||
120 | $this->cache->add($object, $identifier); |
||
121 | } |
||
122 | return $object; |
||
123 | } |
||
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * empties cache and calls reset() on loader if method exists |
||
129 | */ |
||
130 | public function reset() |
||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * build a string representation of a class' arguments |
||
140 | * (mostly because Closures can't be serialized) |
||
141 | * |
||
142 | * @param array $arguments |
||
143 | * @return string |
||
144 | */ |
||
145 | private function getIdentifierForArgument(array $arguments) |
||
164 | |||
165 | |||
166 | } |
||
167 | // End of file CachingLoader.php |
||
169 |