|
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 static function processModelRelationshipDirective( |
|
25
|
|
|
ModelGenerator $generator, |
|
26
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
27
|
|
|
\GraphQL\Language\AST\DirectiveNode $directive |
|
28
|
|
|
): string { |
|
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
|
|
|
$conversion = ''; |
|
40
|
|
|
$width = 200; |
|
41
|
|
|
$height = 200; |
|
42
|
|
|
$responsive = false; |
|
43
|
|
|
|
|
44
|
|
|
// args |
|
45
|
|
|
foreach ($directive->arguments as $arg) { |
|
46
|
|
|
/** |
|
47
|
|
|
* @var \GraphQL\Language\AST\ArgumentNode $arg |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
switch ($arg->name->value) { |
|
51
|
|
|
case 'collection': |
|
52
|
|
|
/** @phpstan-ignore-next-line */ |
|
53
|
|
|
$collection = $arg->value->value; |
|
|
|
|
|
|
54
|
|
|
break; |
|
55
|
|
|
case 'fields': |
|
56
|
|
|
/** @phpstan-ignore-next-line */ |
|
57
|
|
|
foreach ($arg->value->values as $item) { |
|
|
|
|
|
|
58
|
|
|
$customFields[] = $item->value; |
|
59
|
|
|
} |
|
60
|
|
|
break; |
|
61
|
|
|
case 'conversion': |
|
62
|
|
|
$conversion = $arg->value->value; |
|
63
|
|
|
break; |
|
64
|
|
|
case 'width': |
|
65
|
|
|
$width = $arg->value->value; |
|
66
|
|
|
break; |
|
67
|
|
|
case 'height': |
|
68
|
|
|
$height = $arg->value->value; |
|
69
|
|
|
break; |
|
70
|
|
|
case 'responsive': |
|
71
|
|
|
$responsive = $arg->value->value; |
|
72
|
|
|
break; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
$studlyCollection = Str::studly($collection); |
|
76
|
|
|
|
|
77
|
|
|
// registration |
|
78
|
|
|
if (!$generator->class->hasMethod("registerMediaCollections")) { |
|
79
|
|
|
$registerMediaCollections = $generator->class->addMethod("registerMediaCollections") |
|
80
|
|
|
->setPublic() |
|
81
|
|
|
->setReturnType('void') |
|
82
|
|
|
->addComment("Configures Laravel media-library"); |
|
83
|
|
|
} else { |
|
84
|
|
|
$registerMediaCollections = $generator->class->getMethod("registerMediaCollections"); |
|
85
|
|
|
} |
|
86
|
|
|
$registerMediaCollections->addBody("\$this->addMediaCollection(?);\n", [$collection]); |
|
87
|
|
|
|
|
88
|
|
|
// conversion |
|
89
|
|
|
if ($conversion) { |
|
90
|
|
|
if (!$generator->class->hasMethod("registerMediaConversions")) { |
|
91
|
|
|
$registerMediaConversions = $generator->class->addMethod("registerMediaConversions") |
|
92
|
|
|
->setPublic() |
|
93
|
|
|
->setReturnType('void') |
|
94
|
|
|
->addComment("Configures Laravel media-library conversions"); |
|
95
|
|
|
$registerMediaConversions->addParameter('media') |
|
96
|
|
|
->setDefaultValue(null) |
|
97
|
|
|
->setType('\\Spatie\\MediaLibrary\\MediaCollections\\Models\\Media') |
|
98
|
|
|
->setNullable(true); |
|
99
|
|
|
} else { |
|
100
|
|
|
$registerMediaConversions = $generator->class->getMethod("registerMediaConversions"); |
|
101
|
|
|
} |
|
102
|
|
|
$registerMediaConversions->addBody( |
|
103
|
|
|
"\$this->addMediaConversions(?)" . |
|
104
|
|
|
($width ? '->width(?)' : '') . |
|
105
|
|
|
($height ? '->height(?)' : '') . |
|
106
|
|
|
($responsive ? '->withResponsiveImages()' : '') . |
|
107
|
|
|
";\n", |
|
108
|
|
|
array_merge([$conversion], ($width ? [$width] : []), ($height ? [$height] : [])) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// all image models for this collection |
|
113
|
|
|
$generator->class->addMethod("getMedia{$studlyCollection}Collection") |
|
114
|
|
|
->setPublic() |
|
115
|
|
|
->setReturnType('\\Spatie\\MediaLibrary\\MediaCollections\\Models\\Collections\\MediaCollection') |
|
116
|
|
|
->addComment("Returns a collection media from Laravel-MediaLibrary") |
|
117
|
|
|
->setBody("return \$this->getMedia(?);", [$collection]); |
|
118
|
|
|
|
|
119
|
|
|
// custom fields |
|
120
|
|
|
$generator->class->addMethod("getMedia{$studlyCollection}CustomFields") |
|
121
|
|
|
->setPublic() |
|
122
|
|
|
->setReturnType('array') |
|
123
|
|
|
->addComment("Returns custom fields for the media") |
|
124
|
|
|
->setBody("return ?;", [$customFields]); |
|
125
|
|
|
|
|
126
|
|
|
$generator->class->addMethod("get{$studlyFieldName}urlAttribute") |
|
127
|
|
|
->setPublic() |
|
128
|
|
|
->setReturnType('string') |
|
129
|
|
|
->addComment("Returns the media attribute (url) for the $collection") |
|
130
|
|
|
->setBody( /** @lang PHP */ |
|
131
|
|
|
<<< PHP |
|
132
|
|
|
\$image = \$this->getMedia{$studlyCollection}Collection()->first(); |
|
133
|
|
|
if (\$image) { |
|
134
|
|
|
return \$image->getUrl(); |
|
135
|
|
|
} |
|
136
|
|
|
return ''; |
|
137
|
|
|
PHP |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
// all image models for this collection |
|
141
|
|
|
$generator->class->addMethod("get{$studlyFieldName}Attribute") |
|
142
|
|
|
->setPublic() |
|
143
|
|
|
->setReturnType('array') |
|
144
|
|
|
->addComment("Returns media attribute for the $collection media with custom fields") |
|
145
|
|
|
->setBody( /** @lang PHP */ |
|
146
|
|
|
<<< PHP |
|
147
|
|
|
\$image = \$this->getMedia{$studlyCollection}Collection()->first(); |
|
148
|
|
|
if (\$image) { |
|
149
|
|
|
\$customFields = []; |
|
150
|
|
|
foreach (\$this->getMedia{$studlyCollection}CustomFields() as \$c) { |
|
151
|
|
|
\$customFields[\$c] = \$image->getCustomProperty(\$c); |
|
152
|
|
|
} |
|
153
|
|
|
return [ |
|
154
|
|
|
'url' => \$image->getUrl(), |
|
155
|
|
|
'fields' => json_encode(\$customFields) |
|
156
|
|
|
]; |
|
157
|
|
|
} |
|
158
|
|
|
return []; |
|
159
|
|
|
PHP |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
// TODO: get converted images, thumb https://spatie.be/docs/laravel-medialibrary/v8/converting-images/retrieving-converted-images |
|
163
|
|
|
return ''; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|