|
| 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 | +} |
0 commit comments