mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Add namespace builder
This commit is contained in:
parent
4387454fe0
commit
01643e06d3
59
lib/PhpParser/Builder/Namespace_.php
Normal file
59
lib/PhpParser/Builder/Namespace_.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpParser\Builder;
|
||||||
|
|
||||||
|
use PhpParser;
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
|
class Namespace_ extends PhpParser\BuilderAbstract
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $stmts = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a namespace builder.
|
||||||
|
*
|
||||||
|
* @param Node\Name|string $name Name of the namespace
|
||||||
|
*/
|
||||||
|
public function __construct($name) {
|
||||||
|
$this->name = $this->normalizeName($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a statement.
|
||||||
|
*
|
||||||
|
* @param Node|PhpParser\Builder $stmt The statement to add
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addStmt($stmt) {
|
||||||
|
$this->stmts[] = $this->normalizeNode($stmt);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds multiple statements.
|
||||||
|
*
|
||||||
|
* @param array $stmts The statements to add
|
||||||
|
*
|
||||||
|
* @return $this The builder instance (for fluid interface)
|
||||||
|
*/
|
||||||
|
public function addStmts(array $stmts) {
|
||||||
|
foreach ($stmts as $stmt) {
|
||||||
|
$this->addStmt($stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the built node.
|
||||||
|
*
|
||||||
|
* @return Node The built node
|
||||||
|
*/
|
||||||
|
public function getNode() {
|
||||||
|
return new Stmt\Namespace_($this->name, $this->stmts);
|
||||||
|
}
|
||||||
|
}
|
@ -5,16 +5,28 @@ namespace PhpParser;
|
|||||||
use PhpParser\Builder;
|
use PhpParser\Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* "class", "interface" and "function" are reserved keywords, so the methods are defined as _class(),
|
* The following methods use reserved keywords, so their implementation is defined with an underscore and made available
|
||||||
* _interface() and _function() in the class and are made available as class(), interface() and function()
|
* with the reserved name through __call() magic.
|
||||||
* through __call() magic.
|
|
||||||
*
|
*
|
||||||
|
* @method Builder\Namespace_ namespace(string $name) Creates a namespace builder.
|
||||||
* @method Builder\Class_ class(string $name) Creates a class builder.
|
* @method Builder\Class_ class(string $name) Creates a class builder.
|
||||||
* @method Builder\Function_ function(string $name) Creates a function builder
|
|
||||||
* @method Builder\Interface_ interface(string $name) Creates an interface builder.
|
* @method Builder\Interface_ interface(string $name) Creates an interface builder.
|
||||||
|
* @method Builder\Trait_ trait(string $name) Creates a trait builder.
|
||||||
|
* @method Builder\Function_ function(string $name) Creates a function builder
|
||||||
*/
|
*/
|
||||||
class BuilderFactory
|
class BuilderFactory
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Creates a namespace builder.
|
||||||
|
*
|
||||||
|
* @param string $name Fully qualified namespace
|
||||||
|
*
|
||||||
|
* @return Builder\Namespace_ The created namespace builder
|
||||||
|
*/
|
||||||
|
protected function _namespace($name) {
|
||||||
|
return new Builder\Namespace_($name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a class builder.
|
* Creates a class builder.
|
||||||
*
|
*
|
||||||
|
37
test/PhpParser/Builder/NamespaceTest.php
Normal file
37
test/PhpParser/Builder/NamespaceTest.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpParser\Builder;
|
||||||
|
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Stmt;
|
||||||
|
|
||||||
|
class NamespaceTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected function createNamespaceBuilder($fqn) {
|
||||||
|
return new Namespace_($fqn);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreation() {
|
||||||
|
$stmt1 = new Stmt\Class_('SomeClass');
|
||||||
|
$stmt2 = new Stmt\Interface_('SomeInterface');
|
||||||
|
$stmt3 = new Stmt\Function_('someFunction');
|
||||||
|
$expected = new Stmt\Namespace_(
|
||||||
|
new Node\Name('Some\Namespace'),
|
||||||
|
array($stmt1, $stmt2, $stmt3)
|
||||||
|
);
|
||||||
|
|
||||||
|
$node = $this->createNamespaceBuilder('Some\Namespace')
|
||||||
|
->addStmt($stmt1)
|
||||||
|
->addStmts(array($stmt2, $stmt3))
|
||||||
|
->getNode()
|
||||||
|
;
|
||||||
|
$this->assertEquals($expected, $node);
|
||||||
|
|
||||||
|
$node = $this->createNamespaceBuilder(new Node\Name(array('Some', 'Namespace')))
|
||||||
|
->addStmts(array($stmt1, $stmt2))
|
||||||
|
->addStmt($stmt3)
|
||||||
|
->getNode()
|
||||||
|
;
|
||||||
|
$this->assertEquals($expected, $node);
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,7 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function provideTestFactory() {
|
public function provideTestFactory() {
|
||||||
return array(
|
return array(
|
||||||
|
array('namespace', 'PhpParser\Builder\Namespace_'),
|
||||||
array('class', 'PhpParser\Builder\Class_'),
|
array('class', 'PhpParser\Builder\Class_'),
|
||||||
array('interface', 'PhpParser\Builder\Interface_'),
|
array('interface', 'PhpParser\Builder\Interface_'),
|
||||||
array('trait', 'PhpParser\Builder\Trait_'),
|
array('trait', 'PhpParser\Builder\Trait_'),
|
||||||
|
Loading…
Reference in New Issue
Block a user