1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\ApiPlatform\Metadata\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface; |
19
|
|
|
use Silverback\ApiComponentBundle\Action\Uploadable\UploadableAction; |
20
|
|
|
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Configures API Platform metadata for file resources. |
24
|
|
|
* |
25
|
|
|
* @author Daniel West <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class UploadableResourceMetadataFactory implements ResourceMetadataFactoryInterface |
28
|
|
|
{ |
29
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
30
|
|
|
private UploadableAnnotationReader $uploadableHelper; |
31
|
|
|
private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator; |
32
|
|
|
|
33
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated, UploadableAnnotationReader $fileHelper, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
34
|
|
|
{ |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
$this->uploadableHelper = $fileHelper; |
37
|
|
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function create(string $resourceClass): ResourceMetadata |
41
|
|
|
{ |
42
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
43
|
|
|
if (!$this->uploadableHelper->isConfigured($resourceClass)) { |
44
|
|
|
return $resourceMetadata; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$fields = $this->uploadableHelper->getConfiguredProperties($resourceClass, false, false); |
48
|
|
|
$properties = []; |
49
|
|
|
foreach ($fields as $field) { |
50
|
|
|
$properties[$field] = [ |
51
|
|
|
'type' => 'string', |
52
|
|
|
'format' => 'binary', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
$resourceMetadata = $this->getCollectionPostResourceMetadata($resourceMetadata, $properties); |
56
|
|
|
|
57
|
|
|
return $this->getItemPutResourceMetadata($resourceMetadata, $properties); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getCollectionPostResourceMetadata(ResourceMetadata $resourceMetadata, array $properties): ResourceMetadata |
61
|
|
|
{ |
62
|
|
|
$resourceShortName = $resourceMetadata->getShortName(); |
63
|
|
|
$path = sprintf('/%s/upload', $this->pathSegmentNameGenerator->getSegmentName($resourceShortName)); |
64
|
|
|
|
65
|
|
|
$collectionOperations = $resourceMetadata->getCollectionOperations() ?? []; |
66
|
|
|
$collectionOperations['post_upload'] = array_merge(['method' => 'POST'], $this->getOperationConfiguration($properties, $path)); |
67
|
|
|
|
68
|
|
|
return $resourceMetadata->withCollectionOperations($collectionOperations); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, array $properties): ResourceMetadata |
72
|
|
|
{ |
73
|
|
|
$resourceShortName = $resourceMetadata->getShortName(); |
74
|
|
|
$path = sprintf('/%s/{id}/upload', $this->pathSegmentNameGenerator->getSegmentName($resourceShortName)); |
75
|
|
|
|
76
|
|
|
$itemOperations = $resourceMetadata->getItemOperations() ?? []; |
77
|
|
|
$itemOperations['put_upload'] = array_merge(['method' => 'PUT'], $this->getOperationConfiguration($properties, $path)); |
78
|
|
|
|
79
|
|
|
return $resourceMetadata->withItemOperations($itemOperations); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getOperationConfiguration(array $properties, string $path): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
'controller' => UploadableAction::class, |
86
|
|
|
'path' => $path, |
87
|
|
|
'deserialize' => false, |
88
|
|
|
'read' => 'false', |
89
|
|
|
'openapi_context' => [ |
90
|
|
|
'requestBody' => [ |
91
|
|
|
'content' => [ |
92
|
|
|
'multipart/form-data' => [ |
93
|
|
|
'schema' => [ |
94
|
|
|
'type' => 'object', |
95
|
|
|
'properties' => $properties, |
96
|
|
|
], |
97
|
|
|
], |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|