This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
273 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Language\AST\Visitor; | ||
|
||
abstract class AbstractVisitor implements VisitorInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Language\AST\Visitor; | ||
|
||
interface AcceptVisitorInterface | ||
{ | ||
|
||
/** | ||
* @param VisitorInterface $visitor | ||
* @param string|null $key | ||
* @param array $path | ||
* @return bool | ||
*/ | ||
public function accept(VisitorInterface $visitor, ?string $key = null, array $path = []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Language\AST\Visitor; | ||
|
||
trait AcceptVisitorTrait | ||
{ | ||
|
||
protected static $visitorKindToChildNodesMap = [ | ||
'Name' => [], | ||
|
||
'Document' => ['definitions'], | ||
'OperationDefinition' => [ | ||
'name', | ||
'variableDefinitions', | ||
'directives', | ||
'selectionSet', | ||
], | ||
'VariableDefinition' => ['variable', 'type', 'defaultValue'], | ||
'Variable' => ['name'], | ||
'SelectionSet' => ['selections'], | ||
'Field' => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], | ||
'Argument' => ['name', 'value'], | ||
|
||
'FragmentSpread' => ['name', 'directives'], | ||
'InlineFragment' => ['typeCondition', 'directives', 'selectionSet'], | ||
'FragmentDefinition' => [ | ||
'name', | ||
// Note: fragment variable definitions are experimental and may be changed | ||
// or removed in the future. | ||
'variableDefinitions', | ||
'typeCondition', | ||
'directives', | ||
'selectionSet', | ||
], | ||
|
||
'IntValue' => [], | ||
'FloatValue' => [], | ||
'StringValue' => [], | ||
'BooleanValue' => [], | ||
'NullValue' => [], | ||
'EnumValue' => [], | ||
'ListValue' => ['values'], | ||
'ObjectValue' => ['fields'], | ||
'ObjectField' => ['name', 'value'], | ||
|
||
'Directive' => ['name', 'arguments'], | ||
|
||
'NamedType' => ['name'], | ||
'ListType' => ['type'], | ||
'NonNullType' => ['type'], | ||
|
||
'SchemaDefinition' => ['directives', 'operationTypes'], | ||
'OperationTypeDefinition' => ['type'], | ||
|
||
'ScalarTypeDefinition' => ['description', 'name', 'directives'], | ||
'ObjectTypeDefinition' => [ | ||
'description', | ||
'name', | ||
'interfaces', | ||
'directives', | ||
'fields', | ||
], | ||
'FieldDefinition' => ['description', 'name', 'arguments', 'type', 'directives'], | ||
'InputValueDefinition' => [ | ||
'description', | ||
'name', | ||
'type', | ||
'defaultValue', | ||
'directives', | ||
], | ||
'InterfaceTypeDefinition' => ['description', 'name', 'directives', 'fields'], | ||
'UnionTypeDefinition' => ['description', 'name', 'directives', 'types'], | ||
'EnumTypeDefinition' => ['description', 'name', 'directives', 'values'], | ||
'EnumValueDefinition' => ['description', 'name', 'directives'], | ||
'InputObjectTypeDefinition' => ['description', 'name', 'directives', 'fields'], | ||
|
||
'ScalarTypeExtension' => ['name', 'directives'], | ||
'ObjectTypeExtension' => ['name', 'interfaces', 'directives', 'fields'], | ||
'InterfaceTypeExtension' => ['name', 'directives', 'fields'], | ||
'UnionTypeExtension' => ['name', 'directives', 'types'], | ||
'EnumTypeExtension' => ['name', 'directives', 'values'], | ||
'InputObjectTypeExtension' => ['name', 'directives', 'fields'], | ||
|
||
'DirectiveDefinition' => ['description', 'name', 'arguments', 'locations'], | ||
]; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
abstract public function getKind(): string; | ||
|
||
/** | ||
* @param VisitorInterface $visitor | ||
* @param string|null $key | ||
* @return bool | ||
*/ | ||
public function accept(VisitorInterface $visitor, ?string $key = null, array $path = []) | ||
{ | ||
/** @noinspection PhpParamsInspection */ | ||
if ($visitor->enterNode($this, $key, $path)) { | ||
foreach (self::$visitorKindToChildNodesMap[$this->getKind()] as $name) { | ||
$value = $this->{$name}; | ||
if (is_array($value) && !empty($value)) { | ||
$path[] = $name; | ||
foreach ($value as $k => $v) { | ||
if ($v instanceof AcceptVisitorInterface) { | ||
$path[] = $k; | ||
$v->accept($visitor, $k, $path); | ||
$path = array_slice($path, 0, count($path) - 1); | ||
} | ||
} | ||
$path = array_slice($path, 0, count($path) - 1); | ||
} else { | ||
if ($value instanceof AcceptVisitorInterface) { | ||
$path[] = $name; | ||
$value->accept($visitor, $name, $path); | ||
$path = array_slice($path, 0, count($path) - 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** @noinspection PhpParamsInspection */ | ||
return $visitor->leaveNode($this, $key, $path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Language\AST\Visitor; | ||
|
||
use Digia\GraphQL\Language\AST\Node\NodeInterface; | ||
|
||
interface VisitorInterface | ||
{ | ||
|
||
/** | ||
* @param NodeInterface $node | ||
* @param string|null $key | ||
* @param array $path | ||
* @return bool | ||
*/ | ||
public function enterNode(NodeInterface $node, ?string $key = null, array $path = []): bool; | ||
|
||
/** | ||
* @param NodeInterface $node | ||
* @param string|null $key | ||
* @param array $path | ||
* @return bool | ||
*/ | ||
public function leaveNode(NodeInterface $node, ?string $key = null, array $path = []): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Digia\GraphQL\Test\Functional\Language; | ||
|
||
use Digia\GraphQL\Language\AST\Node\DocumentNode; | ||
use Digia\GraphQL\Language\AST\Node\NodeInterface; | ||
use Digia\GraphQL\Language\AST\Visitor\AbstractVisitor; | ||
use Digia\GraphQL\Test\TestCase; | ||
use function Digia\GraphQL\parse; | ||
|
||
class VisitorTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @throws \Digia\GraphQL\Error\GraphQLError | ||
* @throws \Exception | ||
*/ | ||
public function testValidatesPathArgument() | ||
{ | ||
$visited = []; | ||
|
||
/** @var DocumentNode $ast */ | ||
$ast = parse('{ a }'); | ||
|
||
$visitor = new Visitor( | ||
function (NodeInterface $node, ?string $key = null, array $path = []) use (&$visited) { | ||
$visited[] = ['enter', array_slice($path, 0)]; | ||
return true; | ||
}, | ||
function (NodeInterface $node, ?string $key = null, array $path = []) use (&$visited) { | ||
$visited[] = ['leave', array_slice($path, 0)]; | ||
return true; | ||
} | ||
); | ||
|
||
$ast->accept($visitor); | ||
|
||
$this->assertEquals([ | ||
['enter', []], | ||
['enter', ['definitions', 0]], | ||
['enter', ['definitions', 0, 'selectionSet']], | ||
['enter', ['definitions', 0, 'selectionSet', 'selections', 0]], | ||
['enter', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], | ||
['leave', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], | ||
['leave', ['definitions', 0, 'selectionSet', 'selections', 0]], | ||
['leave', ['definitions', 0, 'selectionSet']], | ||
['leave', ['definitions', 0]], | ||
['leave', []], | ||
], $visited); | ||
} | ||
} | ||
|
||
class Visitor extends AbstractVisitor | ||
{ | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
protected $enterFunction; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
protected $leaveFunction; | ||
|
||
/** | ||
* TestableVisitor constructor. | ||
* @param callable $enterFunction | ||
* @param callable $leaveFunction | ||
*/ | ||
public function __construct(callable $enterFunction, callable $leaveFunction) | ||
{ | ||
$this->enterFunction = $enterFunction; | ||
$this->leaveFunction = $leaveFunction; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function enterNode(NodeInterface $node, ?string $key = null, array $path = []): bool | ||
{ | ||
return call_user_func($this->enterFunction, $node, $key, $path); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function leaveNode(NodeInterface $node, ?string $key = null, array $path = []): bool | ||
{ | ||
return call_user_func($this->leaveFunction, $node, $key, $path); | ||
} | ||
} |