Completed
Branch BUG-10669-spco-validation-msg (4c1309)
by
unknown
49:56 queued 37:41
created

Loader::setupNewLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EE_Registry;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidInterfaceException;
8
use EventEspresso\core\services\collections\LooseCollection;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
15
/**
16
 * Class Loader
17
 * Provides a common interface for generating new or shared instantiations of classes
18
 *
19
 * @package       Event Espresso
20
 * @author        Brent Christensen
21
 * @since         $VID:$
22
 */
23
class Loader implements LoaderInterface
24
{
25
26
27
    /**
28
     * @var LoaderInterface $new_loader
29
     */
30
    private $new_loader;
31
32
33
    /**
34
     * @var LoaderInterface $shared_loader
35
     */
36
    private $shared_loader;
37
38
39
40
    /**
41
     * Loader constructor.
42
     *
43
     * @param LoaderInterface|null $new_loader
44
     * @param LoaderInterface|null $shared_loader
45
     * @throws InvalidInterfaceException
46
     * @throws InvalidArgumentException
47
     * @throws InvalidDataTypeException
48
     */
49
    public function __construct(LoaderInterface $new_loader = null, LoaderInterface $shared_loader = null)
50
    {
51
        $this->new_loader = $this->setupNewLoader($new_loader);
52
        $this->shared_loader = $this->setupSharedLoader($shared_loader);
53
    }
54
55
56
57
    /**
58
     * @param LoaderInterface|null $new_loader
59
     * @return CoreLoader|LoaderInterface
60
     * @throws InvalidArgumentException
61
     */
62
    private function setupNewLoader(LoaderInterface $new_loader = null)
63
    {
64
        // if not already generated, create a standard loader
65
        if (! $new_loader instanceof LoaderInterface) {
66
            $new_loader = new CoreLoader(EE_Registry::instance());
67
        }
68
        return $new_loader;
69
    }
70
71
72
73
    /**
74
     * @param LoaderInterface|null $shared_loader
75
     * @return CoreLoader|LoaderInterface
76
     * @throws InvalidDataTypeException
77
     * @throws InvalidInterfaceException
78
     * @throws InvalidArgumentException
79
     */
80
    private function setupSharedLoader(LoaderInterface $shared_loader = null)
81
    {
82
        // if not already generated, create a caching loader
83
        if (! $shared_loader instanceof LoaderInterface) {
84
            $shared_loader = new CachingLoader(
85
                new CoreLoader(EE_Registry::instance()),
86
                new LooseCollection('')
87
            );
88
        }
89
        return $shared_loader;
90
    }
91
92
93
94
    /**
95
     * @return LoaderInterface
96
     */
97
    public function getNewLoader()
98
    {
99
        return $this->new_loader;
100
    }
101
102
103
104
    /**
105
     * @return LoaderInterface
106
     */
107
    public function getSharedLoader()
108
    {
109
        return $this->shared_loader;
110
    }
111
112
113
114
    /**
115
     * @param string $fqcn
116
     * @param array  $arguments
117
     * @param bool   $shared
118
     * @return mixed
119
     */
120
    public function load($fqcn, $arguments = array(), $shared = true)
121
    {
122
        return $shared
123
            ? $this->getSharedLoader()->load($fqcn, $arguments)
124
            : $this->getNewLoader()->load($fqcn, $arguments);
125
    }
126
127
128
129
    /**
130
     * @param string $fqcn
131
     * @param array  $arguments
132
     * @return mixed
133
     */
134
    public function getNew($fqcn, $arguments = array())
135
    {
136
        return $this->getNewLoader()->load($fqcn, $arguments);
137
    }
138
139
140
141
    /**
142
     * @param string $fqcn
143
     * @param array  $arguments
144
     * @return mixed
145
     */
146
    public function getShared($fqcn, $arguments = array())
147
    {
148
        return $this->getSharedLoader()->load($fqcn, $arguments);
149
    }
150
151
152
153
    /**
154
     * calls reset() on loaders if that method exists
155
     */
156
    public function reset()
157
    {
158
        $this->new_loader->reset();
159
        $this->shared_loader->reset();
160
    }
161
162
}
163
// End of file Loader.php
164
// Location: EventEspresso\core\services\loaders/Loader.php