|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Fuel\Dependency |
|
4
|
|
|
* @version 2.0 |
|
5
|
|
|
* @author Fuel Development Team |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2010 - 2015 Fuel Development Team |
|
8
|
|
|
* @link http://fuelphp.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fuel\Dependency; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Dependency container |
|
15
|
|
|
* |
|
16
|
|
|
* @package Fuel\Dependency |
|
17
|
|
|
* |
|
18
|
|
|
* @since 2.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Container extends \League\Container\Container |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new instance of alias regardless of it being singleton or not |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $alias |
|
26
|
|
|
* @param array $args |
|
27
|
|
|
* |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function forge($alias, array $args = []) |
|
31
|
|
|
{ |
|
32
|
1 |
|
return $this->get($alias, $args); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Resolves a named instance from the container |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $alias |
|
39
|
|
|
* @param string $instance |
|
40
|
|
|
* @param array $args |
|
41
|
|
|
* |
|
42
|
|
|
* @return mixed |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function multiton($alias, $instance = '__default__', array $args = []) |
|
45
|
|
|
{ |
|
46
|
1 |
|
if (1 === func_num_args()) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$multitons = []; |
|
49
|
1 |
|
foreach ($this->sharedDefinitions as $name => $value) |
|
50
|
|
|
{ |
|
51
|
1 |
|
if (0 === strpos($name, $alias.'::')) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$multitons[substr($name, strlen($alias)+2)] = $value; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
1 |
|
return $multitons; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$name = $alias.'::'.$instance; |
|
60
|
|
|
|
|
61
|
|
|
// It is a singleton with a special name |
|
62
|
1 |
|
if ($this->hasShared($name)) |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->sharedDefinitions[$name]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Disable singleton so the resolved concrete does not gets stored |
|
68
|
1 |
|
if ($this->has($alias) and isset($this->sharedDefinitions[$alias])) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$previousSingletonSetting = $this->sharedDefinitions[$alias]; |
|
71
|
1 |
|
unset($this->sharedDefinitions[$alias]); |
|
72
|
1 |
|
$this->definitions[$alias] = $previousSingletonSetting; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$concrete = $this->sharedDefinitions[$name] = $this->get($alias, $args); |
|
76
|
|
|
|
|
77
|
|
|
// Reset to the previous value |
|
78
|
1 |
|
if (isset($previousSingletonSetting)) |
|
79
|
|
|
{ |
|
80
|
1 |
|
unset($this->definitions[$alias]); |
|
81
|
1 |
|
$this->sharedDefinitions[$alias] = $previousSingletonSetting; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return $concrete; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|