Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ArgumentNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::ARGUMENT;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
|
||||
class BooleanValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::BOOLEAN;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface DefinitionNode
|
||||
{
|
||||
/**
|
||||
* export type DefinitionNode = OperationDefinitionNode
|
||||
* | FragmentDefinitionNode
|
||||
* | TypeSystemDefinitionNode // experimental non-spec addition.
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DirectiveDefinitionNode extends Node implements TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::DIRECTIVE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]
|
||||
*/
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var NameNode[]
|
||||
*/
|
||||
public $locations;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DirectiveNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::DIRECTIVE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]
|
||||
*/
|
||||
public $arguments;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class DocumentNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::DOCUMENT;
|
||||
|
||||
/**
|
||||
* @var DefinitionNode[]
|
||||
*/
|
||||
public $definitions;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::ENUM_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var EnumValueDefinitionNode[]
|
||||
*/
|
||||
public $values;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumValueDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::ENUM_VALUE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class EnumValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::ENUM;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FieldDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::FIELD_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var InputValueDefinitionNode[]
|
||||
*/
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FieldNode extends Node implements SelectionNode
|
||||
{
|
||||
public $kind = NodeKind::FIELD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NameNode|null
|
||||
*/
|
||||
public $alias;
|
||||
|
||||
/**
|
||||
* @var ArgumentNode[]|null
|
||||
*/
|
||||
public $arguments;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode|null
|
||||
*/
|
||||
public $selectionSet;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FloatValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::FLOAT;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FragmentDefinitionNode extends Node implements DefinitionNode, HasSelectionSet
|
||||
{
|
||||
public $kind = NodeKind::FRAGMENT_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
public $typeCondition;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
public $selectionSet;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class FragmentSpreadNode extends Node implements SelectionNode
|
||||
{
|
||||
public $kind = NodeKind::FRAGMENT_SPREAD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface HasSelectionSet
|
||||
{
|
||||
/**
|
||||
* export type DefinitionNode = OperationDefinitionNode
|
||||
* | FragmentDefinitionNode
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InlineFragmentNode extends Node implements SelectionNode
|
||||
{
|
||||
public $kind = NodeKind::INLINE_FRAGMENT;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
public $typeCondition;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]|null
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
public $selectionSet;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InputObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::INPUT_OBJECT_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var InputValueDefinitionNode[]
|
||||
*/
|
||||
public $fields;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InputValueDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::INPUT_VALUE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
public $defaultValue;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class IntValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::INT;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class InterfaceTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::INTERFACE_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]
|
||||
*/
|
||||
public $fields = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ListTypeNode extends Node implements TypeNode
|
||||
{
|
||||
public $kind = NodeKind::LIST_TYPE;
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
public $type;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ListValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::LST;
|
||||
|
||||
/**
|
||||
* @var ValueNode[]
|
||||
*/
|
||||
public $values;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Language\Source;
|
||||
use GraphQL\Language\Token;
|
||||
|
||||
/**
|
||||
* Contains a range of UTF-8 character offsets and token references that
|
||||
* identify the region of the source from which the AST derived.
|
||||
*/
|
||||
class Location
|
||||
{
|
||||
/**
|
||||
* The character offset at which this Node begins.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $start;
|
||||
|
||||
/**
|
||||
* The character offset at which this Node ends.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $end;
|
||||
|
||||
/**
|
||||
* The Token at which this Node begins.
|
||||
*
|
||||
* @var Token
|
||||
*/
|
||||
public $startToken;
|
||||
|
||||
/**
|
||||
* The Token at which this Node ends.
|
||||
*
|
||||
* @var Token
|
||||
*/
|
||||
public $endToken;
|
||||
|
||||
/**
|
||||
* The Source document the AST represents.
|
||||
*
|
||||
* @var Source|null
|
||||
*/
|
||||
public $source;
|
||||
|
||||
/**
|
||||
* @param $start
|
||||
* @param $end
|
||||
* @return static
|
||||
*/
|
||||
public static function create($start, $end)
|
||||
{
|
||||
$tmp = new static();
|
||||
$tmp->start = $start;
|
||||
$tmp->end = $end;
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
public function __construct(Token $startToken = null, Token $endToken = null, Source $source = null)
|
||||
{
|
||||
$this->startToken = $startToken;
|
||||
$this->endToken = $endToken;
|
||||
$this->source = $source;
|
||||
|
||||
if ($startToken && $endToken) {
|
||||
$this->start = $startToken->start;
|
||||
$this->end = $endToken->end;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NameNode extends Node implements TypeNode
|
||||
{
|
||||
public $kind = NodeKind::NAME;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NamedTypeNode extends Node implements TypeNode
|
||||
{
|
||||
public $kind = NodeKind::NAMED_TYPE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Utils\Utils;
|
||||
|
||||
abstract class Node
|
||||
{
|
||||
/**
|
||||
type Node = NameNode
|
||||
| DocumentNode
|
||||
| OperationDefinitionNode
|
||||
| VariableDefinitionNode
|
||||
| VariableNode
|
||||
| SelectionSetNode
|
||||
| FieldNode
|
||||
| ArgumentNode
|
||||
| FragmentSpreadNode
|
||||
| InlineFragmentNode
|
||||
| FragmentDefinitionNode
|
||||
| IntValueNode
|
||||
| FloatValueNode
|
||||
| StringValueNode
|
||||
| BooleanValueNode
|
||||
| EnumValueNode
|
||||
| ListValueNode
|
||||
| ObjectValueNode
|
||||
| ObjectFieldNode
|
||||
| DirectiveNode
|
||||
| ListTypeNode
|
||||
| NonNullTypeNode
|
||||
*/
|
||||
|
||||
public $kind;
|
||||
|
||||
/**
|
||||
* @var Location
|
||||
*/
|
||||
public $loc;
|
||||
|
||||
/**
|
||||
* @param array $vars
|
||||
*/
|
||||
public function __construct(array $vars)
|
||||
{
|
||||
if (!empty($vars)) {
|
||||
Utils::assign($this, $vars);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function cloneDeep()
|
||||
{
|
||||
return $this->cloneValue($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return array|Node
|
||||
*/
|
||||
private function cloneValue($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$cloned = [];
|
||||
foreach ($value as $key => $arrValue) {
|
||||
$cloned[$key] = $this->cloneValue($arrValue);
|
||||
}
|
||||
} else if ($value instanceof Node) {
|
||||
$cloned = clone $value;
|
||||
foreach (get_object_vars($cloned) as $prop => $propValue) {
|
||||
$cloned->{$prop} = $this->cloneValue($propValue);
|
||||
}
|
||||
} else {
|
||||
$cloned = $value;
|
||||
}
|
||||
|
||||
return $cloned;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$tmp = $this->toArray(true);
|
||||
return json_encode($tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $recursive
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($recursive = false)
|
||||
{
|
||||
if ($recursive) {
|
||||
return $this->recursiveToArray($this);
|
||||
} else {
|
||||
$tmp = (array) $this;
|
||||
|
||||
$tmp['loc'] = [
|
||||
'start' => $this->loc->start,
|
||||
'end' => $this->loc->end
|
||||
];
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node $node
|
||||
* @return array
|
||||
*/
|
||||
private function recursiveToArray(Node $node)
|
||||
{
|
||||
$result = [
|
||||
'kind' => $node->kind,
|
||||
'loc' => [
|
||||
'start' => $node->loc->start,
|
||||
'end' => $node->loc->end
|
||||
]
|
||||
];
|
||||
|
||||
foreach (get_object_vars($node) as $prop => $propValue) {
|
||||
if (isset($result[$prop]))
|
||||
continue;
|
||||
|
||||
if (is_array($propValue) || $propValue instanceof NodeList) {
|
||||
$tmp = [];
|
||||
foreach ($propValue as $tmp1) {
|
||||
$tmp[] = $tmp1 instanceof Node ? $this->recursiveToArray($tmp1) : (array) $tmp1;
|
||||
}
|
||||
} else if ($propValue instanceof Node) {
|
||||
$tmp = $this->recursiveToArray($propValue);
|
||||
} else if (is_scalar($propValue) || null === $propValue) {
|
||||
$tmp = $propValue;
|
||||
} else {
|
||||
$tmp = null;
|
||||
}
|
||||
|
||||
$result[$prop] = $tmp;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NodeKind
|
||||
{
|
||||
// constants from language/kinds.js:
|
||||
|
||||
const NAME = 'Name';
|
||||
|
||||
// Document
|
||||
|
||||
const DOCUMENT = 'Document';
|
||||
const OPERATION_DEFINITION = 'OperationDefinition';
|
||||
const VARIABLE_DEFINITION = 'VariableDefinition';
|
||||
const VARIABLE = 'Variable';
|
||||
const SELECTION_SET = 'SelectionSet';
|
||||
const FIELD = 'Field';
|
||||
const ARGUMENT = 'Argument';
|
||||
|
||||
// Fragments
|
||||
|
||||
const FRAGMENT_SPREAD = 'FragmentSpread';
|
||||
const INLINE_FRAGMENT = 'InlineFragment';
|
||||
const FRAGMENT_DEFINITION = 'FragmentDefinition';
|
||||
|
||||
// Values
|
||||
|
||||
const INT = 'IntValue';
|
||||
const FLOAT = 'FloatValue';
|
||||
const STRING = 'StringValue';
|
||||
const BOOLEAN = 'BooleanValue';
|
||||
const ENUM = 'EnumValue';
|
||||
const NULL = 'NullValue';
|
||||
const LST = 'ListValue';
|
||||
const OBJECT = 'ObjectValue';
|
||||
const OBJECT_FIELD = 'ObjectField';
|
||||
|
||||
// Directives
|
||||
|
||||
const DIRECTIVE = 'Directive';
|
||||
|
||||
// Types
|
||||
|
||||
const NAMED_TYPE = 'NamedType';
|
||||
const LIST_TYPE = 'ListType';
|
||||
const NON_NULL_TYPE = 'NonNullType';
|
||||
|
||||
// Type System Definitions
|
||||
|
||||
const SCHEMA_DEFINITION = 'SchemaDefinition';
|
||||
const OPERATION_TYPE_DEFINITION = 'OperationTypeDefinition';
|
||||
|
||||
// Type Definitions
|
||||
|
||||
const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
|
||||
const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
|
||||
const FIELD_DEFINITION = 'FieldDefinition';
|
||||
const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
|
||||
const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
|
||||
const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
|
||||
const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
|
||||
const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
|
||||
const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';
|
||||
|
||||
// Type Extensions
|
||||
|
||||
const TYPE_EXTENSION_DEFINITION = 'TypeExtensionDefinition';
|
||||
|
||||
// Directive Definitions
|
||||
|
||||
const DIRECTIVE_DEFINITION = 'DirectiveDefinition';
|
||||
|
||||
/**
|
||||
* @todo conver to const array when moving to PHP5.6
|
||||
* @var array
|
||||
*/
|
||||
public static $classMap = [
|
||||
NodeKind::NAME => NameNode::class,
|
||||
|
||||
// Document
|
||||
NodeKind::DOCUMENT => DocumentNode::class,
|
||||
NodeKind::OPERATION_DEFINITION => OperationDefinitionNode::class,
|
||||
NodeKind::VARIABLE_DEFINITION => VariableDefinitionNode::class,
|
||||
NodeKind::VARIABLE => VariableNode::class,
|
||||
NodeKind::SELECTION_SET => SelectionSetNode::class,
|
||||
NodeKind::FIELD => FieldNode::class,
|
||||
NodeKind::ARGUMENT => ArgumentNode::class,
|
||||
|
||||
// Fragments
|
||||
NodeKind::FRAGMENT_SPREAD => FragmentSpreadNode::class,
|
||||
NodeKind::INLINE_FRAGMENT => InlineFragmentNode::class,
|
||||
NodeKind::FRAGMENT_DEFINITION => FragmentDefinitionNode::class,
|
||||
|
||||
// Values
|
||||
NodeKind::INT => IntValueNode::class,
|
||||
NodeKind::FLOAT => FloatValueNode::class,
|
||||
NodeKind::STRING => StringValueNode::class,
|
||||
NodeKind::BOOLEAN => BooleanValueNode::class,
|
||||
NodeKind::ENUM => EnumValueNode::class,
|
||||
NodeKind::NULL => NullValueNode::class,
|
||||
NodeKind::LST => ListValueNode::class,
|
||||
NodeKind::OBJECT => ObjectValueNode::class,
|
||||
NodeKind::OBJECT_FIELD => ObjectFieldNode::class,
|
||||
|
||||
// Directives
|
||||
NodeKind::DIRECTIVE => DirectiveNode::class,
|
||||
|
||||
// Types
|
||||
NodeKind::NAMED_TYPE => NamedTypeNode::class,
|
||||
NodeKind::LIST_TYPE => ListTypeNode::class,
|
||||
NodeKind::NON_NULL_TYPE => NonNullTypeNode::class,
|
||||
|
||||
// Type System Definitions
|
||||
NodeKind::SCHEMA_DEFINITION => SchemaDefinitionNode::class,
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => OperationTypeDefinitionNode::class,
|
||||
|
||||
// Type Definitions
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => ScalarTypeDefinitionNode::class,
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => ObjectTypeDefinitionNode::class,
|
||||
NodeKind::FIELD_DEFINITION => FieldDefinitionNode::class,
|
||||
NodeKind::INPUT_VALUE_DEFINITION => InputValueDefinitionNode::class,
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => InterfaceTypeDefinitionNode::class,
|
||||
NodeKind::UNION_TYPE_DEFINITION => UnionTypeDefinitionNode::class,
|
||||
NodeKind::ENUM_TYPE_DEFINITION => EnumTypeDefinitionNode::class,
|
||||
NodeKind::ENUM_VALUE_DEFINITION => EnumValueDefinitionNode::class,
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION =>InputObjectTypeDefinitionNode::class,
|
||||
|
||||
// Type Extensions
|
||||
NodeKind::TYPE_EXTENSION_DEFINITION => TypeExtensionDefinitionNode::class,
|
||||
|
||||
// Directive Definitions
|
||||
NodeKind::DIRECTIVE_DEFINITION => DirectiveDefinitionNode::class
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
use GraphQL\Utils\AST;
|
||||
|
||||
/**
|
||||
* Class NodeList
|
||||
*
|
||||
* @package GraphQL\Utils
|
||||
*/
|
||||
class NodeList implements \ArrayAccess, \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $nodes;
|
||||
|
||||
/**
|
||||
* @param array $nodes
|
||||
* @return static
|
||||
*/
|
||||
public static function create(array $nodes)
|
||||
{
|
||||
return new static($nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* NodeList constructor.
|
||||
* @param array $nodes
|
||||
*/
|
||||
public function __construct(array $nodes)
|
||||
{
|
||||
$this->nodes = $nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->nodes[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
$item = $this->nodes[$offset];
|
||||
|
||||
if (is_array($item) && isset($item['kind'])) {
|
||||
$this->nodes[$offset] = $item = AST::fromArray($item);
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (is_array($value) && isset($value['kind'])) {
|
||||
$value = AST::fromArray($value);
|
||||
}
|
||||
$this->nodes[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->nodes[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @param mixed $replacement
|
||||
* @return NodeList
|
||||
*/
|
||||
public function splice($offset, $length, $replacement = null)
|
||||
{
|
||||
return new NodeList(array_splice($this->nodes, $offset, $length, $replacement));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $list
|
||||
* @return NodeList
|
||||
*/
|
||||
public function merge($list)
|
||||
{
|
||||
if ($list instanceof NodeList) {
|
||||
$list = $list->nodes;
|
||||
}
|
||||
return new NodeList(array_merge($this->nodes, $list));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Generator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
$count = count($this->nodes);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
yield $this->offsetGet($i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->nodes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NonNullTypeNode extends Node implements TypeNode
|
||||
{
|
||||
public $kind = NodeKind::NON_NULL_TYPE;
|
||||
|
||||
/**
|
||||
* @var NameNode | ListTypeNode
|
||||
*/
|
||||
public $type;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class NullValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::NULL;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
|
||||
class ObjectFieldNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::OBJECT_FIELD;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var ValueNode
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::OBJECT_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]
|
||||
*/
|
||||
public $interfaces = [];
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var FieldDefinitionNode[]
|
||||
*/
|
||||
public $fields;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ObjectValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::OBJECT;
|
||||
|
||||
/**
|
||||
* @var ObjectFieldNode[]
|
||||
*/
|
||||
public $fields;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class OperationDefinitionNode extends Node implements DefinitionNode, HasSelectionSet
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::OPERATION_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string (oneOf 'query', 'mutation'))
|
||||
*/
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* @var VariableDefinitionNode[]
|
||||
*/
|
||||
public $variableDefinitions;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var SelectionSetNode
|
||||
*/
|
||||
public $selectionSet;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class OperationTypeDefinitionNode extends Node
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::OPERATION_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* One of 'query' | 'mutation' | 'subscription'
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $operation;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode
|
||||
*/
|
||||
public $type;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class ScalarTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::SCALAR_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class SchemaDefinitionNode extends Node implements TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::SCHEMA_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var OperationTypeDefinitionNode[]
|
||||
*/
|
||||
public $operationTypes;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface SelectionNode
|
||||
{
|
||||
/**
|
||||
* export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class SelectionSetNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::SELECTION_SET;
|
||||
|
||||
/**
|
||||
* @var SelectionNode[]
|
||||
*/
|
||||
public $selections;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class StringValueNode extends Node implements ValueNode
|
||||
{
|
||||
public $kind = NodeKind::STRING;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $value;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface TypeDefinitionNode extends TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
export type TypeDefinitionNode = ScalarTypeDefinitionNode
|
||||
| ObjectTypeDefinitionNode
|
||||
| InterfaceTypeDefinitionNode
|
||||
| UnionTypeDefinitionNode
|
||||
| EnumTypeDefinitionNode
|
||||
| InputObjectTypeDefinitionNode
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class TypeExtensionDefinitionNode extends Node implements TypeSystemDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::TYPE_EXTENSION_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var ObjectTypeDefinitionNode
|
||||
*/
|
||||
public $definition;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
|
||||
interface TypeNode
|
||||
{
|
||||
/**
|
||||
export type TypeNode = NamedTypeNode
|
||||
| ListTypeNode
|
||||
| NonNullTypeNode
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
interface TypeSystemDefinitionNode extends DefinitionNode
|
||||
{
|
||||
/**
|
||||
export type TypeSystemDefinitionNode = SchemaDefinitionNode
|
||||
| TypeDefinitionNode
|
||||
| TypeExtensionDefinitionNode
|
||||
| DirectiveDefinitionNode
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class UnionTypeDefinitionNode extends Node implements TypeDefinitionNode
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $kind = NodeKind::UNION_TYPE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var DirectiveNode[]
|
||||
*/
|
||||
public $directives;
|
||||
|
||||
/**
|
||||
* @var NamedTypeNode[]
|
||||
*/
|
||||
public $types = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
/**
|
||||
export type ValueNode = VariableNode
|
||||
| IntValueNode
|
||||
| FloatValueNode
|
||||
| StringValueNode
|
||||
| BooleanValueNode
|
||||
| EnumValueNode
|
||||
| ListValueNode
|
||||
| ObjectValueNode
|
||||
*/
|
||||
interface ValueNode
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class VariableDefinitionNode extends Node implements DefinitionNode
|
||||
{
|
||||
public $kind = NodeKind::VARIABLE_DEFINITION;
|
||||
|
||||
/**
|
||||
* @var VariableNode
|
||||
*/
|
||||
public $variable;
|
||||
|
||||
/**
|
||||
* @var TypeNode
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var ValueNode|null
|
||||
*/
|
||||
public $defaultValue;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace GraphQL\Language\AST;
|
||||
|
||||
class VariableNode extends Node
|
||||
{
|
||||
public $kind = NodeKind::VARIABLE;
|
||||
|
||||
/**
|
||||
* @var NameNode
|
||||
*/
|
||||
public $name;
|
||||
}
|
||||
+612
@@ -0,0 +1,612 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Error\SyntaxError;
|
||||
use GraphQL\Utils\Utils;
|
||||
|
||||
/**
|
||||
* A Lexer is a stateful stream generator in that every time
|
||||
* it is advanced, it returns the next token in the Source. Assuming the
|
||||
* source lexes, the final Token emitted by the lexer will be of kind
|
||||
* EOF, after which the lexer will repeatedly return the same EOF token
|
||||
* whenever called.
|
||||
*
|
||||
* Algorithm is O(N) both on memory and time
|
||||
*/
|
||||
class Lexer
|
||||
{
|
||||
/**
|
||||
* @var Source
|
||||
*/
|
||||
public $source;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $options;
|
||||
|
||||
/**
|
||||
* The previously focused non-ignored token.
|
||||
*
|
||||
* @var Token
|
||||
*/
|
||||
public $lastToken;
|
||||
|
||||
/**
|
||||
* The currently focused non-ignored token.
|
||||
*
|
||||
* @var Token
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* The (1-indexed) line containing the current token.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $line;
|
||||
|
||||
/**
|
||||
* The character offset at which the current line begins.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $lineStart;
|
||||
|
||||
/**
|
||||
* Current cursor position for UTF8 encoding of the source
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $position;
|
||||
|
||||
/**
|
||||
* Current cursor position for ASCII representation of the source
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $byteStreamPosition;
|
||||
|
||||
/**
|
||||
* Lexer constructor.
|
||||
*
|
||||
* @param Source $source
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(Source $source, array $options = [])
|
||||
{
|
||||
$startOfFileToken = new Token(Token::SOF, 0, 0, 0, 0, null);
|
||||
|
||||
$this->source = $source;
|
||||
$this->options = $options;
|
||||
$this->lastToken = $startOfFileToken;
|
||||
$this->token = $startOfFileToken;
|
||||
$this->line = 1;
|
||||
$this->lineStart = 0;
|
||||
$this->position = $this->byteStreamPosition = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token
|
||||
*/
|
||||
public function advance()
|
||||
{
|
||||
$token = $this->lastToken = $this->token;
|
||||
|
||||
if ($token->kind !== Token::EOF) {
|
||||
do {
|
||||
$token = $token->next = $this->readToken($token);
|
||||
} while ($token->kind === Token::COMMENT);
|
||||
$this->token = $token;
|
||||
}
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Token
|
||||
*/
|
||||
public function nextToken()
|
||||
{
|
||||
trigger_error(__METHOD__ . ' is deprecated in favor of advance()', E_USER_DEPRECATED);
|
||||
return $this->advance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
private function readToken(Token $prev)
|
||||
{
|
||||
$bodyLength = $this->source->length;
|
||||
|
||||
$this->positionAfterWhitespace();
|
||||
$position = $this->position;
|
||||
|
||||
$line = $this->line;
|
||||
$col = 1 + $position - $this->lineStart;
|
||||
|
||||
if ($position >= $bodyLength) {
|
||||
return new Token(Token::EOF, $bodyLength, $bodyLength, $line, $col, $prev);
|
||||
}
|
||||
|
||||
// Read next char and advance string cursor:
|
||||
list (, $code, $bytes) = $this->readChar(true);
|
||||
|
||||
// SourceCharacter
|
||||
if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) {
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$position,
|
||||
'Cannot contain the invalid character ' . Utils::printCharCode($code)
|
||||
);
|
||||
}
|
||||
|
||||
switch ($code) {
|
||||
case 33: // !
|
||||
return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev);
|
||||
case 35: // #
|
||||
$this->moveStringCursor(-1, -1 * $bytes);
|
||||
return $this->readComment($line, $col, $prev);
|
||||
case 36: // $
|
||||
return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev);
|
||||
case 40: // (
|
||||
return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev);
|
||||
case 41: // )
|
||||
return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev);
|
||||
case 46: // .
|
||||
list (, $charCode1) = $this->readChar(true);
|
||||
list (, $charCode2) = $this->readChar(true);
|
||||
|
||||
if ($charCode1 === 46 && $charCode2 === 46) {
|
||||
return new Token(Token::SPREAD, $position, $position + 3, $line, $col, $prev);
|
||||
}
|
||||
break;
|
||||
case 58: // :
|
||||
return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev);
|
||||
case 61: // =
|
||||
return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev);
|
||||
case 64: // @
|
||||
return new Token(Token::AT, $position, $position + 1, $line, $col, $prev);
|
||||
case 91: // [
|
||||
return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev);
|
||||
case 93: // ]
|
||||
return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev);
|
||||
case 123: // {
|
||||
return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev);
|
||||
case 124: // |
|
||||
return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev);
|
||||
case 125: // }
|
||||
return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev);
|
||||
// A-Z
|
||||
case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72:
|
||||
case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80:
|
||||
case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88:
|
||||
case 89: case 90:
|
||||
// _
|
||||
case 95:
|
||||
// a-z
|
||||
case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104:
|
||||
case 105: case 106: case 107: case 108: case 109: case 110: case 111:
|
||||
case 112: case 113: case 114: case 115: case 116: case 117: case 118:
|
||||
case 119: case 120: case 121: case 122:
|
||||
return $this->moveStringCursor(-1, -1 * $bytes)
|
||||
->readName($line, $col, $prev);
|
||||
// -
|
||||
case 45:
|
||||
// 0-9
|
||||
case 48: case 49: case 50: case 51: case 52:
|
||||
case 53: case 54: case 55: case 56: case 57:
|
||||
return $this->moveStringCursor(-1, -1 * $bytes)
|
||||
->readNumber($line, $col, $prev);
|
||||
// "
|
||||
case 34:
|
||||
return $this->moveStringCursor(-1, -1 * $bytes)
|
||||
->readString($line, $col, $prev);
|
||||
}
|
||||
|
||||
$errMessage = $code === 39
|
||||
? "Unexpected single quote character ('), did you mean to use ". 'a double quote (")?'
|
||||
: 'Cannot parse the unexpected character ' . Utils::printCharCode($code) . '.';
|
||||
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$position,
|
||||
$errMessage
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an alphanumeric + underscore name from the source.
|
||||
*
|
||||
* [_A-Za-z][_0-9A-Za-z]*
|
||||
*
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
*/
|
||||
private function readName($line, $col, Token $prev)
|
||||
{
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
list ($char, $code) = $this->readChar();
|
||||
|
||||
while ($code && (
|
||||
$code === 95 || // _
|
||||
$code >= 48 && $code <= 57 || // 0-9
|
||||
$code >= 65 && $code <= 90 || // A-Z
|
||||
$code >= 97 && $code <= 122 // a-z
|
||||
)) {
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
}
|
||||
return new Token(
|
||||
Token::NAME,
|
||||
$start,
|
||||
$this->position,
|
||||
$line,
|
||||
$col,
|
||||
$prev,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a number token from the source file, either a float
|
||||
* or an int depending on whether a decimal point appears.
|
||||
*
|
||||
* Int: -?(0|[1-9][0-9]*)
|
||||
* Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)?
|
||||
*
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
private function readNumber($line, $col, Token $prev)
|
||||
{
|
||||
$value = '';
|
||||
$start = $this->position;
|
||||
list ($char, $code) = $this->readChar();
|
||||
|
||||
$isFloat = false;
|
||||
|
||||
if ($code === 45) { // -
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
}
|
||||
|
||||
// guard against leading zero's
|
||||
if ($code === 48) { // 0
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($code >= 48 && $code <= 57) {
|
||||
throw new SyntaxError($this->source, $this->position, "Invalid number, unexpected digit after 0: " . Utils::printCharCode($code));
|
||||
}
|
||||
} else {
|
||||
$value .= $this->readDigits();
|
||||
list ($char, $code) = $this->readChar();
|
||||
}
|
||||
|
||||
if ($code === 46) { // .
|
||||
$isFloat = true;
|
||||
$this->moveStringCursor(1, 1);
|
||||
|
||||
$value .= $char;
|
||||
$value .= $this->readDigits();
|
||||
list ($char, $code) = $this->readChar();
|
||||
}
|
||||
|
||||
if ($code === 69 || $code === 101) { // E e
|
||||
$isFloat = true;
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
if ($code === 43 || $code === 45) { // + -
|
||||
$value .= $char;
|
||||
$this->moveStringCursor(1, 1);
|
||||
}
|
||||
$value .= $this->readDigits();
|
||||
}
|
||||
|
||||
return new Token(
|
||||
$isFloat ? Token::FLOAT : Token::INT,
|
||||
$start,
|
||||
$this->position,
|
||||
$line,
|
||||
$col,
|
||||
$prev,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string with all digits + changes current string cursor position to point to the first char after digits
|
||||
*/
|
||||
private function readDigits()
|
||||
{
|
||||
list ($char, $code) = $this->readChar();
|
||||
|
||||
if ($code >= 48 && $code <= 57) { // 0 - 9
|
||||
$value = '';
|
||||
|
||||
do {
|
||||
$value .= $char;
|
||||
list ($char, $code) = $this->moveStringCursor(1, 1)->readChar();
|
||||
} while ($code >= 48 && $code <= 57); // 0 - 9
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($this->position > $this->source->length - 1) {
|
||||
$code = null;
|
||||
}
|
||||
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$this->position,
|
||||
'Invalid number, expected digit but got: ' . Utils::printCharCode($code)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $line
|
||||
* @param int $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
private function readString($line, $col, Token $prev)
|
||||
{
|
||||
$start = $this->position;
|
||||
|
||||
// Skip leading quote and read first string char:
|
||||
list ($char, $code, $bytes) = $this->moveStringCursor(1, 1)->readChar();
|
||||
|
||||
$chunk = '';
|
||||
$value = '';
|
||||
|
||||
while (
|
||||
$code &&
|
||||
// not LineTerminator
|
||||
$code !== 10 && $code !== 13 &&
|
||||
// not Quote (")
|
||||
$code !== 34
|
||||
) {
|
||||
$this->assertValidStringCharacterCode($code, $this->position);
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
|
||||
if ($code === 92) { // \
|
||||
$value .= $chunk;
|
||||
list (, $code) = $this->readChar(true);
|
||||
|
||||
switch ($code) {
|
||||
case 34: $value .= '"'; break;
|
||||
case 47: $value .= '/'; break;
|
||||
case 92: $value .= '\\'; break;
|
||||
case 98: $value .= chr(8); break; // \b (backspace)
|
||||
case 102: $value .= "\f"; break;
|
||||
case 110: $value .= "\n"; break;
|
||||
case 114: $value .= "\r"; break;
|
||||
case 116: $value .= "\t"; break;
|
||||
case 117:
|
||||
$position = $this->position;
|
||||
list ($hex) = $this->readChars(4, true);
|
||||
if (!preg_match('/[0-9a-fA-F]{4}/', $hex)) {
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$position - 1,
|
||||
'Invalid character escape sequence: \\u' . $hex
|
||||
);
|
||||
}
|
||||
$code = hexdec($hex);
|
||||
$this->assertValidStringCharacterCode($code, $position - 2);
|
||||
$value .= Utils::chr($code);
|
||||
break;
|
||||
default:
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$this->position - 1,
|
||||
'Invalid character escape sequence: \\' . Utils::chr($code)
|
||||
);
|
||||
}
|
||||
$chunk = '';
|
||||
} else {
|
||||
$chunk .= $char;
|
||||
}
|
||||
|
||||
list ($char, $code, $bytes) = $this->readChar();
|
||||
}
|
||||
|
||||
if ($code !== 34) {
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$this->position,
|
||||
'Unterminated string.'
|
||||
);
|
||||
}
|
||||
|
||||
$value .= $chunk;
|
||||
|
||||
// Skip trailing quote:
|
||||
$this->moveStringCursor(1, 1);
|
||||
|
||||
return new Token(
|
||||
Token::STRING,
|
||||
$start,
|
||||
$this->position,
|
||||
$line,
|
||||
$col,
|
||||
$prev,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
private function assertValidStringCharacterCode($code, $position)
|
||||
{
|
||||
// SourceCharacter
|
||||
if ($code < 0x0020 && $code !== 0x0009) {
|
||||
throw new SyntaxError(
|
||||
$this->source,
|
||||
$position,
|
||||
'Invalid character within String: ' . Utils::printCharCode($code)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from body starting at startPosition until it finds a non-whitespace
|
||||
* or commented character, then places cursor to the position of that character.
|
||||
*/
|
||||
private function positionAfterWhitespace()
|
||||
{
|
||||
while ($this->position < $this->source->length) {
|
||||
list(, $code, $bytes) = $this->readChar();
|
||||
|
||||
// Skip whitespace
|
||||
// tab | space | comma | BOM
|
||||
if ($code === 9 || $code === 32 || $code === 44 || $code === 0xFEFF) {
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
} else if ($code === 10) { // new line
|
||||
$this->moveStringCursor(1, $bytes);
|
||||
$this->line++;
|
||||
$this->lineStart = $this->position;
|
||||
} else if ($code === 13) { // carriage return
|
||||
list(, $nextCode, $nextBytes) = $this->moveStringCursor(1, $bytes)->readChar();
|
||||
|
||||
if ($nextCode === 10) { // lf after cr
|
||||
$this->moveStringCursor(1, $nextBytes);
|
||||
}
|
||||
$this->line++;
|
||||
$this->lineStart = $this->position;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a comment token from the source file.
|
||||
*
|
||||
* #[\u0009\u0020-\uFFFF]*
|
||||
*
|
||||
* @param $line
|
||||
* @param $col
|
||||
* @param Token $prev
|
||||
* @return Token
|
||||
*/
|
||||
private function readComment($line, $col, Token $prev)
|
||||
{
|
||||
$start = $this->position;
|
||||
$value = '';
|
||||
$bytes = 1;
|
||||
|
||||
do {
|
||||
list ($char, $code, $bytes) = $this->moveStringCursor(1, $bytes)->readChar();
|
||||
$value .= $char;
|
||||
} while (
|
||||
$code &&
|
||||
// SourceCharacter but not LineTerminator
|
||||
($code > 0x001F || $code === 0x0009)
|
||||
);
|
||||
|
||||
return new Token(
|
||||
Token::COMMENT,
|
||||
$start,
|
||||
$this->position,
|
||||
$line,
|
||||
$col,
|
||||
$prev,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads next UTF8Character from the byte stream, starting from $byteStreamPosition.
|
||||
*
|
||||
* @param bool $advance
|
||||
* @param int $byteStreamPosition
|
||||
* @return array
|
||||
*/
|
||||
private function readChar($advance = false, $byteStreamPosition = null)
|
||||
{
|
||||
if ($byteStreamPosition === null) {
|
||||
$byteStreamPosition = $this->byteStreamPosition;
|
||||
}
|
||||
|
||||
$code = 0;
|
||||
$utf8char = '';
|
||||
$bytes = 0;
|
||||
$positionOffset = 0;
|
||||
|
||||
if (isset($this->source->body[$byteStreamPosition])) {
|
||||
$ord = ord($this->source->body[$byteStreamPosition]);
|
||||
|
||||
if ($ord < 128) {
|
||||
$bytes = 1;
|
||||
} else if ($ord < 224) {
|
||||
$bytes = 2;
|
||||
} elseif ($ord < 240) {
|
||||
$bytes = 3;
|
||||
} else {
|
||||
$bytes = 4;
|
||||
}
|
||||
|
||||
$utf8char = '';
|
||||
for ($pos = $byteStreamPosition; $pos < $byteStreamPosition + $bytes; $pos++) {
|
||||
$utf8char .= $this->source->body[$pos];
|
||||
}
|
||||
$positionOffset = 1;
|
||||
$code = $bytes === 1 ? $ord : Utils::ord($utf8char);
|
||||
}
|
||||
|
||||
if ($advance) {
|
||||
$this->moveStringCursor($positionOffset, $bytes);
|
||||
}
|
||||
|
||||
return [$utf8char, $code, $bytes];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads next $numberOfChars UTF8 characters from the byte stream, starting from $byteStreamPosition.
|
||||
*
|
||||
* @param $numberOfChars
|
||||
* @param bool $advance
|
||||
* @param null $byteStreamPosition
|
||||
* @return array
|
||||
*/
|
||||
private function readChars($numberOfChars, $advance = false, $byteStreamPosition = null)
|
||||
{
|
||||
$result = '';
|
||||
$totalBytes = 0;
|
||||
$byteOffset = $byteStreamPosition ?: $this->byteStreamPosition;
|
||||
|
||||
for ($i = 0; $i < $numberOfChars; $i++) {
|
||||
list ($char, $code, $bytes) = $this->readChar(false, $byteOffset);
|
||||
$totalBytes += $bytes;
|
||||
$byteOffset += $bytes;
|
||||
$result .= $char;
|
||||
}
|
||||
if ($advance) {
|
||||
$this->moveStringCursor($numberOfChars, $totalBytes);
|
||||
}
|
||||
return [$result, $totalBytes];
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves internal string cursor position
|
||||
*
|
||||
* @param $positionOffset
|
||||
* @param $byteStreamOffset
|
||||
* @return $this
|
||||
*/
|
||||
private function moveStringCursor($positionOffset, $byteStreamOffset)
|
||||
{
|
||||
$this->position += $positionOffset;
|
||||
$this->byteStreamPosition += $byteStreamOffset;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+1259
File diff suppressed because it is too large.
Load diff
+310
@@ -0,0 +1,310 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Language\AST\ArgumentNode;
|
||||
use GraphQL\Language\AST\DirectiveDefinitionNode;
|
||||
use GraphQL\Language\AST\EnumTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\EnumValueDefinitionNode;
|
||||
use GraphQL\Language\AST\FieldDefinitionNode;
|
||||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\InputValueDefinitionNode;
|
||||
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\ListValueNode;
|
||||
use GraphQL\Language\AST\BooleanValueNode;
|
||||
use GraphQL\Language\AST\DirectiveNode;
|
||||
use GraphQL\Language\AST\DocumentNode;
|
||||
use GraphQL\Language\AST\EnumValueNode;
|
||||
use GraphQL\Language\AST\FieldNode;
|
||||
use GraphQL\Language\AST\FloatValueNode;
|
||||
use GraphQL\Language\AST\FragmentDefinitionNode;
|
||||
use GraphQL\Language\AST\FragmentSpreadNode;
|
||||
use GraphQL\Language\AST\InlineFragmentNode;
|
||||
use GraphQL\Language\AST\IntValueNode;
|
||||
use GraphQL\Language\AST\ListTypeNode;
|
||||
use GraphQL\Language\AST\NamedTypeNode;
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NonNullTypeNode;
|
||||
use GraphQL\Language\AST\NullValueNode;
|
||||
use GraphQL\Language\AST\ObjectFieldNode;
|
||||
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\ObjectValueNode;
|
||||
use GraphQL\Language\AST\OperationDefinitionNode;
|
||||
use GraphQL\Language\AST\OperationTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\SchemaDefinitionNode;
|
||||
use GraphQL\Language\AST\SelectionSetNode;
|
||||
use GraphQL\Language\AST\StringValueNode;
|
||||
use GraphQL\Language\AST\TypeExtensionDefinitionNode;
|
||||
use GraphQL\Language\AST\UnionTypeDefinitionNode;
|
||||
use GraphQL\Language\AST\VariableDefinitionNode;
|
||||
use GraphQL\Utils\Utils;
|
||||
|
||||
/**
|
||||
* Prints AST to string. Capable of printing GraphQL queries and Type definition language.
|
||||
* Useful for pretty-printing queries or printing back AST for logging, documentation, etc.
|
||||
*
|
||||
* Usage example:
|
||||
*
|
||||
* ```php
|
||||
* $query = 'query myQuery {someField}';
|
||||
* $ast = GraphQL\Language\Parser::parse($query);
|
||||
* $printed = GraphQL\Language\Printer::doPrint($ast);
|
||||
* ```
|
||||
*/
|
||||
class Printer
|
||||
{
|
||||
/**
|
||||
* Prints AST to string. Capable of printing GraphQL queries and Type definition language.
|
||||
*
|
||||
* @api
|
||||
* @param Node $ast
|
||||
* @return string
|
||||
*/
|
||||
public static function doPrint($ast)
|
||||
{
|
||||
static $instance;
|
||||
$instance = $instance ?: new static();
|
||||
return $instance->printAST($ast);
|
||||
}
|
||||
|
||||
protected function __construct()
|
||||
{}
|
||||
|
||||
public function printAST($ast)
|
||||
{
|
||||
return Visitor::visit($ast, [
|
||||
'leave' => [
|
||||
NodeKind::NAME => function(Node $node) {
|
||||
return '' . $node->value;
|
||||
},
|
||||
NodeKind::VARIABLE => function($node) {
|
||||
return '$' . $node->name;
|
||||
},
|
||||
NodeKind::DOCUMENT => function(DocumentNode $node) {
|
||||
return $this->join($node->definitions, "\n\n") . "\n";
|
||||
},
|
||||
NodeKind::OPERATION_DEFINITION => function(OperationDefinitionNode $node) {
|
||||
$op = $node->operation;
|
||||
$name = $node->name;
|
||||
$varDefs = $this->wrap('(', $this->join($node->variableDefinitions, ', '), ')');
|
||||
$directives = $this->join($node->directives, ' ');
|
||||
$selectionSet = $node->selectionSet;
|
||||
// Anonymous queries with no directives or variable definitions can use
|
||||
// the query short form.
|
||||
return !$name && !$directives && !$varDefs && $op === 'query'
|
||||
? $selectionSet
|
||||
: $this->join([$op, $this->join([$name, $varDefs]), $directives, $selectionSet], ' ');
|
||||
},
|
||||
NodeKind::VARIABLE_DEFINITION => function(VariableDefinitionNode $node) {
|
||||
return $node->variable . ': ' . $node->type . $this->wrap(' = ', $node->defaultValue);
|
||||
},
|
||||
NodeKind::SELECTION_SET => function(SelectionSetNode $node) {
|
||||
return $this->block($node->selections);
|
||||
},
|
||||
NodeKind::FIELD => function(FieldNode $node) {
|
||||
return $this->join([
|
||||
$this->wrap('', $node->alias, ': ') . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')'),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::ARGUMENT => function(ArgumentNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
// Fragments
|
||||
NodeKind::FRAGMENT_SPREAD => function(FragmentSpreadNode $node) {
|
||||
return '...' . $node->name . $this->wrap(' ', $this->join($node->directives, ' '));
|
||||
},
|
||||
NodeKind::INLINE_FRAGMENT => function(InlineFragmentNode $node) {
|
||||
return $this->join([
|
||||
"...",
|
||||
$this->wrap('on ', $node->typeCondition),
|
||||
$this->join($node->directives, ' '),
|
||||
$node->selectionSet
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::FRAGMENT_DEFINITION => function(FragmentDefinitionNode $node) {
|
||||
return "fragment {$node->name} on {$node->typeCondition} "
|
||||
. $this->wrap('', $this->join($node->directives, ' '), ' ')
|
||||
. $node->selectionSet;
|
||||
},
|
||||
|
||||
// Value
|
||||
NodeKind::INT => function(IntValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::FLOAT => function(FloatValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::STRING => function(StringValueNode $node) {
|
||||
return json_encode($node->value);
|
||||
},
|
||||
NodeKind::BOOLEAN => function(BooleanValueNode $node) {
|
||||
return $node->value ? 'true' : 'false';
|
||||
},
|
||||
NodeKind::NULL => function(NullValueNode $node) {
|
||||
return 'null';
|
||||
},
|
||||
NodeKind::ENUM => function(EnumValueNode $node) {
|
||||
return $node->value;
|
||||
},
|
||||
NodeKind::LST => function(ListValueNode $node) {
|
||||
return '[' . $this->join($node->values, ', ') . ']';
|
||||
},
|
||||
NodeKind::OBJECT => function(ObjectValueNode $node) {
|
||||
return '{' . $this->join($node->fields, ', ') . '}';
|
||||
},
|
||||
NodeKind::OBJECT_FIELD => function(ObjectFieldNode $node) {
|
||||
return $node->name . ': ' . $node->value;
|
||||
},
|
||||
|
||||
// DirectiveNode
|
||||
NodeKind::DIRECTIVE => function(DirectiveNode $node) {
|
||||
return '@' . $node->name . $this->wrap('(', $this->join($node->arguments, ', '), ')');
|
||||
},
|
||||
|
||||
// Type
|
||||
NodeKind::NAMED_TYPE => function(NamedTypeNode $node) {
|
||||
return $node->name;
|
||||
},
|
||||
NodeKind::LIST_TYPE => function(ListTypeNode $node) {
|
||||
return '[' . $node->type . ']';
|
||||
},
|
||||
NodeKind::NON_NULL_TYPE => function(NonNullTypeNode $node) {
|
||||
return $node->type . '!';
|
||||
},
|
||||
|
||||
// Type System Definitions
|
||||
NodeKind::SCHEMA_DEFINITION => function(SchemaDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'schema',
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->operationTypes)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => function(OperationTypeDefinitionNode $def) {
|
||||
return $def->operation . ': ' . $def->type;
|
||||
},
|
||||
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinitionNode $def) {
|
||||
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
|
||||
},
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'type',
|
||||
$def->name,
|
||||
$this->wrap('implements ', $this->join($def->interfaces, ', ')),
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::FIELD_DEFINITION => function(FieldDefinitionNode $def) {
|
||||
return $def->name
|
||||
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ': ' . $def->type
|
||||
. $this->wrap(' ', $this->join($def->directives, ' '));
|
||||
},
|
||||
NodeKind::INPUT_VALUE_DEFINITION => function(InputValueDefinitionNode $def) {
|
||||
return $this->join([
|
||||
$def->name . ': ' . $def->type,
|
||||
$this->wrap('= ', $def->defaultValue),
|
||||
$this->join($def->directives, ' ')
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'interface',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::UNION_TYPE_DEFINITION => function(UnionTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'union',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
'= ' . $this->join($def->types, ' | ')
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::ENUM_TYPE_DEFINITION => function(EnumTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'enum',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->values)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::ENUM_VALUE_DEFINITION => function(EnumValueDefinitionNode $def) {
|
||||
return $this->join([
|
||||
$def->name,
|
||||
$this->join($def->directives, ' ')
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinitionNode $def) {
|
||||
return $this->join([
|
||||
'input',
|
||||
$def->name,
|
||||
$this->join($def->directives, ' '),
|
||||
$this->block($def->fields)
|
||||
], ' ');
|
||||
},
|
||||
NodeKind::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinitionNode $def) {
|
||||
return "extend {$def->definition}";
|
||||
},
|
||||
NodeKind::DIRECTIVE_DEFINITION => function(DirectiveDefinitionNode $def) {
|
||||
return 'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')')
|
||||
. ' on ' . $this->join($def->locations, ' | ');
|
||||
}
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* If maybeString is not null or empty, then wrap with start and end, otherwise
|
||||
* print an empty string.
|
||||
*/
|
||||
public function wrap($start, $maybeString, $end = '')
|
||||
{
|
||||
return $maybeString ? ($start . $maybeString . $end) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Given array, print each item on its own line, wrapped in an
|
||||
* indented "{ }" block.
|
||||
*/
|
||||
public function block($array)
|
||||
{
|
||||
return $array && $this->length($array) ? $this->indent("{\n" . $this->join($array, "\n")) . "\n}" : '{}';
|
||||
}
|
||||
|
||||
public function indent($maybeString)
|
||||
{
|
||||
return $maybeString ? str_replace("\n", "\n ", $maybeString) : '';
|
||||
}
|
||||
|
||||
public function manyList($start, $list, $separator, $end)
|
||||
{
|
||||
return $this->length($list) === 0 ? null : ($start . $this->join($list, $separator) . $end);
|
||||
}
|
||||
|
||||
public function length($maybeArray)
|
||||
{
|
||||
return $maybeArray ? count($maybeArray) : 0;
|
||||
}
|
||||
|
||||
public function join($maybeArray, $separator = '')
|
||||
{
|
||||
return $maybeArray
|
||||
? implode(
|
||||
$separator,
|
||||
Utils::filter(
|
||||
$maybeArray,
|
||||
function($x) { return !!$x;}
|
||||
)
|
||||
)
|
||||
: '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Utils\Utils;
|
||||
|
||||
/**
|
||||
* Class Source
|
||||
* @package GraphQL\Language
|
||||
*/
|
||||
class Source
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $body;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $length;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var SourceLocation
|
||||
*/
|
||||
public $locationOffset;
|
||||
|
||||
/**
|
||||
* Source constructor.
|
||||
*
|
||||
* A representation of source input to GraphQL.
|
||||
* `name` and `locationOffset` are optional. They are useful for clients who
|
||||
* store GraphQL documents in source files; for example, if the GraphQL input
|
||||
* starts at line 40 in a file named Foo.graphql, it might be useful for name to
|
||||
* be "Foo.graphql" and location to be `{ line: 40, column: 0 }`.
|
||||
* line and column in locationOffset are 1-indexed
|
||||
*
|
||||
* @param $body
|
||||
* @param null $name
|
||||
* @param SourceLocation|null $location
|
||||
*/
|
||||
public function __construct($body, $name = null, SourceLocation $location = null)
|
||||
{
|
||||
Utils::invariant(
|
||||
is_string($body),
|
||||
'GraphQL query body is expected to be string, but got ' . Utils::getVariableType($body)
|
||||
);
|
||||
|
||||
$this->body = $body;
|
||||
$this->length = mb_strlen($body, 'UTF-8');
|
||||
$this->name = $name ?: 'GraphQL';
|
||||
$this->locationOffset = $location ?: new SourceLocation(1, 1);
|
||||
|
||||
Utils::invariant(
|
||||
$this->locationOffset->line > 0,
|
||||
'line in locationOffset is 1-indexed and must be positive'
|
||||
);
|
||||
Utils::invariant(
|
||||
$this->locationOffset->column > 0,
|
||||
'column in locationOffset is 1-indexed and must be positive'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $position
|
||||
* @return SourceLocation
|
||||
*/
|
||||
public function getLocation($position)
|
||||
{
|
||||
$line = 1;
|
||||
$column = $position + 1;
|
||||
|
||||
$utfChars = json_decode('"\u2028\u2029"');
|
||||
$lineRegexp = '/\r\n|[\n\r'.$utfChars.']/su';
|
||||
$matches = [];
|
||||
preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, PREG_OFFSET_CAPTURE);
|
||||
|
||||
foreach ($matches[0] as $index => $match) {
|
||||
$line += 1;
|
||||
$column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8'));
|
||||
}
|
||||
|
||||
return new SourceLocation($line, $column);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
class SourceLocation implements \JsonSerializable
|
||||
{
|
||||
public $line;
|
||||
public $column;
|
||||
|
||||
public function __construct($line, $col)
|
||||
{
|
||||
$this->line = $line;
|
||||
$this->column = $col;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'line' => $this->line,
|
||||
'column' => $this->column
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toSerializableArray()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource.
|
||||
* @since 5.4.0
|
||||
*/
|
||||
function jsonSerialize()
|
||||
{
|
||||
return $this->toSerializableArray();
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
/**
|
||||
* Represents a range of characters represented by a lexical token
|
||||
* within a Source.
|
||||
*/
|
||||
class Token
|
||||
{
|
||||
// Each kind of token.
|
||||
const SOF = '<SOF>';
|
||||
const EOF = '<EOF>';
|
||||
const BANG = '!';
|
||||
const DOLLAR = '$';
|
||||
const PAREN_L = '(';
|
||||
const PAREN_R = ')';
|
||||
const SPREAD = '...';
|
||||
const COLON = ':';
|
||||
const EQUALS = '=';
|
||||
const AT = '@';
|
||||
const BRACKET_L = '[';
|
||||
const BRACKET_R = ']';
|
||||
const BRACE_L = '{';
|
||||
const PIPE = '|';
|
||||
const BRACE_R = '}';
|
||||
const NAME = 'Name';
|
||||
const INT = 'Int';
|
||||
const FLOAT = 'Float';
|
||||
const STRING = 'String';
|
||||
const COMMENT = 'Comment';
|
||||
|
||||
/**
|
||||
* @param $kind
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getKindDescription($kind)
|
||||
{
|
||||
trigger_error('Deprecated as of 16.10.2016 ($kind itself contains description string now)', E_USER_DEPRECATED);
|
||||
|
||||
$description = [];
|
||||
$description[self::SOF] = '<SOF>';
|
||||
$description[self::EOF] = '<EOF>';
|
||||
$description[self::BANG] = '!';
|
||||
$description[self::DOLLAR] = '$';
|
||||
$description[self::PAREN_L] = '(';
|
||||
$description[self::PAREN_R] = ')';
|
||||
$description[self::SPREAD] = '...';
|
||||
$description[self::COLON] = ':';
|
||||
$description[self::EQUALS] = '=';
|
||||
$description[self::AT] = '@';
|
||||
$description[self::BRACKET_L] = '[';
|
||||
$description[self::BRACKET_R] = ']';
|
||||
$description[self::BRACE_L] = '{';
|
||||
$description[self::PIPE] = '|';
|
||||
$description[self::BRACE_R] = '}';
|
||||
$description[self::NAME] = 'Name';
|
||||
$description[self::INT] = 'Int';
|
||||
$description[self::FLOAT] = 'Float';
|
||||
$description[self::STRING] = 'String';
|
||||
$description[self::COMMENT] = 'Comment';
|
||||
|
||||
return $description[$kind];
|
||||
}
|
||||
|
||||
/**
|
||||
* The kind of Token (see one of constants above).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $kind;
|
||||
|
||||
/**
|
||||
* The character offset at which this Node begins.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $start;
|
||||
|
||||
/**
|
||||
* The character offset at which this Node ends.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $end;
|
||||
|
||||
/**
|
||||
* The 1-indexed line number on which this Token appears.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $line;
|
||||
|
||||
/**
|
||||
* The 1-indexed column number at which this Token begins.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $column;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* Tokens exist as nodes in a double-linked-list amongst all tokens
|
||||
* including ignored tokens. <SOF> is always the first node and <EOF>
|
||||
* the last.
|
||||
*
|
||||
* @var Token
|
||||
*/
|
||||
public $prev;
|
||||
|
||||
/**
|
||||
* @var Token
|
||||
*/
|
||||
public $next;
|
||||
|
||||
/**
|
||||
* Token constructor.
|
||||
* @param $kind
|
||||
* @param $start
|
||||
* @param $end
|
||||
* @param $line
|
||||
* @param $column
|
||||
* @param Token $previous
|
||||
* @param null $value
|
||||
*/
|
||||
public function __construct($kind, $start, $end, $line, $column, Token $previous = null, $value = null)
|
||||
{
|
||||
$this->kind = $kind;
|
||||
$this->start = (int) $start;
|
||||
$this->end = (int) $end;
|
||||
$this->line = (int) $line;
|
||||
$this->column = (int) $column;
|
||||
$this->prev = $previous;
|
||||
$this->next = null;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->kind . ($this->value ? ' "' . $this->value . '"' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'kind' => $this->kind,
|
||||
'value' => $this->value,
|
||||
'line' => $this->line,
|
||||
'column' => $this->column
|
||||
];
|
||||
}
|
||||
}
|
||||
+486
@@ -0,0 +1,486 @@
|
||||
<?php
|
||||
namespace GraphQL\Language;
|
||||
|
||||
use GraphQL\Language\AST\Node;
|
||||
use GraphQL\Language\AST\NodeKind;
|
||||
use GraphQL\Language\AST\NodeList;
|
||||
use GraphQL\Utils\TypeInfo;
|
||||
|
||||
class VisitorOperation
|
||||
{
|
||||
public $doBreak;
|
||||
|
||||
public $doContinue;
|
||||
|
||||
public $removeNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility for efficient AST traversal and modification.
|
||||
*
|
||||
* `visit()` will walk through an AST using a depth first traversal, calling
|
||||
* the visitor's enter function at each node in the traversal, and calling the
|
||||
* leave function after visiting that node and all of it's child nodes.
|
||||
*
|
||||
* By returning different values from the enter and leave functions, the
|
||||
* behavior of the visitor can be altered, including skipping over a sub-tree of
|
||||
* the AST (by returning false), editing the AST by returning a value or null
|
||||
* to remove the value, or to stop the whole traversal by returning BREAK.
|
||||
*
|
||||
* When using `visit()` to edit an AST, the original AST will not be modified, and
|
||||
* a new version of the AST with the changes applied will be returned from the
|
||||
* visit function.
|
||||
*
|
||||
* $editedAST = Visitor::visit($ast, [
|
||||
* 'enter' => function ($node, $key, $parent, $path, $ancestors) {
|
||||
* // return
|
||||
* // null: no action
|
||||
* // Visitor::skipNode(): skip visiting this node
|
||||
* // Visitor::stop(): stop visiting altogether
|
||||
* // Visitor::removeNode(): delete this node
|
||||
* // any value: replace this node with the returned value
|
||||
* },
|
||||
* 'leave' => function ($node, $key, $parent, $path, $ancestors) {
|
||||
* // return
|
||||
* // null: no action
|
||||
* // Visitor::stop(): stop visiting altogether
|
||||
* // Visitor::removeNode(): delete this node
|
||||
* // any value: replace this node with the returned value
|
||||
* }
|
||||
* ]);
|
||||
*
|
||||
* Alternatively to providing enter() and leave() functions, a visitor can
|
||||
* instead provide functions named the same as the [kinds of AST nodes](reference.md#graphqllanguageastnodekind),
|
||||
* or enter/leave visitors at a named key, leading to four permutations of
|
||||
* visitor API:
|
||||
*
|
||||
* 1) Named visitors triggered when entering a node a specific kind.
|
||||
*
|
||||
* Visitor::visit($ast, [
|
||||
* 'Kind' => function ($node) {
|
||||
* // enter the "Kind" node
|
||||
* }
|
||||
* ]);
|
||||
*
|
||||
* 2) Named visitors that trigger upon entering and leaving a node of
|
||||
* a specific kind.
|
||||
*
|
||||
* Visitor::visit($ast, [
|
||||
* 'Kind' => [
|
||||
* 'enter' => function ($node) {
|
||||
* // enter the "Kind" node
|
||||
* }
|
||||
* 'leave' => function ($node) {
|
||||
* // leave the "Kind" node
|
||||
* }
|
||||
* ]
|
||||
* ]);
|
||||
*
|
||||
* 3) Generic visitors that trigger upon entering and leaving any node.
|
||||
*
|
||||
* Visitor::visit($ast, [
|
||||
* 'enter' => function ($node) {
|
||||
* // enter any node
|
||||
* },
|
||||
* 'leave' => function ($node) {
|
||||
* // leave any node
|
||||
* }
|
||||
* ]);
|
||||
*
|
||||
* 4) Parallel visitors for entering and leaving nodes of a specific kind.
|
||||
*
|
||||
* Visitor::visit($ast, [
|
||||
* 'enter' => [
|
||||
* 'Kind' => function($node) {
|
||||
* // enter the "Kind" node
|
||||
* }
|
||||
* },
|
||||
* 'leave' => [
|
||||
* 'Kind' => function ($node) {
|
||||
* // leave the "Kind" node
|
||||
* }
|
||||
* ]
|
||||
* ]);
|
||||
*/
|
||||
class Visitor
|
||||
{
|
||||
public static $visitorKeys = [
|
||||
NodeKind::NAME => [],
|
||||
NodeKind::DOCUMENT => ['definitions'],
|
||||
NodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'],
|
||||
NodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'],
|
||||
NodeKind::VARIABLE => ['name'],
|
||||
NodeKind::SELECTION_SET => ['selections'],
|
||||
NodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'],
|
||||
NodeKind::ARGUMENT => ['name', 'value'],
|
||||
NodeKind::FRAGMENT_SPREAD => ['name', 'directives'],
|
||||
NodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'],
|
||||
NodeKind::FRAGMENT_DEFINITION => ['name', 'typeCondition', 'directives', 'selectionSet'],
|
||||
|
||||
NodeKind::INT => [],
|
||||
NodeKind::FLOAT => [],
|
||||
NodeKind::STRING => [],
|
||||
NodeKind::BOOLEAN => [],
|
||||
NodeKind::NULL => [],
|
||||
NodeKind::ENUM => [],
|
||||
NodeKind::LST => ['values'],
|
||||
NodeKind::OBJECT => ['fields'],
|
||||
NodeKind::OBJECT_FIELD => ['name', 'value'],
|
||||
NodeKind::DIRECTIVE => ['name', 'arguments'],
|
||||
NodeKind::NAMED_TYPE => ['name'],
|
||||
NodeKind::LIST_TYPE => ['type'],
|
||||
NodeKind::NON_NULL_TYPE => ['type'],
|
||||
|
||||
NodeKind::SCHEMA_DEFINITION => ['directives', 'operationTypes'],
|
||||
NodeKind::OPERATION_TYPE_DEFINITION => ['type'],
|
||||
NodeKind::SCALAR_TYPE_DEFINITION => ['name', 'directives'],
|
||||
NodeKind::OBJECT_TYPE_DEFINITION => ['name', 'interfaces', 'directives', 'fields'],
|
||||
NodeKind::FIELD_DEFINITION => ['name', 'arguments', 'type', 'directives'],
|
||||
NodeKind::INPUT_VALUE_DEFINITION => ['name', 'type', 'defaultValue', 'directives'],
|
||||
NodeKind::INTERFACE_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ],
|
||||
NodeKind::UNION_TYPE_DEFINITION => [ 'name', 'directives', 'types' ],
|
||||
NodeKind::ENUM_TYPE_DEFINITION => [ 'name', 'directives', 'values' ],
|
||||
NodeKind::ENUM_VALUE_DEFINITION => [ 'name', 'directives' ],
|
||||
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ],
|
||||
NodeKind::TYPE_EXTENSION_DEFINITION => [ 'definition' ],
|
||||
NodeKind::DIRECTIVE_DEFINITION => [ 'name', 'arguments', 'locations' ]
|
||||
];
|
||||
|
||||
/**
|
||||
* Visit the AST (see class description for details)
|
||||
*
|
||||
* @api
|
||||
* @param Node $root
|
||||
* @param array $visitor
|
||||
* @param array $keyMap
|
||||
* @return Node|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function visit($root, $visitor, $keyMap = null)
|
||||
{
|
||||
$visitorKeys = $keyMap ?: self::$visitorKeys;
|
||||
|
||||
$stack = null;
|
||||
$inArray = $root instanceof NodeList || is_array($root);
|
||||
$keys = [$root];
|
||||
$index = -1;
|
||||
$edits = [];
|
||||
$parent = null;
|
||||
$path = [];
|
||||
$ancestors = [];
|
||||
$newRoot = $root;
|
||||
|
||||
$UNDEFINED = null;
|
||||
|
||||
do {
|
||||
$index++;
|
||||
$isLeaving = $index === count($keys);
|
||||
$key = null;
|
||||
$node = null;
|
||||
$isEdited = $isLeaving && count($edits) !== 0;
|
||||
|
||||
if ($isLeaving) {
|
||||
$key = count($ancestors) === 0 ? $UNDEFINED : array_pop($path);
|
||||
$node = $parent;
|
||||
$parent = array_pop($ancestors);
|
||||
|
||||
if ($isEdited) {
|
||||
if ($inArray) {
|
||||
// $node = $node; // arrays are value types in PHP
|
||||
if ($node instanceof NodeList) {
|
||||
$node = clone $node;
|
||||
}
|
||||
} else {
|
||||
$node = clone $node;
|
||||
}
|
||||
$editOffset = 0;
|
||||
for ($ii = 0; $ii < count($edits); $ii++) {
|
||||
$editKey = $edits[$ii][0];
|
||||
$editValue = $edits[$ii][1];
|
||||
|
||||
if ($inArray) {
|
||||
$editKey -= $editOffset;
|
||||
}
|
||||
if ($inArray && $editValue === null) {
|
||||
if ($node instanceof NodeList) {
|
||||
$node->splice($editKey, 1);
|
||||
} else {
|
||||
array_splice($node, $editKey, 1);
|
||||
}
|
||||
$editOffset++;
|
||||
} else {
|
||||
if ($node instanceof NodeList || is_array($node)) {
|
||||
$node[$editKey] = $editValue;
|
||||
} else {
|
||||
$node->{$editKey} = $editValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$index = $stack['index'];
|
||||
$keys = $stack['keys'];
|
||||
$edits = $stack['edits'];
|
||||
$inArray = $stack['inArray'];
|
||||
$stack = $stack['prev'];
|
||||
} else {
|
||||
$key = $parent ? ($inArray ? $index : $keys[$index]) : $UNDEFINED;
|
||||
$node = $parent ? (($parent instanceof NodeList || is_array($parent)) ? $parent[$key] : $parent->{$key}) : $newRoot;
|
||||
if ($node === null || $node === $UNDEFINED) {
|
||||
continue;
|
||||
}
|
||||
if ($parent) {
|
||||
$path[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
$result = null;
|
||||
if (!$node instanceof NodeList && !is_array($node)) {
|
||||
if (!($node instanceof Node)) {
|
||||
throw new \Exception('Invalid AST Node: ' . json_encode($node));
|
||||
}
|
||||
|
||||
$visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving);
|
||||
|
||||
if ($visitFn) {
|
||||
$result = call_user_func($visitFn, $node, $key, $parent, $path, $ancestors);
|
||||
|
||||
if ($result !== null) {
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doBreak) {
|
||||
break;
|
||||
}
|
||||
if (!$isLeaving && $result->doContinue) {
|
||||
array_pop($path);
|
||||
continue;
|
||||
}
|
||||
if ($result->removeNode) {
|
||||
$editValue = null;
|
||||
}
|
||||
} else {
|
||||
$editValue = $result;
|
||||
}
|
||||
|
||||
$edits[] = [$key, $editValue];
|
||||
if (!$isLeaving) {
|
||||
if ($editValue instanceof Node) {
|
||||
$node = $editValue;
|
||||
} else {
|
||||
array_pop($path);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($result === null && $isEdited) {
|
||||
$edits[] = [$key, $node];
|
||||
}
|
||||
|
||||
if (!$isLeaving) {
|
||||
$stack = [
|
||||
'inArray' => $inArray,
|
||||
'index' => $index,
|
||||
'keys' => $keys,
|
||||
'edits' => $edits,
|
||||
'prev' => $stack
|
||||
];
|
||||
$inArray = $node instanceof NodeList || is_array($node);
|
||||
|
||||
$keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
|
||||
$index = -1;
|
||||
$edits = [];
|
||||
if ($parent) {
|
||||
$ancestors[] = $parent;
|
||||
}
|
||||
$parent = $node;
|
||||
}
|
||||
|
||||
} while ($stack);
|
||||
|
||||
if (count($edits) !== 0) {
|
||||
$newRoot = $edits[0][1];
|
||||
}
|
||||
|
||||
return $newRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns marker for visitor break
|
||||
*
|
||||
* @api
|
||||
* @return VisitorOperation
|
||||
*/
|
||||
public static function stop()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r->doBreak = true;
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns marker for skipping current node
|
||||
*
|
||||
* @api
|
||||
* @return VisitorOperation
|
||||
*/
|
||||
public static function skipNode()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r->doContinue = true;
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns marker for removing a node
|
||||
*
|
||||
* @api
|
||||
* @return VisitorOperation
|
||||
*/
|
||||
public static function removeNode()
|
||||
{
|
||||
$r = new VisitorOperation();
|
||||
$r->removeNode = true;
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $visitors
|
||||
* @return array
|
||||
*/
|
||||
static function visitInParallel($visitors)
|
||||
{
|
||||
$visitorsCount = count($visitors);
|
||||
$skipping = new \SplFixedArray($visitorsCount);
|
||||
|
||||
return [
|
||||
'enter' => function ($node) use ($visitors, $skipping, $visitorsCount) {
|
||||
for ($i = 0; $i < $visitorsCount; $i++) {
|
||||
if (empty($skipping[$i])) {
|
||||
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ false);
|
||||
|
||||
if ($fn) {
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doContinue) {
|
||||
$skipping[$i] = $node;
|
||||
} else if ($result->doBreak) {
|
||||
$skipping[$i] = $result;
|
||||
} else if ($result->removeNode) {
|
||||
return $result;
|
||||
}
|
||||
} else if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'leave' => function ($node) use ($visitors, $skipping, $visitorsCount) {
|
||||
for ($i = 0; $i < $visitorsCount; $i++) {
|
||||
if (empty($skipping[$i])) {
|
||||
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ true);
|
||||
|
||||
if ($fn) {
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
if ($result instanceof VisitorOperation) {
|
||||
if ($result->doBreak) {
|
||||
$skipping[$i] = $result;
|
||||
} else if ($result->removeNode) {
|
||||
return $result;
|
||||
}
|
||||
} else if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
} else if ($skipping[$i] === $node) {
|
||||
$skipping[$i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new visitor instance which maintains a provided TypeInfo instance
|
||||
* along with visiting visitor.
|
||||
*/
|
||||
static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor)
|
||||
{
|
||||
return [
|
||||
'enter' => function ($node) use ($typeInfo, $visitor) {
|
||||
$typeInfo->enter($node);
|
||||
$fn = self::getVisitFn($visitor, $node->kind, false);
|
||||
|
||||
if ($fn) {
|
||||
$result = call_user_func_array($fn, func_get_args());
|
||||
if ($result) {
|
||||
$typeInfo->leave($node);
|
||||
if ($result instanceof Node) {
|
||||
$typeInfo->enter($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
'leave' => function ($node) use ($typeInfo, $visitor) {
|
||||
$fn = self::getVisitFn($visitor, $node->kind, true);
|
||||
$result = $fn ? call_user_func_array($fn, func_get_args()) : null;
|
||||
$typeInfo->leave($node);
|
||||
return $result;
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $visitor
|
||||
* @param $kind
|
||||
* @param $isLeaving
|
||||
* @return null
|
||||
*/
|
||||
public static function getVisitFn($visitor, $kind, $isLeaving)
|
||||
{
|
||||
if (!$visitor) {
|
||||
return null;
|
||||
}
|
||||
$kindVisitor = isset($visitor[$kind]) ? $visitor[$kind] : null;
|
||||
|
||||
if (!$isLeaving && is_callable($kindVisitor)) {
|
||||
// { Kind() {} }
|
||||
return $kindVisitor;
|
||||
}
|
||||
|
||||
if (is_array($kindVisitor)) {
|
||||
if ($isLeaving) {
|
||||
$kindSpecificVisitor = isset($kindVisitor['leave']) ? $kindVisitor['leave'] : null;
|
||||
} else {
|
||||
$kindSpecificVisitor = isset($kindVisitor['enter']) ? $kindVisitor['enter'] : null;
|
||||
}
|
||||
|
||||
if ($kindSpecificVisitor && is_callable($kindSpecificVisitor)) {
|
||||
// { Kind: { enter() {}, leave() {} } }
|
||||
return $kindSpecificVisitor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
$visitor += ['leave' => null, 'enter' => null];
|
||||
$specificVisitor = $isLeaving ? $visitor['leave'] : $visitor['enter'];
|
||||
|
||||
if ($specificVisitor) {
|
||||
if (is_callable($specificVisitor)) {
|
||||
// { enter() {}, leave() {} }
|
||||
return $specificVisitor;
|
||||
}
|
||||
$specificKindVisitor = isset($specificVisitor[$kind]) ? $specificVisitor[$kind] : null;
|
||||
|
||||
if (is_callable($specificKindVisitor)) {
|
||||
// { enter: { Kind() {} }, leave: { Kind() {} } }
|
||||
return $specificKindVisitor;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user