Completed
Branch FET/11399/verify-paypal-creden... (c7ad03)
by
unknown
66:22 queued 52:43
created

CachingLoader::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 3
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use Closure;
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
 */
22
class CachingLoader extends CachingLoaderDecorator
23
{
24
25
    /**
26
     * @var CollectionInterface $cache
27
     */
28
    protected $cache;
29
30
    /**
31
     * @var string $identifier
32
     */
33
    protected $identifier;
34
35
36
37
    /**
38
     * CachingLoader constructor.
39
     *
40
     * @param LoaderDecoratorInterface $loader
41
     * @param CollectionInterface      $cache
42
     * @param string                   $identifier
43
     * @throws InvalidDataTypeException
44
     */
45
    public function __construct(
46
        LoaderDecoratorInterface $loader,
47
        CollectionInterface $cache,
48
        $identifier = ''
49
    ) {
50
        parent::__construct($loader);
51
        $this->cache = $cache;
52
        $this->setIdentifier($identifier);
53
        if ($this->identifier !== '') {
54
            // to only clear this cache, and assuming an identifier has been set, simply do the following:
55
            // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER');
56
            // where "IDENTIFIER" = the string that was set during construction
57
            add_action(
58
                "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}",
59
                array($this, 'reset')
60
            );
61
        }
62
        // to clear ALL caches, simply do the following:
63
        // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache');
64
        add_action(
65
            'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache',
66
            array($this, 'reset')
67
        );
68
    }
69
70
71
72
    /**
73
     * @return string
74
     */
75
    public function identifier()
76
    {
77
        return $this->identifier;
78
    }
79
80
81
82
    /**
83
     * @param string $identifier
84
     * @throws InvalidDataTypeException
85
     */
86
    private function setIdentifier($identifier)
87
    {
88
        if (! is_string($identifier)) {
89
            throw new InvalidDataTypeException('$identifier', $identifier, 'string');
90
        }
91
        $this->identifier = $identifier;
92
    }
93
94
95
    /**
96
     * @param string $fqcn
97
     * @param mixed  $object
98
     * @return bool
99
     * @throws InvalidArgumentException
100
     */
101
    public function share($fqcn, $object)
102
    {
103
        if ($object instanceof $fqcn) {
104
            return $this->cache->add($object, md5($fqcn));
105
        }
106
        throw new InvalidArgumentException(
107
            sprintf(
108
                esc_html__(
109
                    'The supplied class name "%1$s" must match the class of the supplied object.',
110
                    'event_espresso'
111
                ),
112
                $fqcn
113
            )
114
        );
115
    }
116
117
118
    /**
119
     * @param string $fqcn
120
     * @param array  $arguments
121
     * @param bool   $shared
122
     * @return mixed
123
     */
124
    public function load($fqcn, $arguments = array(), $shared = true)
125
    {
126
        $fqcn = ltrim($fqcn, '\\');
127
        // caching can be turned off via the following code:
128
        // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true');
129
        if(
130
            apply_filters(
131
                'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache',
132
                false,
133
                $this
134
            )
135
        ){
136
            // even though $shared might be true, caching could be bypassed for whatever reason,
137
            // so we don't want the core loader to cache anything, therefore caching is turned off
138
            return $this->loader->load($fqcn, $arguments, false);
139
        }
140
        $identifier = md5($fqcn . $this->getIdentifierForArgument($arguments));
141
        if ($this->cache->has($identifier)) {
142
            return $this->cache->get($identifier);
143
        }
144
        $object = $this->loader->load($fqcn, $arguments, $shared);
145
        if ($object instanceof $fqcn) {
146
            $this->cache->add($object, $identifier);
147
        }
148
        return $object;
149
    }
150
151
152
153
    /**
154
     * empties cache and calls reset() on loader if method exists
155
     */
156
    public function reset()
157
    {
158
        $this->cache->trashAndDetachAll();
159
        $this->loader->reset();
160
    }
161
162
163
164
    /**
165
     * build a string representation of a class' arguments
166
     * (mostly because Closures can't be serialized)
167
     *
168
     * @param array $arguments
169
     * @return string
170
     */
171
    private function getIdentifierForArgument(array $arguments)
172
    {
173
        $identifier = '';
174
        foreach ($arguments as $argument) {
175
            switch (true) {
176
                case is_object($argument) :
177
                case $argument instanceof Closure :
0 ignored issues
show
Bug introduced by
The class Closure does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
178
                    $identifier .= spl_object_hash($argument);
179
                    break;
180
                case is_array($argument) :
181
                    $identifier .= $this->getIdentifierForArgument($argument);
182
                    break;
183
                default :
184
                    $identifier .= $argument;
185
                    break;
186
            }
187
        }
188
        return $identifier;
189
    }
190
191
192
}
193
// End of file CachingLoader.php
194
// Location: EventEspresso\core\services\loaders/CachingLoader.php
195