1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\DI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait to use for DI aware services to let them know of the current $di |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
trait TInjectable |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Properties |
14
|
|
|
*/ |
15
|
|
|
protected $di; // the service container |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the service container to use |
21
|
|
|
* |
22
|
|
|
* @param class $di a service container |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function setDI($di) |
27
|
|
|
{ |
28
|
|
|
$this->di = $di; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Magic method to get and create services. |
35
|
|
|
* When created it is also stored as a parameter of this object. |
36
|
|
|
* |
37
|
|
|
* @param string $service name of class property not existing. |
38
|
|
|
* |
39
|
|
|
* @return class as the service requested. |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function __get($service) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (!$this->di) { |
44
|
|
|
throw new \Exception( |
45
|
|
|
'In trait TInjectable used by class ' . __CLASS__ . '. You are trying to get |
46
|
|
|
message, code a property from $this->di, but $this->di is not set. Did you |
47
|
|
|
forget to call setDI()?' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$this->$service = $this->di->get($service); |
53
|
|
|
return $this->$service; |
54
|
|
|
} catch (\Exception $e) { |
55
|
|
|
throw new \Exception( |
56
|
|
|
'In trait TInjectable used by class ' . __CLASS__ . '. You are trying to get |
57
|
|
|
a property (service) "' . $service . '" from $this->di, but the service is not |
58
|
|
|
set in $this->di. Did you misspell the service you are trying to reach or did |
59
|
|
|
you forget to load it into the $di container?' |
60
|
|
|
. $e->getMessage() |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Magic method to get and create services as a method call. |
69
|
|
|
* When created it is also stored as a parameter of this object. |
70
|
|
|
* |
71
|
|
|
* @param string $service name of class property not existing. |
72
|
|
|
* @param array $arguments Additional arguments to sen to the method (NOT IMPLEMENTED). |
73
|
|
|
* |
74
|
|
|
* @return class as the service requested. |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function __call($service, $arguments = []) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (!$this->di) { |
79
|
|
|
throw new \Exception( |
80
|
|
|
'In trait TInjectable used by class ' . __CLASS__ . '. You are trying to call a |
81
|
|
|
method in $this->di, but $this->di is not set. Did you forget to call setDI()?' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$this->$service = $this->di->get($service); |
87
|
|
|
return $this->$service; |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
throw new \Exception( |
90
|
|
|
'In trait TInjectable used by class ' . __CLASS__ . '. You are trying to get a |
91
|
|
|
method (service) "' . $service . '" from $this->di, but the service is not set |
92
|
|
|
in $this->di. Did you misspell the service you are trying to reach or did you |
93
|
|
|
forget to load it into the $di container? ' |
94
|
|
|
. $e->getMessage() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.