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\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface; |
19
|
|
|
use Silverback\ApiComponentsBundle\Action\Uploadable\UploadableAction; |
20
|
|
|
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReaderInterface; |
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 UploadableAnnotationReaderInterface $uploadableHelper; |
31
|
|
|
private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator; |
32
|
|
|
|
33
|
2 |
|
public function __construct(ResourceMetadataFactoryInterface $decorated, UploadableAnnotationReaderInterface $uploadableHelper, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
34
|
|
|
{ |
35
|
2 |
|
$this->decorated = $decorated; |
36
|
2 |
|
$this->uploadableHelper = $uploadableHelper; |
37
|
2 |
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
2 |
|
public function create(string $resourceClass): ResourceMetadata |
41
|
|
|
{ |
42
|
2 |
|
$resourceMetadata = $this->decorated->create($resourceClass); |
43
|
2 |
|
if (!$this->uploadableHelper->isConfigured($resourceClass)) { |
44
|
1 |
|
return $resourceMetadata; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$fields = $this->uploadableHelper->getConfiguredProperties($resourceClass, false, false); |
48
|
1 |
|
$properties = []; |
49
|
1 |
|
foreach ($fields as $field) { |
50
|
1 |
|
$properties[$field] = [ |
51
|
|
|
'type' => 'string', |
52
|
|
|
'format' => 'binary', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
1 |
|
$resourceShortName = $resourceMetadata->getShortName(); |
56
|
1 |
|
$pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName); |
|
|
|
|
57
|
1 |
|
$resourceMetadata = $this->getCollectionPostResourceMetadata($resourceMetadata, $properties, $pathSegmentName); |
58
|
|
|
|
59
|
1 |
|
return $this->getItemPutResourceMetadata($resourceMetadata, $properties, $pathSegmentName); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
private function getCollectionPostResourceMetadata(ResourceMetadata $resourceMetadata, array $properties, string $pathSegmentName): ResourceMetadata |
63
|
|
|
{ |
64
|
1 |
|
$path = sprintf('/%s/upload', $pathSegmentName); |
65
|
|
|
|
66
|
1 |
|
$collectionOperations = $resourceMetadata->getCollectionOperations() ?? []; |
67
|
1 |
|
$collectionOperations['post_upload'] = array_merge(['method' => 'POST'], $this->getOperationConfiguration($properties, $path)); |
68
|
|
|
|
69
|
1 |
|
return $resourceMetadata->withCollectionOperations($collectionOperations); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
private function getItemPutResourceMetadata(ResourceMetadata $resourceMetadata, array $properties, string $pathSegmentName): ResourceMetadata |
73
|
|
|
{ |
74
|
1 |
|
$path = sprintf('/%s/{id}/upload', $pathSegmentName); |
75
|
|
|
|
76
|
1 |
|
$itemOperations = $resourceMetadata->getItemOperations() ?? []; |
77
|
1 |
|
$putProperties = $this->getOperationConfiguration($properties, $path); |
78
|
1 |
|
$itemOperations['put_upload'] = array_merge(['method' => 'PUT'], $putProperties); |
79
|
1 |
|
$itemOperations['patch_upload'] = array_merge(['method' => 'PATCH'], $putProperties); |
80
|
|
|
|
81
|
1 |
|
return $resourceMetadata->withItemOperations($itemOperations); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
private function getOperationConfiguration(array $properties, string $path): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
1 |
|
'controller' => UploadableAction::class, |
88
|
1 |
|
'path' => $path, |
89
|
|
|
'deserialize' => false, |
90
|
|
|
'read' => false, |
91
|
|
|
'openapi_context' => [ |
92
|
|
|
'requestBody' => [ |
93
|
|
|
'content' => [ |
94
|
|
|
'multipart/form-data' => [ |
95
|
|
|
'schema' => [ |
96
|
1 |
|
'type' => 'object', |
97
|
1 |
|
'properties' => $properties, |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|