|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
6
|
|
|
use GraphQL\Type\Definition\Type; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Modelarium\BaseGenerator; |
|
9
|
|
|
use Modelarium\GeneratedCollection; |
|
10
|
|
|
use Modelarium\GeneratedItem; |
|
11
|
|
|
use Modelarium\Parser; |
|
12
|
|
|
use Nette\PhpGenerator\ClassType; |
|
13
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
|
14
|
|
|
|
|
15
|
|
|
class PolicyGenerator extends BaseGenerator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectType |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $type = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var GeneratedCollection |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $collection = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var ClassType[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $policyClasses = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Parser $parser |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
* @param Type|string $type |
|
42
|
|
|
* @phpstan-ignore-next-line |
|
43
|
|
|
*/ |
|
44
|
9 |
|
public function __construct(Parser $parser, string $name, $type = null) |
|
45
|
|
|
{ |
|
46
|
9 |
|
parent::__construct($parser, '', $type); |
|
47
|
9 |
|
$this->collection = new GeneratedCollection(); |
|
48
|
9 |
|
} |
|
49
|
|
|
|
|
50
|
9 |
|
public function generate(): GeneratedCollection |
|
51
|
|
|
{ |
|
52
|
9 |
|
foreach ($this->type->getFields() as $field) { |
|
53
|
1 |
|
$directives = $field->astNode->directives; |
|
54
|
1 |
|
$this->processFieldDirectives($field, $directives, 'Policy'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
9 |
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
|
58
|
|
|
|
|
59
|
9 |
|
foreach ($this->policyClasses as $name => $c) { |
|
60
|
1 |
|
$namespace = new PhpNamespace('App\\Policies'); |
|
61
|
1 |
|
$namespace->addUse('App\\Models\\User'); |
|
62
|
1 |
|
$namespace->addUse('App\\Models\\' . $name); |
|
63
|
1 |
|
$namespace->add($c); |
|
64
|
|
|
|
|
65
|
1 |
|
$this->collection->push( |
|
66
|
1 |
|
new GeneratedItem( |
|
67
|
1 |
|
GeneratedItem::TYPE_POLICY, |
|
68
|
1 |
|
"<?php declare(strict_types=1);\n\n" . $printer->printNamespace($namespace), |
|
69
|
1 |
|
$this->getGenerateFilename($name), |
|
70
|
1 |
|
true |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
9 |
|
return $this->collection; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function getPolicyClass(string $name): ClassType |
|
78
|
|
|
{ |
|
79
|
1 |
|
if (array_key_exists($name, $this->policyClasses)) { |
|
80
|
1 |
|
return $this->policyClasses[$name]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var ClassType $class |
|
85
|
|
|
*/ |
|
86
|
1 |
|
$class = new ClassType($name . 'Policy'); |
|
87
|
|
|
$class |
|
88
|
1 |
|
->addComment("This file was automatically generated by Modelarium.") |
|
89
|
1 |
|
->setTraits(['Illuminate\Auth\Access\HandlesAuthorization']); |
|
90
|
1 |
|
$this->policyClasses[$name] = $class; |
|
91
|
1 |
|
return $class; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function getGenerateFilename(string $name): string |
|
95
|
|
|
{ |
|
96
|
1 |
|
return $this->getBasePath('app/Policies/'. Str::studly($name) . 'Policy.php'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|