Completed
Branch FET-10657-loader (efdbe3)
by
unknown
211:33 queued 198:16
created

Loader::getNewLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 1
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
        // if not already generated, create a standard loader
52
        if (! $new_loader instanceof LoaderInterface) {
53
            $new_loader = new CoreLoader(EE_Registry::instance());
54
        }
55
        $this->new_loader = $new_loader;
56
        // if not already generated, create a caching loader
57
        if (! $shared_loader instanceof LoaderInterface) {
58
            $shared_loader = new CachingLoader(
59
                new CoreLoader(EE_Registry::instance()),
60
                new LooseCollection('')
61
            );
62
        }
63
        $this->shared_loader = $shared_loader;
64
    }
65
66
67
68
    /**
69
     * @return LoaderInterface
70
     */
71
    public function getNewLoader()
72
    {
73
        return $this->new_loader;
74
    }
75
76
77
78
    /**
79
     * @return LoaderInterface
80
     */
81
    public function getSharedLoader()
82
    {
83
        return $this->shared_loader;
84
    }
85
86
87
88
    /**
89
     * @param string $fqcn
90
     * @param array  $arguments
91
     * @param bool   $shared
92
     * @return mixed
93
     */
94
    public function load($fqcn, $arguments = array(), $shared = true)
95
    {
96
        return $shared
97
            ? $this->getSharedLoader()->load($fqcn, $arguments)
98
            : $this->getNewLoader()->load($fqcn, $arguments);
99
    }
100
101
}
102
// End of file Loader.php
103
// Location: EventEspresso\core\services\loaders/Loader.php