Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Add dummy AST printer
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 27, 2018
1 parent 9493b83 commit 928281e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"./src/Language/blockStringValue.php",
"./src/Language/helpers.php",
"./src/Language/parse.php",
"./src/Language/print.php",
"./src/Type/definition.php",
"./src/Type/directives.php",
"./src/Type/scalars.php",
Expand All @@ -34,6 +35,7 @@
"./src/Language/blockStringValue.php",
"./src/Language/helpers.php",
"./src/Language/parse.php",
"./src/Language/print.php",
"./src/Type/definition.php",
"./src/Type/directives.php",
"./src/Type/scalars.php",
Expand Down
5 changes: 5 additions & 0 deletions src/Language/AST/Node/Contract/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function getKind(): string;
* @return Location|null
*/
public function getLocation(): ?Location;

/**
* @return string
*/
public function toJSON(): string;
}
13 changes: 7 additions & 6 deletions src/Language/Contract/PrinterInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: chris
* Date: 27/02/2018
* Time: 9.53
*/

namespace Digia\GraphQL\Language\Contract;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;

interface PrinterInterface
{

/**
* @param NodeInterface $node
* @return string
*/
public function print(NodeInterface $node): string;
}
19 changes: 19 additions & 0 deletions src/Language/Printer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Digia\GraphQL\Language;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;
use Digia\GraphQL\Language\Contract\PrinterInterface;

class Printer implements PrinterInterface
{

/**
* @inheritdoc
*/
public function print(NodeInterface $node): string
{
// TODO: Implement proper printing of AST trees
return $node->toJSON();
}
}
20 changes: 20 additions & 0 deletions src/Language/print.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Digia\GraphQL\Language;

use Digia\GraphQL\Language\AST\Node\Contract\NodeInterface;

/**
* @param NodeInterface $node
* @return string
*/
function printNode(NodeInterface $node): string
{
static $instance = null;

if (null === $instance) {
$instance = new Printer();
}

return $instance->print($node);
}
3 changes: 2 additions & 1 deletion tests/Functional/Language/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Digia\GraphQL\Language\AST\Node\DocumentNode;
use Digia\GraphQL\Test\TestCase;
use function Digia\GraphQL\Language\parse;
use function Digia\GraphQL\Language\printNode;
use function Digia\GraphQL\Util\readFile;

class SchemaPrinterTest extends TestCase
Expand All @@ -25,7 +26,7 @@ public function testDoesNotAlterAST()
$ast = parse($kitchenSink);
$astBefore = $ast->toJSON();

// TODO: Print the schema once the Printer is implemented
printNode($ast);

$this->assertEquals($ast->toJSON(), $astBefore);
}
Expand Down

0 comments on commit 928281e

Please sign in to comment.