|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Metadata\ApiResource; |
|
17
|
|
|
use ApiPlatform\Metadata\CollectionOperationInterface; |
|
18
|
|
|
use ApiPlatform\Metadata\Get; |
|
19
|
|
|
use ApiPlatform\Metadata\HttpOperation; |
|
20
|
|
|
use ApiPlatform\Metadata\Operation; |
|
21
|
|
|
use ApiPlatform\Metadata\Post; |
|
22
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
|
23
|
|
|
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
|
24
|
|
|
use ApiPlatform\Operation\PathSegmentNameGeneratorInterface; |
|
25
|
|
|
use JetBrains\PhpStorm\Pure; |
|
26
|
|
|
use Silverback\ApiComponentsBundle\Action\Uploadable\DownloadAction; |
|
27
|
|
|
use Silverback\ApiComponentsBundle\Action\Uploadable\UploadAction; |
|
28
|
|
|
use Silverback\ApiComponentsBundle\AttributeReader\UploadableAttributeReaderInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Configures API Platform metadata for file resources. |
|
32
|
|
|
* POST /resource_short_name/upload (multipart/form-data) |
|
33
|
|
|
* POST /resource_short_name/{id}/upload (multipart/form-data) |
|
34
|
|
|
* GET /resource_short_name/{id}/download/{property} (download file). |
|
35
|
|
|
* |
|
36
|
|
|
* @author Daniel West <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class UploadableResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
|
39
|
|
|
{ |
|
40
|
|
|
private ResourceMetadataCollectionFactoryInterface $decorated; |
|
41
|
|
|
private UploadableAttributeReaderInterface $uploadableFileManager; |
|
42
|
|
|
private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(ResourceMetadataCollectionFactoryInterface $decorated, UploadableAttributeReaderInterface $annotationReader, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->decorated = $decorated; |
|
47
|
|
|
$this->uploadableFileManager = $annotationReader; |
|
48
|
|
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function create(string $resourceClass): ResourceMetadataCollection |
|
52
|
|
|
{ |
|
53
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
|
54
|
|
|
if (!$this->uploadableFileManager->isConfigured($resourceClass)) { |
|
55
|
|
|
return $resourceMetadata; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$fields = $this->uploadableFileManager->getConfiguredProperties($resourceClass, false); |
|
59
|
|
|
$openApiRequestMultipartProperties = []; |
|
60
|
|
|
foreach ($fields as $field => $configuration) { |
|
61
|
|
|
$openApiRequestMultipartProperties[$field] = [ |
|
62
|
|
|
'type' => 'string', |
|
63
|
|
|
'format' => 'binary', |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var ApiResource $resourceMetadatum */ |
|
68
|
|
|
foreach ($resourceMetadata as $resourceMetadatum) { |
|
69
|
|
|
$resourceShortName = $resourceMetadatum->getShortName(); |
|
70
|
|
|
$pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName); |
|
71
|
|
|
$operations = $resourceMetadatum->getOperations(); |
|
72
|
|
|
if (!$operations) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
/** @var Operation $operation */ |
|
76
|
|
|
foreach ($operations as $operation) { |
|
77
|
|
|
if ($operation instanceof Post) { |
|
78
|
|
|
$postUploadOperation = static::generatePostOperation($operation, $openApiRequestMultipartProperties, $pathSegmentName); |
|
79
|
|
|
$operations->add(self::generateOperationName($postUploadOperation), $postUploadOperation); |
|
80
|
|
|
} |
|
81
|
|
|
if ($operation instanceof Get) { |
|
82
|
|
|
$uploadItemOperation = self::generateUploadItemOperation($operation, $openApiRequestMultipartProperties, $pathSegmentName); |
|
83
|
|
|
$operations->add(self::generateOperationName($uploadItemOperation), $uploadItemOperation); |
|
84
|
|
|
|
|
85
|
|
|
$downloadItemOperation = self::generateDownloadItemOperation($operation, $pathSegmentName); |
|
86
|
|
|
$operations->add(self::generateOperationName($downloadItemOperation), $downloadItemOperation); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $resourceMetadata; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
#[Pure] |
|
95
|
|
|
private static function generateOperationName(Operation $operation): string |
|
96
|
|
|
{ |
|
97
|
|
|
return sprintf( |
|
98
|
|
|
'_api_%s_%s%s', |
|
99
|
|
|
$operation->getUriTemplate(), |
|
|
|
|
|
|
100
|
|
|
strtolower($operation->getMethod()), |
|
|
|
|
|
|
101
|
|
|
$operation instanceof CollectionOperationInterface ? '_collection' : '' |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
#[Pure] |
|
106
|
|
|
private static function configurePostOperation(Operation $postOperation, array $openApiRequestMultipartProperties): Operation |
|
107
|
|
|
{ |
|
108
|
|
|
return $postOperation |
|
109
|
|
|
->withController(UploadAction::class) |
|
|
|
|
|
|
110
|
|
|
->withDeserialize(false) |
|
111
|
|
|
->withStateless(null) |
|
112
|
|
|
->withOpenapiContext([ |
|
113
|
|
|
'requestBody' => [ |
|
114
|
|
|
'content' => [ |
|
115
|
|
|
'multipart/form-data' => [ |
|
116
|
|
|
'schema' => [ |
|
117
|
|
|
'type' => 'object', |
|
118
|
|
|
'properties' => $openApiRequestMultipartProperties, |
|
119
|
|
|
], |
|
120
|
|
|
], |
|
121
|
|
|
], |
|
122
|
|
|
], |
|
123
|
|
|
]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
#[Pure] |
|
127
|
|
|
private static function generatePostOperation(Post $defaultOperation, array $openApiRequestMultipartProperties, string $pathSegmentName): Operation |
|
128
|
|
|
{ |
|
129
|
|
|
$path = sprintf('/%s/upload', $pathSegmentName); |
|
130
|
|
|
$newPost = $defaultOperation |
|
131
|
|
|
->withUriTemplate($path) |
|
132
|
|
|
->withShortName($defaultOperation->getShortName()) |
|
133
|
|
|
->withRoutePrefix($defaultOperation->getRoutePrefix() ?? ''); |
|
134
|
|
|
|
|
135
|
|
|
return self::configurePostOperation($newPost, $openApiRequestMultipartProperties); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
#[Pure] |
|
139
|
|
|
private static function generateUploadItemOperation(Get $getOperation, array $openApiRequestMultipartProperties, string $pathSegmentName): Operation |
|
140
|
|
|
{ |
|
141
|
|
|
$path = sprintf('/%s/{id}/upload', $pathSegmentName); |
|
142
|
|
|
$newUploadPost = $getOperation |
|
143
|
|
|
->withUriTemplate($path) |
|
144
|
|
|
->withMethod(HttpOperation::METHOD_POST) |
|
145
|
|
|
->withShortName($getOperation->getShortName()) |
|
146
|
|
|
->withRoutePrefix($getOperation->getRoutePrefix() ?? ''); |
|
147
|
|
|
|
|
148
|
|
|
return self::configurePostOperation($newUploadPost, $openApiRequestMultipartProperties); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
#[Pure] |
|
152
|
|
|
private static function generateDownloadItemOperation(Get $getOperation, string $pathSegmentName): Operation |
|
153
|
|
|
{ |
|
154
|
|
|
$downloadPath = sprintf('/%s/{id}/download/{property}', $pathSegmentName); |
|
155
|
|
|
|
|
156
|
|
|
return $getOperation |
|
157
|
|
|
->withUriTemplate($downloadPath) |
|
158
|
|
|
->withStateless(null) |
|
159
|
|
|
->withController(DownloadAction::class) |
|
160
|
|
|
->withSerialize(false); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|