|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
|
6
|
|
|
use GraphQL\Type\Definition\NonNull; |
|
7
|
|
|
use Modelarium\Laravel\Targets\PolicyGenerator; |
|
8
|
|
|
use Modelarium\Laravel\Targets\Interfaces\PolicyDirectiveInterface; |
|
9
|
|
|
|
|
10
|
|
|
class CanDirective implements PolicyDirectiveInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public static function processPolicyTypeDirective( |
|
13
|
|
|
PolicyGenerator $generator, |
|
|
|
|
|
|
14
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
|
|
|
|
|
15
|
|
|
): void { |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
1 |
|
public static function processPolicyFieldDirective( |
|
19
|
|
|
PolicyGenerator $generator, |
|
20
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
21
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
22
|
|
|
): void { |
|
23
|
1 |
|
$ability = ''; |
|
24
|
1 |
|
$find = ''; |
|
25
|
1 |
|
$injected = false; |
|
26
|
1 |
|
$args = false; |
|
27
|
|
|
|
|
28
|
1 |
|
if ($field->type instanceof NonNull) { |
|
29
|
|
|
$type = $field->type->getWrappedType(); |
|
30
|
|
|
} else { |
|
31
|
1 |
|
$type = $field->type; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var DirectiveNode $directive |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
1 |
|
$model = $type->name; /** @phpstan-ignore-line */ |
|
39
|
|
|
|
|
40
|
1 |
|
foreach ($directive->arguments as $arg) { |
|
41
|
1 |
|
switch ($arg->name->value) { |
|
42
|
1 |
|
case 'ability': |
|
43
|
|
|
// @phpstan-ignore-next-line |
|
44
|
1 |
|
$ability = $arg->value->value; |
|
|
|
|
|
|
45
|
1 |
|
break; |
|
46
|
1 |
|
case 'find': |
|
47
|
|
|
// @phpstan-ignore-next-line |
|
48
|
1 |
|
$find = $arg->value->value; |
|
49
|
1 |
|
break; |
|
50
|
1 |
|
case 'model': |
|
51
|
|
|
// @phpstan-ignore-next-line |
|
52
|
|
|
$model = $arg->value->value; |
|
53
|
|
|
break; |
|
54
|
1 |
|
case 'injectArgs': |
|
55
|
1 |
|
$injected = true; |
|
56
|
1 |
|
break; |
|
57
|
1 |
|
case 'args': |
|
58
|
1 |
|
$args = true; |
|
59
|
1 |
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
list($namespace, $modelClassName, $relativePath) = $generator->splitClassName($model); |
|
64
|
|
|
|
|
65
|
1 |
|
$class = $generator->getPolicyClass($modelClassName); |
|
66
|
|
|
|
|
67
|
1 |
|
$method = $class->addMethod($ability); |
|
68
|
1 |
|
$method->setPublic() |
|
69
|
1 |
|
->setReturnType('bool') |
|
70
|
1 |
|
->addBody( |
|
71
|
1 |
|
'return false;' |
|
72
|
|
|
); |
|
73
|
1 |
|
$method->addParameter('user')->setType('\\App\\Models\\User')->setNullable(true); |
|
74
|
|
|
|
|
75
|
1 |
|
if ($find) { |
|
76
|
1 |
|
$method->addParameter('model')->setType('\\App\\Models\\' . $modelClassName); |
|
77
|
|
|
} |
|
78
|
1 |
|
if ($injected) { |
|
79
|
1 |
|
$method->addParameter('injectedArgs')->setType('array'); |
|
80
|
|
|
} |
|
81
|
1 |
|
if ($args) { |
|
82
|
1 |
|
$method->addParameter('staticArgs')->setType('array'); |
|
83
|
|
|
} |
|
84
|
1 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.