1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Container; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A reference to another entry in a {@link Container}. |
12
|
|
|
*/ |
13
|
|
|
class Reference implements ReferenceInterface { |
14
|
|
|
/** |
15
|
|
|
* @var string|array |
16
|
|
|
*/ |
17
|
|
|
private $name; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $args; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Construct a new instance of the {@link Reference} class. |
26
|
|
|
* |
27
|
|
|
* @param string|array $name The name of the reference. |
28
|
|
|
* @param array $args Constructor arguments for the reference. |
29
|
|
|
*/ |
30
|
16 |
|
public function __construct($name, array $args = []) { |
31
|
16 |
|
$this->setName($name); |
32
|
16 |
|
$this->setArgs($args); |
33
|
16 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the name of the reference. |
37
|
|
|
* |
38
|
|
|
* @return string|array Returns the name of the reference. |
39
|
|
|
*/ |
40
|
1 |
|
public function getName() { |
41
|
1 |
|
return $this->name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set the name of the reference. |
46
|
|
|
* |
47
|
|
|
* @param string|array $name The name of the reference. |
48
|
|
|
*/ |
49
|
16 |
|
public function setName($name) { |
50
|
16 |
|
$this->name = $name; |
51
|
16 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
13 |
|
public function resolve(Container $container, $_ = null) { |
|
|
|
|
57
|
13 |
|
if (empty($this->name)) { |
58
|
1 |
|
return null; |
59
|
12 |
|
} elseif (is_string($this->name)) { |
60
|
9 |
|
return $container->getArgs($this->name, $this->args); |
61
|
|
|
} else { |
62
|
3 |
|
$result = $container; |
63
|
3 |
|
foreach ($this->name as $name) { |
64
|
3 |
|
$result = $result->get($name); |
65
|
|
|
} |
66
|
3 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get constructor arguments for the the reference. |
72
|
|
|
* |
73
|
|
|
* @return array Returns the arguments. |
74
|
|
|
*/ |
75
|
|
|
public function getArgs() { |
76
|
|
|
return $this->args; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set constructor arguments for the the reference. |
81
|
|
|
* |
82
|
|
|
* @param array $args An array of arguments. |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
16 |
|
public function setArgs($args) { |
86
|
16 |
|
$this->args = $args; |
87
|
16 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.