1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Zero Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Nuno Maduro <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace NunoMaduro\ZeroFramework; |
13
|
|
|
|
14
|
|
|
use BadMethodCallException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The is the Zero Framework container proxy class. |
18
|
|
|
* |
19
|
|
|
* @author Nuno Maduro <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait ContainerProxyTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Proxies calls into the container. |
25
|
|
|
* |
26
|
|
|
* @param string $method |
27
|
|
|
* @param array $parameters |
28
|
|
|
* |
29
|
|
|
* @throws \BadMethodCallException |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
public function __call(string $method, array $parameters) |
34
|
|
|
{ |
35
|
|
|
if (is_callable([$this->container, $method])) { |
|
|
|
|
36
|
|
|
return call_user_func_array([$this->container, $method], $parameters); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
throw new BadMethodCallException("Method [{$method}] does not exist."); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Dynamically access container services. |
44
|
|
|
* |
45
|
|
|
* @param string $key |
46
|
|
|
* |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function __get(string $key) |
50
|
|
|
{ |
51
|
|
|
return $this->container->{$key}; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Dynamically set container services. |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* @param mixed $value |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function __set(string $key, $value): void |
63
|
|
|
{ |
64
|
|
|
$this->container->{$key} = $value; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determine if a given offset exists. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function offsetExists($key): bool |
75
|
|
|
{ |
76
|
|
|
return isset($this->container[$key]); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the value at a given offset. |
81
|
|
|
* |
82
|
|
|
* @param string $key |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function offsetGet($key) |
87
|
|
|
{ |
88
|
|
|
return $this->container[$key]; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the value at a given offset. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @param mixed $value |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function offsetSet($key, $value): void |
100
|
|
|
{ |
101
|
|
|
$this->container[$key] = $value; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Unset the value at a given offset. |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function offsetUnset($key): void |
112
|
|
|
{ |
113
|
|
|
unset($this->container[$key]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.