|
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 Silverback\ApiComponentBundle\Action\File\CreateFileAction; |
|
19
|
|
|
use Silverback\ApiComponentBundle\Helper\FileHelper; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Configures API Platform metadata for media object resources. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Daniel West <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class FileResourceMetadataFactory implements ResourceMetadataFactoryInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
|
29
|
|
|
private FileHelper $mediaObjectHelper; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated, FileHelper $mediaObjectHelper) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->decorated = $decorated; |
|
34
|
|
|
$this->mediaObjectHelper = $mediaObjectHelper; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function create(string $resourceClass): ResourceMetadata |
|
38
|
|
|
{ |
|
39
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
|
40
|
|
|
if (!$this->mediaObjectHelper->isConfigured($resourceClass)) { |
|
41
|
|
|
return $resourceMetadata; |
|
42
|
|
|
} |
|
43
|
|
|
$mediaConfiguration = $this->mediaObjectHelper->getConfiguration($resourceClass); |
|
44
|
|
|
|
|
45
|
|
|
$attributes = $resourceMetadata->getAttributes() ?: []; |
|
46
|
|
|
$collectionOperations = $resourceMetadata->getAttribute('collectionOperations') ?? []; |
|
47
|
|
|
|
|
48
|
|
|
$collectionOperations['post'] = array_replace_recursive( |
|
49
|
|
|
$collectionOperations['post'] ?? [], |
|
50
|
|
|
[ |
|
51
|
|
|
'controller' => CreateFileAction::class, |
|
52
|
|
|
'deserialize' => false, |
|
53
|
|
|
'openapi_context' => [ |
|
54
|
|
|
'requestBody' => [ |
|
55
|
|
|
'content' => [ |
|
56
|
|
|
'multipart/form-data' => [ |
|
57
|
|
|
'schema' => [ |
|
58
|
|
|
'type' => 'object', |
|
59
|
|
|
'properties' => [ |
|
60
|
|
|
$mediaConfiguration->fileFieldName => [ |
|
61
|
|
|
'type' => 'string', |
|
62
|
|
|
'format' => 'binary', |
|
63
|
|
|
], |
|
64
|
|
|
], |
|
65
|
|
|
], |
|
66
|
|
|
], |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
], |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
// Because we have defined a post operation, if we do not add this then we cannot get collections of media objects |
|
74
|
|
|
// User can disable this functionality in their annotation |
|
75
|
|
|
if (!$mediaConfiguration->disableGetCollection && !isset($collectionOperations['get'])) { |
|
76
|
|
|
$collectionOperations['get'] = []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$resourceMetadata->withAttributes( |
|
80
|
|
|
array_merge($attributes, [ |
|
81
|
|
|
'collectionOperations' => $collectionOperations, |
|
82
|
|
|
]) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $resourceMetadata; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|