Skip to content

Commit 71d6806

Browse files
committed
added Printer, code from __toString moved to Printer
1 parent d00eaff commit 71d6806

19 files changed

+411
-134
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\PhpGenerator;
1111

1212
use Nette;
13-
use Nette\Utils\Strings;
1413

1514

1615
/**
@@ -87,44 +86,7 @@ public function __construct(string $name = null, PhpNamespace $namespace = null)
8786

8887
public function __toString(): string
8988
{
90-
$resolver = $this->namespace ? [$this->namespace, 'unresolveName'] : function ($s) { return $s; };
91-
92-
$traits = [];
93-
foreach ($this->traits as $trait => $resolutions) {
94-
$traits[] = 'use ' . $resolver($trait)
95-
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
96-
}
97-
98-
$consts = [];
99-
foreach ($this->consts as $const) {
100-
$consts[] = Helpers::formatDocComment((string) $const->getComment())
101-
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
102-
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ';';
103-
}
104-
105-
$properties = [];
106-
foreach ($this->properties as $property) {
107-
$properties[] = Helpers::formatDocComment((string) $property->getComment())
108-
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
109-
. ($property->getValue() === null ? '' : ' = ' . Helpers::dump($property->getValue()))
110-
. ';';
111-
}
112-
113-
return Strings::normalize(
114-
Helpers::formatDocComment($this->comment . "\n")
115-
. ($this->abstract ? 'abstract ' : '')
116-
. ($this->final ? 'final ' : '')
117-
. ($this->name ? "$this->type $this->name " : '')
118-
. ($this->extends ? 'extends ' . implode(', ', array_map($resolver, (array) $this->extends)) . ' ' : '')
119-
. ($this->implements ? 'implements ' . implode(', ', array_map($resolver, $this->implements)) . ' ' : '')
120-
. ($this->name ? "\n" : '') . "{\n"
121-
. Strings::indent(
122-
($traits ? implode("\n", $traits) . "\n\n" : '')
123-
. ($consts ? implode("\n", $consts) . "\n\n" : '')
124-
. ($properties ? implode("\n\n", $properties) . "\n\n\n" : '')
125-
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''))
126-
. '}'
127-
) . ($this->name ? "\n" : '');
89+
return (new Printer)->printClass($this, $this->namespace);
12890
}
12991

13092

src/PhpGenerator/Closure.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,7 @@ public static function from(\Closure $closure): self
3737

3838
public function __toString(): string
3939
{
40-
$uses = [];
41-
foreach ($this->uses as $param) {
42-
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
43-
}
44-
$useStr = strlen($tmp = implode(', ', $uses)) > Helpers::WRAP_LENGTH && count($uses) > 1
45-
? "\n\t" . implode(",\n\t", $uses) . "\n"
46-
: $tmp;
47-
48-
return 'function '
49-
. ($this->returnReference ? '&' : '')
50-
. $this->parametersToString()
51-
. ($uses ? " use ($useStr)" : '')
52-
. $this->returnTypeToString()
53-
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
40+
return (new Printer)->printClosure($this);
5441
}
5542

5643

src/PhpGenerator/GlobalFunction.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ public static function from(string $function): self
3535

3636
public function __toString(): string
3737
{
38-
return Helpers::formatDocComment($this->comment . "\n")
39-
. 'function '
40-
. ($this->returnReference ? '&' : '')
41-
. $this->name
42-
. $this->parametersToString()
43-
. $this->returnTypeToString()
44-
. "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
38+
return (new Printer)->printFunction($this, $this->namespace);
4539
}
4640
}

src/PhpGenerator/Method.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,7 @@ public static function from($method): self
5050

5151
public function __toString(): string
5252
{
53-
return Helpers::formatDocComment($this->comment . "\n")
54-
. ($this->abstract ? 'abstract ' : '')
55-
. ($this->final ? 'final ' : '')
56-
. ($this->visibility ? $this->visibility . ' ' : '')
57-
. ($this->static ? 'static ' : '')
58-
. 'function '
59-
. ($this->returnReference ? '&' : '')
60-
. $this->name
61-
. ($params = $this->parametersToString())
62-
. $this->returnTypeToString()
63-
. ($this->abstract || $this->body === null
64-
? ';'
65-
: (strpos($params, "\n") === false ? "\n" : ' ')
66-
. "{\n"
67-
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"))
68-
. '}');
53+
return (new Printer)->printMethod($this, $this->namespace);
6954
}
7055

7156

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Nette\PhpGenerator;
1111

1212
use Nette;
13-
use Nette\Utils\Strings;
1413

1514

1615
/**
@@ -77,10 +76,6 @@ public function getNamespaces(): array
7776

7877
public function __toString(): string
7978
{
80-
return Strings::normalize(
81-
"<?php\n"
82-
. ($this->comment ? "\n" . Helpers::formatDocComment($this->comment . "\n") . "\n" : '')
83-
. implode("\n\n", $this->namespaces)
84-
) . "\n";
79+
return (new Printer)->printFile($this);
8580
}
8681
}

src/PhpGenerator/PhpNamespace.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -190,30 +190,6 @@ public function getClasses(): array
190190

191191
public function __toString(): string
192192
{
193-
$uses = [];
194-
foreach ($this->uses as $alias => $original) {
195-
$useNamespace = Helpers::extractNamespace($original);
196-
197-
if ($this->name !== $useNamespace) {
198-
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
199-
$uses[] = "use $original;";
200-
} else {
201-
$uses[] = "use $original as $alias;";
202-
}
203-
}
204-
}
205-
206-
$body = ($uses ? implode("\n", $uses) . "\n\n" : '')
207-
. implode("\n", $this->classes);
208-
209-
if ($this->bracketedSyntax) {
210-
return 'namespace' . ($this->name ? " $this->name" : '') . " {\n\n"
211-
. Strings::indent($body)
212-
. "\n}\n";
213-
214-
} else {
215-
return ($this->name ? "namespace $this->name;\n\n" : '')
216-
. $body;
217-
}
193+
return (new Printer)->printNamespace($this);
218194
}
219195
}

src/PhpGenerator/Printer.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
use Nette;
13+
use Nette\Utils\Strings;
14+
15+
16+
/**
17+
* Generates PHP code.
18+
*/
19+
class Printer
20+
{
21+
use Nette\SmartObject;
22+
23+
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
24+
{
25+
return Helpers::formatDocComment($function->getComment() . "\n")
26+
. 'function '
27+
. ($function->getReturnReference() ? '&' : '')
28+
. $function->getName()
29+
. $this->printParameters($function, $namespace)
30+
. $this->printReturnType($function, $namespace)
31+
. "\n{\n" . Strings::indent(ltrim(rtrim($function->getBody()) . "\n")) . '}';
32+
}
33+
34+
35+
public function printClosure(Closure $closure): string
36+
{
37+
$uses = [];
38+
foreach ($closure->getUses() as $param) {
39+
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
40+
}
41+
$useStr = strlen($tmp = implode(', ', $uses)) > Helpers::WRAP_LENGTH && count($uses) > 1
42+
? "\n\t" . implode(",\n\t", $uses) . "\n"
43+
: $tmp;
44+
45+
return 'function '
46+
. ($closure->getReturnReference() ? '&' : '')
47+
. $this->printParameters($closure, null)
48+
. ($uses ? " use ($useStr)" : '')
49+
. $this->printReturnType($closure, null)
50+
. " {\n" . Strings::indent(ltrim(rtrim($closure->getBody()) . "\n")) . '}';
51+
}
52+
53+
54+
public function printMethod(Method $method, PhpNamespace $namespace = null): string
55+
{
56+
return Helpers::formatDocComment($method->getComment() . "\n")
57+
. ($method->isAbstract() ? 'abstract ' : '')
58+
. ($method->isFinal() ? 'final ' : '')
59+
. ($method->getVisibility() ? $method->getVisibility() . ' ' : '')
60+
. ($method->isStatic() ? 'static ' : '')
61+
. 'function '
62+
. ($method->getReturnReference() ? '&' : '')
63+
. $method->getName()
64+
. ($params = $this->printParameters($method, $namespace))
65+
. $this->printReturnType($method, $namespace)
66+
. ($method->isAbstract() || $method->getBody() === null
67+
? ';'
68+
: (strpos($params, "\n") === false ? "\n" : ' ')
69+
. "{\n"
70+
. Strings::indent(ltrim(rtrim($method->getBody()) . "\n"))
71+
. '}');
72+
}
73+
74+
75+
public function printClass(ClassType $class, PhpNamespace $namespace = null): string
76+
{
77+
$resolver = $namespace ? [$namespace, 'unresolveName'] : function ($s) { return $s; };
78+
79+
$traits = [];
80+
foreach ($class->getTraitResolutions() as $trait => $resolutions) {
81+
$traits[] = 'use ' . $resolver($trait)
82+
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
83+
}
84+
85+
$consts = [];
86+
foreach ($class->getConstants() as $const) {
87+
$consts[] = Helpers::formatDocComment((string) $const->getComment())
88+
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
89+
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ';';
90+
}
91+
92+
$properties = [];
93+
foreach ($class->getProperties() as $property) {
94+
$properties[] = Helpers::formatDocComment((string) $property->getComment())
95+
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
96+
. ($property->getValue() === null ? '' : ' = ' . Helpers::dump($property->getValue()))
97+
. ';';
98+
}
99+
100+
return Strings::normalize(
101+
Helpers::formatDocComment($class->getComment() . "\n")
102+
. ($class->isAbstract() ? 'abstract ' : '')
103+
. ($class->isFinal() ? 'final ' : '')
104+
. ($class->getName() ? $class->getType() . ' ' . $class->getName() . ' ' : '')
105+
. ($class->getExtends() ? 'extends ' . implode(', ', array_map($resolver, (array) $class->getExtends())) . ' ' : '')
106+
. ($class->getImplements() ? 'implements ' . implode(', ', array_map($resolver, $class->getImplements())) . ' ' : '')
107+
. ($class->getName() ? "\n" : '') . "{\n"
108+
. Strings::indent(
109+
($traits ? implode("\n", $traits) . "\n\n" : '')
110+
. ($consts ? implode("\n", $consts) . "\n\n" : '')
111+
. ($properties ? implode("\n\n", $properties) . "\n\n\n" : '')
112+
. ($class->getMethods() ? implode("\n\n\n", $class->getMethods()) . "\n" : ''))
113+
. '}'
114+
) . ($class->getName() ? "\n" : '');
115+
}
116+
117+
118+
public function printNamespace(PhpNamespace $namespace): string
119+
{
120+
$name = $namespace->getName();
121+
122+
$uses = [];
123+
foreach ($namespace->getUses() as $alias => $original) {
124+
$useNamespace = Helpers::extractNamespace($original);
125+
126+
if ($name !== $useNamespace) {
127+
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
128+
$uses[] = "use $original;";
129+
} else {
130+
$uses[] = "use $original as $alias;";
131+
}
132+
}
133+
}
134+
135+
$body = ($uses ? implode("\n", $uses) . "\n\n" : '')
136+
. implode("\n", $namespace->getClasses());
137+
138+
if ($namespace->getBracketedSyntax()) {
139+
return 'namespace' . ($name ? " $name" : '') . " {\n\n"
140+
. Strings::indent($body)
141+
. "\n}\n";
142+
143+
} else {
144+
return ($name ? "namespace $name;\n\n" : '')
145+
. $body;
146+
}
147+
}
148+
149+
150+
public function printFile(PhpFile $file): string
151+
{
152+
return Strings::normalize(
153+
"<?php\n"
154+
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") . "\n" : '')
155+
. implode("\n\n", $file->getNamespaces())
156+
) . "\n";
157+
}
158+
159+
160+
/**
161+
* @param Nette\PhpGenerator\Traits\FunctionLike $function
162+
*/
163+
protected function printParameters($function, ?PhpNamespace $namespace): string
164+
{
165+
$params = [];
166+
$list = $function->getParameters();
167+
foreach ($list as $param) {
168+
$variadic = $function->isVariadic() && $param === end($list);
169+
$hint = $param->getTypeHint();
170+
$params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '')
171+
. ($param->isReference() ? '&' : '')
172+
. ($variadic ? '...' : '')
173+
. '$' . $param->getName()
174+
. ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->getDefaultValue()) : '');
175+
}
176+
177+
return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH && count($params) > 1
178+
? "(\n\t" . implode(",\n\t", $params) . "\n)"
179+
: "($tmp)";
180+
}
181+
182+
183+
/**
184+
* @param Nette\PhpGenerator\Traits\FunctionLike $function
185+
*/
186+
protected function printReturnType($function, ?PhpNamespace $namespace): string
187+
{
188+
return $function->getReturnType()
189+
? ': ' . ($function->getReturnNullable() ? '?' : '') . ($namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType())
190+
: '';
191+
}
192+
}

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -190,31 +190,4 @@ public function setNamespace(PhpNamespace $val = null): self
190190
$this->namespace = $val;
191191
return $this;
192192
}
193-
194-
195-
protected function parametersToString(): string
196-
{
197-
$params = [];
198-
foreach ($this->parameters as $param) {
199-
$variadic = $this->variadic && $param === end($this->parameters);
200-
$hint = $param->getTypeHint();
201-
$params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '')
202-
. ($param->isReference() ? '&' : '')
203-
. ($variadic ? '...' : '')
204-
. '$' . $param->getName()
205-
. ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->getDefaultValue()) : '');
206-
}
207-
208-
return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH && count($params) > 1
209-
? "(\n\t" . implode(",\n\t", $params) . "\n)"
210-
: "($tmp)";
211-
}
212-
213-
214-
protected function returnTypeToString(): string
215-
{
216-
return $this->returnType
217-
? ': ' . ($this->returnNullable ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType)
218-
: '';
219-
}
220193
}

0 commit comments

Comments
 (0)