-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyContainer.php
More file actions
118 lines (109 loc) · 3.21 KB
/
DependencyContainer.php
File metadata and controls
118 lines (109 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Definition of class DependencyContainer
*
* @copyright 2015-today Justso GmbH
* @author j.schirrmacher@justso.de
* @package justso\justapi
*/
namespace justso\justapi;
/**
* Class DependencyContainer
*
* @package justso\justapi
*/
class DependencyContainer implements DependencyContainerInterface
{
/**
* @var array
*/
private $config = null;
/**
* @var SystemEnvironmentInterface
*/
private $env;
private $singletons = [];
/**
* @param SystemEnvironmentInterface $env
*/
public function __construct(SystemEnvironmentInterface $env)
{
$this->env = $env;
}
/**
* Create new objects of a class or interface with this method.
* It uses a mapping table to map the given $name to a implementing class, thus providing a kind of DIC.
*
* @param string $name
* @param array $arguments for constructor
* @return object
*/
public function get($name, array $arguments = null)
{
$this->load();
if ($arguments === null) {
$arguments = [$this->env];
}
if (isset($this->config[$name])) {
$entry = $this->config[$name];
if (is_callable($entry)) {
return $entry(...$arguments);
} elseif (is_object($entry)) {
return $entry;
} else {
$name = $entry;
}
}
return new $name(...$arguments);
}
/**
* Defines a dependency entry to be a singleton object.
* This function should only be called in your `dependencies.php` file to make an entry being a singleton.
* Example:
* 'UserRepository' => $this->singleton(UserRepository::class, [$this->env])
*
* After that, each call to `get()` will return the singleton object, which is created only when `get()` is called
* the first time.
*
* @param string $className
* @param array $arguments for constructor
* @return callable
*/
public function singleton($className, $arguments)
{
$singletonNo = count($this->singletons) + 1;
return function () use ($className, $arguments, $singletonNo) {
if (!isset($this->singletons[$singletonNo])) {
$this->singletons[$singletonNo] = new $className(...$arguments);
}
return $this->singletons[$singletonNo];
};
}
/**
* Sets a new config entry for the given name.
*
* @param string $name
* @param string|callback|object $entry
*/
public function setDICEntry($name, $entry)
{
$this->load();
$this->config[$name] = $entry;
}
/**
* Loads the configuration from file.
*/
private function load()
{
if ($this->config === null) {
$this->config = [];
$localPath = '/conf/dependencies.php';
$appRoot = $this->env->getBootstrap()->getAppRoot();
$fs = $this->env->getFileSystem();
if ($fs->fileExists($appRoot . $localPath)) {
/** @noinspection PhpIncludeInspection */
$this->config = require_once($fs->getRealPath($appRoot . $localPath));
}
}
}
}