1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\DependencyInjection\Definitions; |
19
|
|
|
|
20
|
|
|
use Nette; |
21
|
|
|
use Nette\DI\Definitions; |
22
|
|
|
use Nette\DI\ServiceCreationException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Definition of standard service. |
26
|
|
|
*/ |
27
|
|
|
final class InterfaceDefinition extends Definitions\Definition |
28
|
|
|
{ |
29
|
|
|
/** @var Definitions\Definition|Definitions\ServiceDefinition */ |
30
|
|
|
private $resultDefinition; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->resultDefinition = new Definitions\ServiceDefinition(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __clone() |
38
|
|
|
{ |
39
|
|
|
parent::__clone(); |
40
|
|
|
$this->resultDefinition = \unserialize(\serialize($this->resultDefinition)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @return static */ |
44
|
|
|
public function setImplement(string $type) |
45
|
|
|
{ |
46
|
|
|
if (!\interface_exists($type)) { |
47
|
|
|
throw new Nette\InvalidArgumentException("Service '{$this->getName()}': Interface '$type' not found."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->resultDefinition->setName($this->getName()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return parent::setType($type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getImplement(): ?string |
56
|
|
|
{ |
57
|
|
|
return $this->getType(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
final public function getResultType(): ?string |
61
|
|
|
{ |
62
|
|
|
return $this->resultDefinition->getType(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @return static */ |
66
|
|
|
public function setResultDefinition(Definitions\Definition $definition) |
67
|
|
|
{ |
68
|
|
|
$this->resultDefinition = $definition; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @return Definitions\ServiceDefinition */ |
74
|
|
|
public function getResultDefinition(): Definitions\Definition |
75
|
|
|
{ |
76
|
|
|
return $this->resultDefinition; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function resolveType(Nette\DI\Resolver $resolver): void |
80
|
|
|
{ |
81
|
|
|
$resultDef = $this->resultDefinition; |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$resolver->resolveDefinition($resultDef); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} catch (ServiceCreationException $e) { |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$resultDef->getType()) { |
91
|
|
|
$interface = $this->getType(); |
92
|
|
|
|
93
|
|
|
if (!$interface || !\interface_exists($interface)) { |
94
|
|
|
throw new ServiceCreationException('Type is missing in definition of service.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$resultDef->setType($interface); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$resolver->resolveDefinition($resultDef); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function complete(Nette\DI\Resolver $resolver): void |
104
|
|
|
{ |
105
|
|
|
$resultDef = $this->resultDefinition; |
106
|
|
|
|
107
|
|
|
if ($resultDef instanceof Definitions\ServiceDefinition) { |
108
|
|
|
if ($resultDef->getEntity() instanceof Definitions\Reference && !$resultDef->getFactory()->arguments) { |
|
|
|
|
109
|
|
|
$resultDef->setFactory([ // render as $container->createMethod() |
110
|
|
|
new Definitions\Reference(Nette\DI\ContainerBuilder::THIS_CONTAINER), |
111
|
|
|
Nette\DI\Container::getMethodName($resultDef->getEntity()->getValue()), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$resolver->completeDefinition($resultDef); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void |
120
|
|
|
{ |
121
|
|
|
$class = (new Nette\PhpGenerator\ClassType()) |
122
|
|
|
->addImplement($this->getType()); |
|
|
|
|
123
|
|
|
|
124
|
|
|
if (!\is_string($entity = $this->resultDefinition->getEntity())) { |
125
|
|
|
throw new ServiceCreationException( |
126
|
|
|
\sprintf('Type entity must be a string of interface, \'%s\' given', \gettype($entity)) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$code = new Definitions\Statement('class', $this->resultDefinition->getFactory()->arguments); |
131
|
|
|
$code = $generator->formatStatement($code); |
132
|
|
|
|
133
|
|
|
$method->setBody('$service = ' . "{$code} " . $class->addExtend($entity) . ';'); |
134
|
|
|
|
135
|
|
|
if (!empty($this->resultDefinition->getSetup())) { |
136
|
|
|
$setups = ''; |
137
|
|
|
|
138
|
|
|
foreach ($this->resultDefinition->getSetup() as $setup) { |
139
|
|
|
$setups .= $generator->formatStatement($setup) . ";\n"; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$method->addBody("\n" . $setups); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$method->addBody('return $service;'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|