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
|
2 |
|
public function __construct(Parser $parser, string $name, $type = null) |
45
|
|
|
{ |
46
|
2 |
|
parent::__construct($parser, '', $type); |
47
|
2 |
|
$this->collection = new GeneratedCollection(); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
public function generate(): GeneratedCollection |
51
|
|
|
{ |
52
|
2 |
|
foreach ($this->type->getFields() as $field) { |
53
|
1 |
|
$directives = $field->astNode->directives; |
54
|
1 |
|
$this->processFieldDirectives($field, $directives, 'Policy'); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
58
|
|
|
|
59
|
2 |
|
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
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
2 |
|
return $this->collection; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function getPolicyClass(string $name): ClassType |
77
|
|
|
{ |
78
|
|
|
if (array_key_exists($name, $this->policyClasses)) { |
79
|
|
|
return $this->policyClasses[$name]; |
80
|
1 |
|
} |
81
|
1 |
|
|
82
|
1 |
|
/** |
83
|
1 |
|
* @var ClassType $class |
84
|
1 |
|
*/ |
85
|
|
|
$class = new ClassType($name . 'Policy'); |
86
|
1 |
|
$class |
87
|
1 |
|
->addComment("This file was automatically generated by Modelarium.") |
88
|
|
|
->setTraits(['Illuminate\Auth\Access\HandlesAuthorization']); |
89
|
|
|
$this->policyClasses[$name] = $class; |
90
|
|
|
return $class; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getGenerateFilename(string $name): string |
94
|
|
|
{ |
95
|
1 |
|
return $this->getBasePath('app/Policies/'. Str::studly($name) . 'Policy.php'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|