Skip to content

Commit 0503161

Browse files
committed
PhpFile, PhpNamespace: can contain functions [Closes #80]
1 parent f453e3f commit 0503161

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

readme.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -641,19 +641,20 @@ echo $printer->printNamespace($namespace);
641641
PHP Files
642642
---------
643643

644-
Classes and namespaces can be grouped into PHP files represented by the class [PhpFile](https://api.nette.org/php-generator/Nette/PhpGenerator/PhpFile.html):
644+
Classes, functions and namespaces can be grouped into PHP files represented by the class [PhpFile](https://api.nette.org/php-generator/Nette/PhpGenerator/PhpFile.html):
645645

646646
```php
647647
$file = new Nette\PhpGenerator\PhpFile;
648648
$file->addComment('This file is auto-generated.');
649649
$file->setStrictTypes(); // adds declare(strict_types=1)
650650

651-
$namespace = $file->addNamespace('Foo');
652-
$class = $namespace->addClass('A');
653-
$class->addMethod('hello');
651+
$class = $file->addClass('Foo\A');
652+
$function = $file->addFunction('Foo\foo');
654653

655-
// or insert an existing namespace into the file
656-
// $file->addNamespace(new Nette\PhpGenerator\PhpNamespace('Foo'));
654+
// or
655+
// $namespace = $file->addNamespace('Foo');
656+
// $class = $namespace->addClass('A');
657+
// $function = $namespace->addFunction('foo');
657658

658659
echo $file;
659660

@@ -676,9 +677,10 @@ namespace Foo;
676677

677678
class A
678679
{
679-
public function hello()
680-
{
681-
}
680+
}
681+
682+
function foo()
683+
{
682684
}
683685
```
684686

src/PhpGenerator/PhpFile.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public function addNamespace($namespace): PhpNamespace
8484
}
8585

8686

87+
public function addFunction(string $name): GlobalFunction
88+
{
89+
return $this
90+
->addNamespace(Helpers::extractNamespace($name))
91+
->addFunction(Helpers::extractShortName($name));
92+
}
93+
94+
8795
/** @return PhpNamespace[] */
8896
public function getNamespaces(): array
8997
{
@@ -105,6 +113,20 @@ public function getClasses(): array
105113
}
106114

107115

116+
/** @return GlobalFunction[] */
117+
public function getFunctions(): array
118+
{
119+
$functions = [];
120+
foreach ($this->namespaces as $n => $namespace) {
121+
$n .= $n ? '\\' : '';
122+
foreach ($namespace->getFunctions() as $c => $class) {
123+
$functions[$n . $c] = $class;
124+
}
125+
}
126+
return $functions;
127+
}
128+
129+
108130
/** @return static */
109131
public function addUse(string $name, string $alias = null): self
110132
{

src/PhpGenerator/PhpNamespace.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ final class PhpNamespace
4444
/** @var ClassType[] */
4545
private $classes = [];
4646

47+
/** @var GlobalFunction[] */
48+
private $functions = [];
49+
4750

4851
public function __construct(string $name)
4952
{
@@ -195,13 +198,26 @@ public function addEnum(string $name): ClassType
195198
}
196199

197200

201+
public function addFunction(string $name): GlobalFunction
202+
{
203+
return $this->functions[$name] = new GlobalFunction($name);
204+
}
205+
206+
198207
/** @return ClassType[] */
199208
public function getClasses(): array
200209
{
201210
return $this->classes;
202211
}
203212

204213

214+
/** @return GlobalFunction[] */
215+
public function getFunctions(): array
216+
{
217+
return $this->functions;
218+
}
219+
220+
205221
public function __toString(): string
206222
{
207223
try {

src/PhpGenerator/Printer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,16 @@ public function printNamespace(PhpNamespace $namespace): string
203203
$name = $namespace->getName();
204204
$uses = $this->printUses($namespace);
205205

206-
$classes = [];
206+
$items = [];
207207
foreach ($namespace->getClasses() as $class) {
208-
$classes[] = $this->printClass($class, $namespace);
208+
$items[] = $this->printClass($class, $namespace);
209+
}
210+
foreach ($namespace->getFunctions() as $function) {
211+
$items[] = $this->printFunction($function, $namespace);
209212
}
210213

211214
$body = ($uses ? $uses . "\n\n" : '')
212-
. implode("\n", $classes);
215+
. implode("\n", $items);
213216

214217
if ($namespace->hasBracketedSyntax()) {
215218
return 'namespace' . ($name ? " $name" : '') . "\n{\n"

tests/PhpGenerator/PhpFile.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ $interfaceF
7070
$traitG = $file->addTrait('Baz\G');
7171
Assert::same($file->addNamespace('Baz'), $traitG->getNamespace());
7272

73+
$file->addFunction('Baz\\f2')
74+
->setReturnType('Foo\B');
75+
7376

7477
sameFile(__DIR__ . '/expected/PhpFile.regular.expect', (string) $file);
7578

7679
$file->addClass('H');
7780

7881
$file->addClass('FooBar\I');
7982

83+
$file->addFunction('f1')
84+
->setBody('return 1;');
85+
8086
sameFile(__DIR__ . '/expected/PhpFile.bracketed.expect', (string) $file);
8187

8288
Assert::same([
@@ -102,6 +108,9 @@ Assert::same([
102108
'FooBar\I',
103109
], array_keys($file->getClasses()));
104110

111+
Assert::same(['Baz\\f2', 'f1'], array_keys($file->getFunctions()));
112+
113+
105114

106115

107116
$file = new PhpFile;

tests/PhpGenerator/expected/PhpFile.bracketed.expect

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ namespace Baz
5757
trait G
5858
{
5959
}
60+
61+
function f2(): \Foo\B
62+
{
63+
}
6064
}
6165

6266

@@ -65,6 +69,11 @@ namespace
6569
class H
6670
{
6771
}
72+
73+
function f1()
74+
{
75+
return 1;
76+
}
6877
}
6978

7079

tests/PhpGenerator/expected/PhpFile.regular.expect

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ interface F extends \Foo\B, \Bar\C
5555
trait G
5656
{
5757
}
58+
59+
function f2(): \Foo\B
60+
{
61+
}

0 commit comments

Comments
 (0)