Skip to content

Commit

Permalink
added PhpFile::add()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 6, 2018
1 parent 5ea8d0e commit fd6ea6a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ class A
}
```

You can also add existing `ClassType` or `PhpNamespace` objects to the file:

```php
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
$class = new Nette\PhpGenerator\ClassType('Demo');

$file = new Nette\PhpGenerator\PhpFile;
$file->add($namespace);
$file->add($class);
```

Generate using Reflection
-------------------------

Expand Down
30 changes: 26 additions & 4 deletions src/PhpGenerator/PhpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ final class PhpFile
private $namespaces = [];


/**
* @param ClassType|PhpNamespace $item
* @return static
*/
public function add($item): self
{
if ($item instanceof PhpNamespace) {
$this->namespaces[$item->getName()] = $item;
foreach ($this->namespaces as $namespace) {
$namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces['']));
}

} elseif ($item instanceof ClassType) {
$namespace = $item->getNamespace();
$this->addNamespace($namespace ? $namespace->getName() : '')
->add($item);

} else {
throw new Nette\InvalidArgumentException('Argument must be ClassType|PhpNamespace.');
}

return $this;
}


public function addClass(string $name): ClassType
{
return $this
Expand Down Expand Up @@ -56,10 +81,7 @@ public function addTrait(string $name): ClassType
public function addNamespace(string $name): PhpNamespace
{
if (!isset($this->namespaces[$name])) {
$this->namespaces[$name] = new PhpNamespace($name);
foreach ($this->namespaces as $namespace) {
$namespace->setBracketedSyntax(count($this->namespaces) > 1 && isset($this->namespaces['']));
}
$this->add(new PhpNamespace($name));
}
return $this->namespaces[$name];
}
Expand Down
31 changes: 31 additions & 0 deletions tests/PhpGenerator/PhpFile.add.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PhpNamespace;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


Assert::exception(function () {
(new PhpFile)
->add(new stdClass);
}, Nette\InvalidArgumentException::class, 'Argument must be ClassType|PhpNamespace.');


$file = (new PhpFile)
->add($classA = new ClassType('A'))
->add($classB = new ClassType('B', new PhpNamespace('X')))
->add(new PhpNamespace('Y'));

$namespaces = $file->getNamespaces();
Assert::same($namespaces[''], $classA->getNamespace());
Assert::same($namespaces['X'], $classB->getNamespace());
Assert::true(isset($namespaces['Y']));

Assert::true($namespaces['']->getBracketedSyntax());
Assert::true($namespaces['X']->getBracketedSyntax());
Assert::true($namespaces['Y']->getBracketedSyntax());

0 comments on commit fd6ea6a

Please sign in to comment.