Browse files

Implemented NamespaceStatement, UseStatement and no longer use closin…

…g PHP tag on documents with single PhpBlock node
  • Loading branch information...
1 parent c1d6493 commit 330417b8dedc49eca18c1344a833449785ef825e @jmalloc jmalloc committed Aug 9, 2012
Showing with 83 additions and 11 deletions.
  1. +2 −2 composer.lock
  2. +34 −2 lib/Icecave/Rasta/Generator.php
  3. +47 −7 test/suite/Icecave/Rasta/GeneratorTest.php
View
4 composer.lock
@@ -8,8 +8,8 @@
{
"package": "icecave/pasta-ast",
"version": "dev-master",
- "source-reference": "d6d029b05aa2a6d862b0673959392b7e39c62db7",
- "commit-date": "1344343676"
+ "source-reference": "050c3ccab2b0ca5d5c7aeec578b828f526e196f6",
+ "commit-date": "1344435576"
},
{
"package": "icecave/visita",
View
36 lib/Icecave/Rasta/Generator.php
@@ -15,6 +15,7 @@
use Icecave\Pasta\AST\IfStatement;
use Icecave\Pasta\AST\InterfaceDefinition;
use Icecave\Pasta\AST\Literal;
+use Icecave\Pasta\AST\NamespaceStatement;
use Icecave\Pasta\AST\Node;
use Icecave\Pasta\AST\Operator\BinaryOperator;
use Icecave\Pasta\AST\Operator\Equals;
@@ -32,6 +33,7 @@
use Icecave\Pasta\AST\ReturnStatement;
use Icecave\Pasta\AST\Root;
use Icecave\Pasta\AST\StatementBlock;
+use Icecave\Pasta\AST\UseStatement;
use Icecave\Pasta\AST\Variable;
class Generator implements IVisitor
@@ -45,16 +47,31 @@ public function __construct($indentString = ' ')
public function visitRoot(Root $node)
{
+ $children = $node->children();
+ if (1 === count($children))
+ {
+ $child = current($children);
+ if ($child instanceof PhpBlock)
+ {
+ return $this->visitPhpBlock($child, false);
+ }
+ }
+
return $this->visitChildren($node);
}
- public function visitPhpBlock(PhpBlock $node)
+ public function visitPhpBlock(PhpBlock $node, $closeTag = true)
{
$code = '<?';
if (!$node->isShortTag()) {
$code .= 'php';
}
- return $code . PHP_EOL . $this->visitChildren($node);
+ $code .= PHP_EOL;
+ $code .= $this->visitChildren($node);
+ if ($closeTag) {
+ $code .= '?>';
+ }
+ return $code;
}
public function visitContentBlock(ContentBlock $node)
@@ -241,6 +258,21 @@ public function visitVariable(Variable $node)
return '$' . $node->name();
}
+ public function visitNamespaceStatement(NamespaceStatement $node)
+ {
+ return $this->indent('namespace ' . $node->name()->accept($this) . ';' . PHP_EOL);
+ }
+
+ public function visitUseStatement(UseStatement $node)
+ {
+ $code = $this->indent('use ' . $node->name()->accept($this));
+ if ($alias = $node->alias()) {
+ $code .= ' as ' . $alias;
+ }
+ $code .= ';' . PHP_EOL;
+ return $code;
+ }
+
// Operators ...
public function visitEquals(Equals $node)
View
54 test/suite/Icecave/Rasta/GeneratorTest.php
@@ -13,6 +13,7 @@
use Icecave\Pasta\AST\IfStatement;
use Icecave\Pasta\AST\InterfaceDefinition;
use Icecave\Pasta\AST\Literal;
+use Icecave\Pasta\AST\NamespaceStatement;
use Icecave\Pasta\AST\Operator\Equals;
use Icecave\Pasta\AST\Operator\InstanceOfType;
use Icecave\Pasta\AST\Operator\LogicalAnd;
@@ -26,6 +27,7 @@
use Icecave\Pasta\AST\ReturnStatement;
use Icecave\Pasta\AST\Root;
use Icecave\Pasta\AST\StatementBlock;
+use Icecave\Pasta\AST\UseStatement;
use Icecave\Pasta\AST\Variable;
use PHPUnit_Framework_TestCase;
@@ -39,24 +41,38 @@ public function setUp()
public function testVisitRoot()
{
$node = new Root;
- $node->add(new PhpBlock);
+ $node->add(new ContentBlock('<html></html>'));
$code = $this->_generator->visitRoot($node);
- $this->assertSame('<?php' . PHP_EOL, $code);
+ $this->assertSame('<html></html>', $code);
}
- public function testVisitRootShortTag()
+ public function testVisitRootEmpty()
{
$node = new Root;
- $node->add(new PhpBlock(true));
$code = $this->_generator->visitRoot($node);
- $this->assertSame('<?' . PHP_EOL, $code);
+ $this->assertSame('', $code);
}
- public function testVisitRootEmpty()
+ public function testVisitPhpBlock()
+ {
+ $node = new PhpBlock;
+ $code = $this->_generator->visitPhpBlock($node);
+ $this->assertSame('<' . '?php' . PHP_EOL . '?' . '>', $code);
+ }
+
+ public function testVisitPhpBlockShortTag()
+ {
+ $node = new PhpBlock(true);
+ $code = $this->_generator->visitPhpBlock($node);
+ $this->assertSame('<' . '?' . PHP_EOL . '?' . '>', $code);
+ }
+
+ public function testVisitRootWithSinglePhpBlock()
{
$node = new Root;
+ $node->add(new PhpBlock);
$code = $this->_generator->visitRoot($node);
- $this->assertSame('', $code);
+ $this->assertSame('<' . '?php' . PHP_EOL, $code);
}
public function testVisitContentBlock()
@@ -491,6 +507,30 @@ public function testVisitVariable()
$this->assertSame($expected, $code);
}
+ public function testVisitNamespaceStatement()
+ {
+ $node = new NamespaceStatement(QualifiedName::fromString('Foo\Bar\Spam'));
+ $code = $this->_generator->visitNamespaceStatement($node);
+
+ $this->assertSame('namespace Foo\Bar\Spam;' . PHP_EOL, $code);
+ }
+
+ public function testVisitUseStatement()
+ {
+ $node = new UseStatement(QualifiedName::fromString('Foo\Bar\Spam'));
+ $code = $this->_generator->visitUseStatement($node);
+
+ $this->assertSame('use Foo\Bar\Spam;' . PHP_EOL, $code);
+ }
+
+ public function testVisitUseStatementWithAlias()
+ {
+ $node = new UseStatement(QualifiedName::fromString('Foo\Bar\Spam'), 'Alias');
+ $code = $this->_generator->visitUseStatement($node);
+
+ $this->assertSame('use Foo\Bar\Spam as Alias;' . PHP_EOL, $code);
+ }
+
public function testVisitEquals()
{
$node = new Equals(new Literal(123), new Literal(456));

0 comments on commit 330417b

Please sign in to comment.