|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of laravel.su package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace App\GraphQL\Feature\Kernel; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
use GraphQL\Type\Definition\Type; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class FeaturesSupport |
|
17
|
|
|
* @package App\GraphQL\Feature\Kernel |
|
18
|
|
|
*/ |
|
19
|
|
|
trait FeaturesSupport |
|
20
|
|
|
{ |
|
21
|
|
|
use AttributeExists; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $args = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|\Closure[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $fieldsWrapper = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array|\Closure[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $queries = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param Type $type |
|
41
|
|
|
* @param string|null $description |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function addArgument(string $name, Type $type, string $description = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->args[$name] = [ |
|
47
|
|
|
'type' => $type, |
|
48
|
|
|
'description' => $description ?? Str::ucfirst($name) . ' field of ' . get_class($this), |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \Closure $wrapper |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function addFieldsWrapper(\Closure $wrapper) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->fieldsWrapper[] = $wrapper; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \Closure $resolveQuery |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addQuery(\Closure $resolveQuery) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->queries[] = $resolveQuery; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $argument |
|
78
|
|
|
* @param \Closure $resolveQuery |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addQueryFor(string $argument, \Closure $resolveQuery) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->queries[] = function(Builder $builder, array $args = []) use ($argument, $resolveQuery) { |
|
84
|
|
|
return $this->whenExists($args, $argument, function ($value) use ($builder, $resolveQuery) { |
|
85
|
|
|
return $resolveQuery($builder, $value); |
|
86
|
|
|
}) ?? $builder; |
|
87
|
|
|
}; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function boot(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->bootTraits(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function bootTraits(): void |
|
104
|
|
|
{ |
|
105
|
|
|
foreach (class_uses_recursive(static::class) as $trait) { |
|
106
|
|
|
if (method_exists($this, $method = 'boot' . class_basename($trait))) { |
|
107
|
|
|
call_user_func([$this, $method]); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $arguments |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function extendArguments(array $arguments = []): array |
|
117
|
|
|
{ |
|
118
|
|
|
return array_merge($arguments, $this->args); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array $fields |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function extendFields(array $fields = []): array |
|
126
|
|
|
{ |
|
127
|
|
|
foreach ($this->fieldsWrapper as $wrapper) { |
|
128
|
|
|
$fields = $wrapper($fields); |
|
129
|
|
|
|
|
130
|
|
|
if ($fields instanceof \Iterator) { |
|
131
|
|
|
$fields = iterator_to_array($fields); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $fields; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param Builder $builder |
|
140
|
|
|
* @param array $args |
|
141
|
|
|
* @return Builder|mixed |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function extendQuery(Builder $builder, array $args) |
|
144
|
|
|
{ |
|
145
|
|
|
foreach ($this->queries as $query) { |
|
146
|
|
|
$builder = $query($builder, $args); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $builder; |
|
150
|
|
|
} |
|
151
|
|
|
} |