/
PhpGenerator.php
196 lines (158 loc) 路 5.03 KB
/
PhpGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI;
use Nette;
use Nette\DI\Definitions\Reference;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\Helpers as PhpHelpers;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Utils\Strings;
/**
* Container PHP code generator.
*/
class PhpGenerator
{
use Nette\SmartObject;
/** @var ContainerBuilder */
private $builder;
/** @var string */
private $className;
public function __construct(ContainerBuilder $builder)
{
$this->builder = $builder;
}
/**
* Generates PHP classes. First class is the container.
*/
public function generate(string $className): Nette\PhpGenerator\ClassType
{
$this->builder->complete();
$this->className = $className;
$class = new Nette\PhpGenerator\ClassType($this->className);
$class->setExtends(Container::class);
$class->addMethod('__construct')
->addBody('$this->parameters = $params;')
->addBody('$this->parameters += ?;', [$this->builder->parameters])
->addParameter('params', [])
->setTypeHint('array');
foreach ($this->builder->exportMeta() as $key => $value) {
$class->addProperty($key)
->setVisibility('protected')
->setValue($value);
}
$definitions = $this->builder->getDefinitions();
ksort($definitions);
foreach ($definitions as $def) {
$class->addMember($this->generateMethod($def));
}
$class->getMethod(Container::getMethodName(ContainerBuilder::THIS_CONTAINER))
->setReturnType($className)
->setBody('return $this;');
return $class;
}
public function generateMethod(Definitions\Definition $def): Nette\PhpGenerator\Method
{
try {
$name = (string) $def->getName();
$method = new Nette\PhpGenerator\Method(Container::getMethodName($name));
$method->setVisibility('public');
$method->setReturnType($def->getType());
$def->generateMethod($method, $this);
return $method;
} catch (\Exception $e) {
throw new ServiceCreationException("Service '$name': " . $e->getMessage(), 0, $e);
}
}
/**
* Formats PHP code for class instantiating, function calling or property setting in PHP.
*/
public function formatStatement(Statement $statement): string
{
$entity = $statement->getEntity();
$arguments = $statement->arguments;
switch (true) {
case is_string($entity) && Strings::contains($entity, '?'): // PHP literal
return $this->formatPhp($entity, $arguments);
case is_string($entity): // create class
return $this->formatPhp("new $entity" . ($arguments ? '(...?)' : ''), $arguments ? [$arguments] : []);
case is_array($entity):
switch (true) {
case $entity[1][0] === '$': // property getter, setter or appender
$name = substr($entity[1], 1);
if ($append = (substr($name, -2) === '[]')) {
$name = substr($name, 0, -2);
}
if ($entity[0] instanceof Reference) {
$prop = $this->formatPhp('?->?', [$entity[0], $name]);
} else {
$prop = $this->formatPhp($entity[0] . '::$?', [$name]);
}
return $arguments
? $this->formatPhp($prop . ($append ? '[]' : '') . ' = ?', [$arguments[0]])
: $prop;
case $entity[0] instanceof Statement:
$inner = $this->formatPhp('?', [$entity[0]]);
if (substr($inner, 0, 4) === 'new ') {
$inner = "($inner)";
}
return $this->formatPhp("$inner->?(...?)", [$entity[1], $arguments]);
case $entity[0] instanceof Reference:
return $this->formatPhp('?->?(...?)', [$entity[0], $entity[1], $arguments]);
case $entity[0] === '': // function call
return $this->formatPhp("$entity[1](...?)", [$arguments]);
case is_string($entity[0]): // static method call
return $this->formatPhp("$entity[0]::$entity[1](...?)", [$arguments]);
}
}
throw Nette\InvalidStateException;
}
/**
* Formats PHP statement.
* @internal
*/
public function formatPhp(string $statement, array $args): string
{
array_walk_recursive($args, function (&$val): void {
if ($val instanceof Statement) {
$val = new PhpLiteral($this->formatStatement($val));
} elseif ($val instanceof Reference) {
$name = $val->getValue();
if ($val->isSelf()) {
$val = new PhpLiteral('$service');
} elseif ($name === ContainerBuilder::THIS_CONTAINER) {
$val = new PhpLiteral('$this');
} else {
$val = ContainerBuilder::literal('$this->getService(?)', [$name]);
}
}
});
return PhpHelpers::formatArgs($statement, $args);
}
/**
* Converts parameters from Definition to PhpGenerator.
* @return Nette\PhpGenerator\Parameter[]
*/
public function convertParameters(array $parameters): array
{
$res = [];
foreach ($parameters as $k => $v) {
$tmp = explode(' ', is_int($k) ? $v : $k);
$param = $res[] = new Nette\PhpGenerator\Parameter(end($tmp));
if (!is_int($k)) {
$param->setDefaultValue($v);
}
if (isset($tmp[1])) {
$param->setTypeHint($tmp[0]);
}
}
return $res;
}
public function getClassName(): ?string
{
return $this->className;
}
}