Skip to content

Commit 54af5ad

Browse files
committed
ClassType: added possibility to define trait resolution rules
1 parent b1b4f4c commit 54af5ad

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public function __construct($name = NULL, PhpNamespace $namespace = NULL)
8989
*/
9090
public function __toString()
9191
{
92+
$traits = [];
93+
foreach ($this->traits as $trait => $resolutions) {
94+
$traits[] = 'use ' . ($this->namespace ? $this->namespace->unresolveName($trait) : $trait)
95+
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
96+
}
97+
9298
$consts = [];
9399
foreach ($this->consts as $const) {
94100
$consts[] = Helpers::formatDocComment($const->getComment())
@@ -117,7 +123,7 @@ public function __toString()
117123
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
118124
. ($this->name ? "\n" : '') . "{\n"
119125
. Strings::indent(
120-
($this->traits ? 'use ' . implode(";\nuse ", $mapper($this->traits)) . ";\n\n" : '')
126+
($this->traits ? implode("\n", $traits) . "\n\n" : '')
121127
. ($this->consts ? implode('', $consts) . "\n" : '')
122128
. ($this->properties ? implode("\n", $properties) . "\n" : '')
123129
. ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1)
@@ -293,7 +299,7 @@ public function addImplement($type)
293299
*/
294300
public function setTraits(array $traits)
295301
{
296-
$this->traits = $traits;
302+
$this->traits = array_fill_keys($traits, []);
297303
return $this;
298304
}
299305

@@ -303,17 +309,17 @@ public function setTraits(array $traits)
303309
*/
304310
public function getTraits()
305311
{
306-
return $this->traits;
312+
return array_keys($this->traits);
307313
}
308314

309315

310316
/**
311317
* @param string
312318
* @return static
313319
*/
314-
public function addTrait($trait)
320+
public function addTrait($trait, array $resolutions = [])
315321
{
316-
$this->traits[] = (string) $trait;
322+
$this->traits[$trait] = $resolutions;
317323
return $this;
318324
}
319325

tests/PhpGenerator/ClassType.expect

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
abstract final class Example extends ParentClass implements IExample, IOne
88
{
99
use ObjectTrait;
10+
use AnotherTrait {
11+
sayHello as protected;
12+
}
1013

1114
const ROLE = 'admin';
1215
const ACTIVE = FALSE;

tests/PhpGenerator/ClassType.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ $class
2020
->addImplement('IExample')
2121
->addImplement('IOne')
2222
->addTrait('ObjectTrait')
23+
->addTrait('AnotherTrait', ['sayHello as protected'])
2324
->addComment("Description of class.\nThis is example\n")
2425
->addComment('@property-read Nette\Forms\Form $form')
2526
->setConsts(['ROLE' => 'admin'])

0 commit comments

Comments
 (0)