Completed
Branch FET-10785-ee-system-loader (4ec117)
by
unknown
139:17 queued 127:33
created

CachingLoader::identifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
use EventEspresso\core\exceptions\InvalidEntityException;
7
use EventEspresso\core\services\collections\CollectionInterface;
8
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException;
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
 * @since         $VID:$
21
 */
22
class CachingLoader extends LoaderDecorator
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(LoaderDecoratorInterface $loader, CollectionInterface $cache, $identifier = '')
46
    {
47
        parent::__construct($loader);
48
        $this->cache = $cache;
49
        $this->setIdentifier($identifier);
50
        if ($this->identifier !== '') {
51
            // to only clear this cache, and assuming an identifier has been set, simply do the following:
52
            // do_action('AHEE__EventEspresso\core\services\loaders\CachingLoader__resetCache__IDENTIFIER');
53
            // where "IDENTIFIER" = the string that was set during construction
54
            add_action(
55
                "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}",
56
                array($this, 'reset')
57
            );
58
        }
59
        // to clear ALL caches, simply do the following:
60
        // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache');
61
        add_action(
62
            'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache',
63
            array($this, 'reset')
64
        );
65
    }
66
67
68
69
    /**
70
     * @return string
71
     */
72
    public function identifier()
73
    {
74
        return $this->identifier;
75
    }
76
77
78
79
    /**
80
     * @param string $identifier
81
     * @throws InvalidDataTypeException
82
     */
83
    private function setIdentifier($identifier)
84
    {
85
        if ( ! is_string($identifier)) {
86
            throw new InvalidDataTypeException('$identifier', $identifier, 'string');
87
        }
88
        $this->identifier = $identifier;
89
    }
90
91
92
93
    /**
94
     * @param string $fqcn
95
     * @param array  $arguments
96
     * @return mixed
97
     * @throws InvalidEntityException
98
     * @throws ServiceNotFoundException
99
     */
100
    public function load($fqcn, $arguments = array())
101
    {
102
        $fqcn = ltrim($fqcn, '\\');
103
        // caching can be turned off via the following code:
104
        // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true');
105
        if(
106
            apply_filters(
107
                'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache',
108
                false,
109
                $this
110
            )
111
        ){
112
            return $this->loader->load($fqcn, $arguments);
113
        }
114
        $identifier = md5($fqcn . serialize($arguments));
115
        if($this->cache->has($identifier)){
116
            return $this->cache->get($identifier);
117
        }
118
        $object = $this->loader->load($fqcn, $arguments);
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()
131
    {
132
        $this->cache->detachAll();
133
        $this->loader->reset();
134
    }
135
136
137
}
138
// End of file CachingLoader.php
139
// Location: EventEspresso\core\services\loaders/CachingLoader.php
140