|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\DI; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\DI\Exception\Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait to use for DI aware services to let them know of the current $di. |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
trait InjectionMagicTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Magic method to get and create services. |
|
15
|
|
|
* When created it is also stored as a parameter of this object. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $service name of class property not existing. |
|
18
|
|
|
* |
|
19
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
|
20
|
|
|
* |
|
21
|
|
|
* @return object as the service requested. |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __get($service) |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->find($service); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Magic method to get and create services as a method call. |
|
32
|
|
|
* When created it is also stored as a parameter of this object. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $service name of class property not existing. |
|
35
|
|
|
* @param array $arguments Additional arguments to sen to the method |
|
36
|
|
|
* (NOT IMPLEMENTED). |
|
37
|
|
|
* |
|
38
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
|
39
|
|
|
* |
|
40
|
|
|
* @return class as the service requested. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __call($service, $arguments = []) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->find($service); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Find, load service and set as part of the class, then return it. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $service name of class property not existing. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws NotFoundException No entry was found for this identifier. |
|
55
|
|
|
* |
|
56
|
|
|
* @return object as the service requested. |
|
57
|
|
|
*/ |
|
58
|
|
|
private function find($service) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->di) { |
|
|
|
|
|
|
61
|
|
|
throw new Exception("InjectionAwareTrait \$di is not set. Call setDI()?"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->$service = $this->di->get($service); |
|
|
|
|
|
|
65
|
|
|
return $this->$service; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
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@propertyannotation 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.