1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Directives; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Modelarium\Laravel\Targets\ModelGenerator; |
7
|
|
|
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface; |
8
|
|
|
|
9
|
|
|
class LaravelMediaLibraryDataDirective implements ModelDirectiveInterface |
10
|
|
|
{ |
11
|
|
|
public static function processModelTypeDirective( |
12
|
|
|
ModelGenerator $generator, |
13
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
14
|
|
|
): void { |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function processModelFieldDirective( |
18
|
|
|
ModelGenerator $generator, |
19
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
20
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
21
|
|
|
): void { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function processModelRelationshipDirective( |
25
|
|
|
ModelGenerator $generator, |
26
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
27
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
28
|
|
|
): void { |
29
|
|
|
$collection = 'images'; |
30
|
|
|
$customFields = []; |
31
|
|
|
$studlyFieldName = Str::studly($field->name); |
32
|
|
|
|
33
|
|
|
// deps |
34
|
|
|
if (!in_array('\\Spatie\\MediaLibrary\\HasMedia', $generator->class->getImplements())) { |
35
|
|
|
$generator->class->addImplement('\\Spatie\\MediaLibrary\\HasMedia'); |
36
|
|
|
$generator->class->addTrait('\\Spatie\\MediaLibrary\\InteractsWithMedia'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// args |
40
|
|
|
foreach ($directive->arguments as $arg) { |
41
|
|
|
/** |
42
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
switch ($arg->name->value) { |
46
|
|
|
case 'collection': |
47
|
|
|
/** @phpstan-ignore-next-line */ |
48
|
|
|
$collection = $arg->value->value; |
|
|
|
|
49
|
|
|
break; |
50
|
|
|
case 'fields': |
51
|
|
|
/** @phpstan-ignore-next-line */ |
52
|
|
|
foreach ($arg->value->values as $item) { |
|
|
|
|
53
|
|
|
$customFields[] = $item->value; |
54
|
|
|
} |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$studlyCollection = Str::studly($collection); |
59
|
|
|
|
60
|
|
|
// registration |
61
|
|
|
if (!$generator->class->hasMethod("registerMediaCollections")) { |
62
|
|
|
$registerMediaCollections = $generator->class->addMethod("registerMediaCollections") |
63
|
|
|
->setPublic() |
64
|
|
|
->setReturnType('void') |
65
|
|
|
->addComment("Configures Laravel media-library"); |
66
|
|
|
} else { |
67
|
|
|
$registerMediaCollections = $generator->class->getMethod("registerMediaCollections"); |
68
|
|
|
} |
69
|
|
|
$registerMediaCollections->addBody("\$generator->addMediaCollection(?);\n", [$collection]); |
70
|
|
|
|
71
|
|
|
// all image models for this collection |
72
|
|
|
$generator->class->addMethod("getMedia{$studlyCollection}Collection") |
73
|
|
|
->setPublic() |
74
|
|
|
->setReturnType('\\Spatie\\MediaLibrary\\MediaCollections\\Models\\Collections\\MediaCollection') |
75
|
|
|
->addComment("Returns a collection media from Laravel-MediaLibrary") |
76
|
|
|
->setBody("return \$generator->getMedia(?);", [$collection]); |
77
|
|
|
|
78
|
|
|
// custom fields |
79
|
|
|
$generator->class->addMethod("getMedia{$studlyCollection}CustomFields") |
80
|
|
|
->setPublic() |
81
|
|
|
->setReturnType('array') |
82
|
|
|
->addComment("Returns custom fields for the media") |
83
|
|
|
->setBody("return ?;", [$customFields]); |
84
|
|
|
|
85
|
|
|
$generator->class->addMethod("get{$studlyFieldName}urlAttribute") |
86
|
|
|
->setPublic() |
87
|
|
|
->setReturnType('string') |
88
|
|
|
->addComment("Returns the media attribute (url) for the $collection") |
89
|
|
|
->setBody( /** @lang PHP */ |
90
|
|
|
<<< PHP |
91
|
|
|
\$image = \$generator->getMedia{$studlyCollection}Collection()->first(); |
92
|
|
|
if (\$image) { |
93
|
|
|
return \$image->getUrl(); |
94
|
|
|
} |
95
|
|
|
return ''; |
96
|
|
|
PHP |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
// all image models for this collection |
100
|
|
|
$generator->class->addMethod("get{$studlyFieldName}Attribute") |
101
|
|
|
->setPublic() |
102
|
|
|
->setReturnType('array') |
103
|
|
|
->addComment("Returns media attribute for the $collection media with custom fields") |
104
|
|
|
->setBody( /** @lang PHP */ |
105
|
|
|
<<< PHP |
106
|
|
|
\$image = \$generator->getMedia{$studlyCollection}Collection()->first(); |
107
|
|
|
if (\$image) { |
108
|
|
|
\$customFields = []; |
109
|
|
|
foreach (\$generator->getMedia{$studlyCollection}CustomFields() as \$c) { |
110
|
|
|
\$customFields[\$c] = \$image->getCustomProperty(\$c); |
111
|
|
|
} |
112
|
|
|
return [ |
113
|
|
|
'url' => \$image->getUrl(), |
114
|
|
|
'fields' => json_encode(\$customFields) |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
return []; |
118
|
|
|
PHP |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|