Skip to content

Commit 1287ec7

Browse files
jakubkulhandg
authored andcommitted
added support for namespaces
1 parent 1f408e9 commit 1287ec7

File tree

7 files changed

+687
-12
lines changed

7 files changed

+687
-12
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*
1717
* @author David Grudl
1818
*
19+
* @method PhpNamespace getNamespace()
1920
* @method ClassType setName(string)
2021
* @method string getName()
2122
* @method ClassType setType(string)
@@ -45,6 +46,15 @@
4546
*/
4647
class ClassType extends Nette\Object
4748
{
49+
const TYPE_CLASS = 'class';
50+
51+
const TYPE_INTERFACE = 'interface';
52+
53+
const TYPE_TRAIT = 'trait';
54+
55+
/** @var PhpNamespace */
56+
private $namespace;
57+
4858
/** @var string */
4959
private $name;
5060

@@ -79,7 +89,10 @@ class ClassType extends Nette\Object
7989
private $methods = array();
8090

8191

82-
/** @return ClassType */
92+
/**
93+
* @param \ReflectionClass|string
94+
* @return ClassType
95+
*/
8396
public static function from($from)
8497
{
8598
$from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
@@ -116,29 +129,41 @@ public static function from($from)
116129
}
117130

118131

119-
public function __construct($name = NULL)
132+
public function __construct($name = NULL, PhpNamespace $namespace = NULL)
120133
{
121134
$this->name = $name;
135+
$this->namespace = $namespace;
122136
}
123137

124138

125-
/** @return ClassType */
139+
/**
140+
* @param string
141+
* @param mixed
142+
* @return ClassType
143+
*/
126144
public function addConst($name, $value)
127145
{
128146
$this->consts[$name] = $value;
129147
return $this;
130148
}
131149

132150

133-
/** @return Property */
151+
/**
152+
* @param string
153+
* @param mixed
154+
* @return Property
155+
*/
134156
public function addProperty($name, $value = NULL)
135157
{
136158
$property = new Property;
137159
return $this->properties[$name] = $property->setName($name)->setValue($value);
138160
}
139161

140162

141-
/** @return Method */
163+
/**
164+
* @param string
165+
* @return Method
166+
*/
142167
public function addMethod($name)
143168
{
144169
$method = new Method;
@@ -151,35 +176,63 @@ public function addMethod($name)
151176
}
152177

153178

154-
/** @return string PHP code */
179+
/**
180+
* @return string PHP code
181+
*/
155182
public function __toString()
156183
{
157184
$consts = array();
158185
foreach ($this->consts as $name => $value) {
159186
$consts[] = "const $name = " . Helpers::dump($value) . ";\n";
160187
}
188+
161189
$properties = array();
162190
foreach ($this->properties as $property) {
163-
$properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
164-
. $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name
191+
$properties[] = ($property->getDocuments() ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->getDocuments())) . "\n */\n" : '')
192+
. $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
165193
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
166194
. ";\n";
167195
}
196+
197+
$extends = $implements = $traits = array();
198+
199+
if ($this->namespace) {
200+
$fqnToAlias = array_flip($this->namespace->getUses());
201+
202+
foreach ((array) $this->extends as $fqn) {
203+
$extends[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
204+
}
205+
206+
foreach ((array) $this->implements as $fqn) {
207+
$implements[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
208+
}
209+
210+
foreach ((array) $this->traits as $fqn) {
211+
$traits[] = isset($fqnToAlias[$fqn]) ? $fqnToAlias[$fqn] : '\\' . $fqn;
212+
}
213+
214+
} else {
215+
$extends = (array) $this->extends;
216+
$implements = (array) $this->implements;
217+
$traits = (array) $this->traits;
218+
}
219+
168220
return Strings::normalize(
169221
($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
170222
. ($this->abstract ? 'abstract ' : '')
171223
. ($this->final ? 'final ' : '')
172224
. $this->type . ' '
173225
. $this->name . ' '
174-
. ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
175-
. ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
226+
. ($this->extends ? 'extends ' . implode(', ', $extends) . ' ' : '')
227+
. ($this->implements ? 'implements ' . implode(', ', $implements) . ' ' : '')
176228
. "\n{\n\n"
177229
. Strings::indent(
178-
($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
230+
($this->traits ? 'use ' . implode(', ', $traits) . ";\n\n" : '')
179231
. ($this->consts ? implode('', $consts) . "\n\n" : '')
180232
. ($this->properties ? implode("\n", $properties) . "\n\n" : '')
181233
. implode("\n\n\n", $this->methods), 1)
182-
. "\n\n}") . "\n";
234+
. "\n\n}"
235+
) . "\n";
183236
}
184237

185238
}

src/PhpGenerator/Helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,24 @@ public static function createObject($class, array $props)
197197
return unserialize('O' . substr(serialize((string) $class), 1, -1) . substr(serialize($props), 1));
198198
}
199199

200+
201+
/**
202+
* @param string
203+
* @return string
204+
*/
205+
public static function extractNamespace($fqn)
206+
{
207+
return ($pos = strrpos($fqn, '\\')) ? substr($fqn, 0, $pos) : NULL;
208+
}
209+
210+
211+
/**
212+
* @param string
213+
* @return string
214+
*/
215+
public static function extractShortName($fqn)
216+
{
217+
return ($pos = strrpos($fqn, '\\')) ? substr($fqn, $pos + 1) : $fqn;
218+
}
219+
200220
}

src/PhpGenerator/PhpFile.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (http://nette.org)
5+
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6+
*/
7+
8+
namespace Nette\PhpGenerator;
9+
10+
use Nette\Object;
11+
use Nette\Utils\Strings;
12+
13+
14+
/**
15+
* Instance of PHP file.
16+
*
17+
* Generates:
18+
* - opening tag (<?php)
19+
* - doc comments
20+
* - one or more namespaces
21+
*
22+
* @author Jakub Kulhan <[email protected]>
23+
*/
24+
class PhpFile extends Object
25+
{
26+
/** @var string[] */
27+
private $documents;
28+
29+
/** @var PhpNamespace[] */
30+
private $namespaces = array();
31+
32+
33+
/**
34+
* @return string[]
35+
*/
36+
public function getDocuments()
37+
{
38+
return $this->documents;
39+
}
40+
41+
42+
/**
43+
* @param string[]
44+
* @return self
45+
*/
46+
public function setDocuments(array $documents)
47+
{
48+
$this->documents = $documents;
49+
return $this;
50+
}
51+
52+
53+
/**
54+
* @param string
55+
* @return self
56+
*/
57+
public function addDocument($document)
58+
{
59+
$this->documents[] = $document;
60+
return $this;
61+
}
62+
63+
64+
/**
65+
* @param string
66+
* @return ClassType
67+
*/
68+
public function addClass($fqn)
69+
{
70+
return $this
71+
->addNamespace(Helpers::extractNamespace($fqn))
72+
->addClass(Helpers::extractShortName($fqn));
73+
}
74+
75+
76+
/**
77+
* @param string
78+
* @return ClassType
79+
*/
80+
public function addInterface($fqn)
81+
{
82+
return $this
83+
->addNamespace(Helpers::extractNamespace($fqn))
84+
->addInterface(Helpers::extractShortName($fqn));
85+
}
86+
87+
88+
/**
89+
* @param string
90+
* @return ClassType
91+
*/
92+
public function addTrait($fqn)
93+
{
94+
return $this
95+
->addNamespace(Helpers::extractNamespace($fqn))
96+
->addTrait(Helpers::extractShortName($fqn));
97+
}
98+
99+
100+
/**
101+
* @param string NULL means global namespace
102+
* @return PhpNamespace
103+
*/
104+
public function addNamespace($name)
105+
{
106+
if (!isset($this->namespaces[$name])) {
107+
$this->namespaces[$name] = new PhpNamespace($name);
108+
}
109+
return $this->namespaces[$name];
110+
}
111+
112+
113+
/**
114+
* @return string PHP code
115+
*/
116+
public function __toString()
117+
{
118+
foreach ($this->namespaces as $namespace) {
119+
$namespace->setBracketedSyntax(isset($this->namespaces[NULL]));
120+
}
121+
122+
return Strings::normalize(
123+
"<?php\n"
124+
. ($this->documents ? "\n" . str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n\n" : '')
125+
. implode("\n\n", $this->namespaces)
126+
) . "\n";
127+
}
128+
129+
}

0 commit comments

Comments
 (0)