1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraComponents\GraphQL\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
|
7
|
|
|
class TypeMakeCommand extends GeneratorCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The console command signature. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'make:graphql:type {name} {--type= : Create a specific type.}'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Create a new GraphQL type class'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The type of class being generated. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type = 'Type'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The type to create. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $graphqlType = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The list of type and namespaces. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $graphqlTypes = [ |
43
|
|
|
'AbstractType' => 'Youshido\GraphQL\Type\AbstractType', |
44
|
|
|
'AbstractScalarType' => 'Youshido\GraphQL\Type\Scalar\AbstractScalarType', |
45
|
|
|
'AbstractObjectType' => 'Youshido\GraphQL\Type\Object\AbstractObjectType', |
46
|
|
|
'AbstractMutationObjectType' => 'Youshido\GraphQL\Type\Object\AbstractMutationObjectType', |
47
|
|
|
'AbstractInputObjectType' => 'Youshido\GraphQL\Type\InputObject\AbstractInputObjectType', |
48
|
|
|
'AbstractInterfaceType' => 'Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType', |
49
|
|
|
'AbstractEnumType' => 'Youshido\GraphQL\Type\Enum\AbstractEnumType', |
50
|
|
|
'AbstractListType' => 'Youshido\GraphQL\Type\ListType\AbstractListType', |
51
|
|
|
'AbstractUnionType' => 'Youshido\GraphQL\Type\Union\AbstractUnionType', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build the class with the given name. |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function buildClass($name) |
61
|
|
|
{ |
62
|
|
|
$this->determineWhatShouldBeCreated(); |
63
|
|
|
|
64
|
|
|
$stub = parent::buildClass($name); |
65
|
|
|
$stub = $this->replaceDummy($stub, $this->graphqlType, 'DummyType'); |
66
|
|
|
|
67
|
|
|
return $this->replaceDummy($stub, $this->graphqlTypes[$this->graphqlType], 'DummyUseType'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the stub file for the generator. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getStub() |
76
|
|
|
{ |
77
|
|
|
return __DIR__.'/stubs/Type.stub'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the default namespace for the class. |
82
|
|
|
* |
83
|
|
|
* @param string $rootNamespace |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getDefaultNamespace($rootNamespace) |
87
|
|
|
{ |
88
|
|
|
return $rootNamespace.'\GraphQL\Types'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determine the type to create. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function determineWhatShouldBeCreated() |
97
|
|
|
{ |
98
|
|
|
$optionType = (string) $this->option('type'); |
99
|
|
|
|
100
|
|
|
if (array_key_exists($optionType, $this->graphqlTypes)) { |
101
|
|
|
$this->graphqlType = $optionType; |
102
|
|
|
|
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->promptForType(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Prompt for which type to create. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function promptForType() |
115
|
|
|
{ |
116
|
|
|
$choice = $this->choice( |
117
|
|
|
'Which type would you like to create?', |
118
|
|
|
array_map(function ($type) { |
119
|
|
|
return '<comment>'.$type.'</comment>'; |
120
|
|
|
}, array_keys($this->graphqlTypes)) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->graphqlType = strip_tags($choice); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Replace the extends type for the given stub. |
128
|
|
|
* |
129
|
|
|
* @param string $stub |
130
|
|
|
* @param string $name |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
protected function replaceDummy($stub, $name, $dummy) |
134
|
|
|
{ |
135
|
|
|
return str_replace($dummy, $name, $stub); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|