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\EventListener\Api; |
15
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Silverback\ApiComponentBundle\Annotation\UploadableField; |
18
|
|
|
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader; |
19
|
|
|
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
22
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Vincent Chalamon <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class UploadableEventListener |
28
|
|
|
{ |
29
|
|
|
use ClassMetadataTrait; |
30
|
|
|
|
31
|
|
|
private UploadableAnnotationReader $uploadableAnnotationReader; |
32
|
|
|
|
33
|
|
|
public function __construct(ManagerRegistry $registry, UploadableAnnotationReader $uploadableAnnotationReader) |
34
|
|
|
{ |
35
|
|
|
$this->initRegistry($registry); |
36
|
|
|
$this->uploadableAnnotationReader = $uploadableAnnotationReader; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function onPreWrite(ViewEvent $event): void |
40
|
|
|
{ |
41
|
|
|
$request = $event->getRequest(); |
42
|
|
|
$data = $request->attributes->get('data'); |
43
|
|
|
if ( |
44
|
|
|
empty($data) || |
45
|
|
|
!$this->uploadableAnnotationReader->isConfigured($data) || |
46
|
|
|
$request->isMethod(Request::METHOD_DELETE) |
47
|
|
|
) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
52
|
|
|
$classMetadata = $this->getClassMetadata($data); |
53
|
|
|
/** |
54
|
|
|
* @var UploadableField[] $fieldConfiguration |
55
|
|
|
*/ |
56
|
|
|
foreach ($this->uploadableAnnotationReader->getConfiguredProperties($data, true, true) as $fileProperty => $fieldConfiguration) { |
57
|
|
|
dump('uploadable event listener', $fileProperty, $fieldConfiguration); |
58
|
|
|
$file = $propertyAccessor->getValue($data, $fileProperty); |
59
|
|
|
$classMetadata->setFieldValue($data, $fieldConfiguration->property, '/new_filepath'); |
60
|
|
|
// todo Upload file to adapter |
61
|
|
|
// todo Set fileName on object |
62
|
|
|
// todo Set $field value to null for security/performance reasons |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|