Skip to content

Commit 009f9ca

Browse files
authored
Merge pull request #84 from sherlockode/feature/upload-events
Dispatch events on upload / remove file requests
2 parents eb70e2e + 639a90f commit 009f9ca

10 files changed

+367
-13
lines changed

Controller/DependentEntityController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function getDependentResultsAction(Request $request)
5050
}
5151

5252
$event = new GetResponseDependentResultEvent($mapper, $entity);
53-
$this->eventDispatcher->dispatch($event);
5453
if (Kernel::VERSION_ID < 40300) {
5554
$this->eventDispatcher->dispatch(get_class($event), $event);
5655
} else {

Controller/FileUploadController.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
namespace Sherlockode\AdvancedFormBundle\Controller;
44

55
use Doctrine\ORM\EntityManagerInterface;
6+
use Sherlockode\AdvancedFormBundle\Event\GetResponseRemoveFileEvent;
7+
use Sherlockode\AdvancedFormBundle\Event\GetResponseUploadFileEvent;
68
use Sherlockode\AdvancedFormBundle\Form\Type\EntityMappingType;
79
use Sherlockode\AdvancedFormBundle\Form\Type\UploadFileType;
810
use Sherlockode\AdvancedFormBundle\Manager\MappingManager;
911
use Sherlockode\AdvancedFormBundle\Manager\UploadManager;
1012
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1114
use Symfony\Component\HttpFoundation\JsonResponse;
1215
use Symfony\Component\HttpFoundation\Request;
1316
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Kernel;
1418

1519
class FileUploadController extends AbstractController
1620
{
@@ -30,15 +34,26 @@ class FileUploadController extends AbstractController
3034
private $em;
3135

3236
/**
33-
* @param UploadManager $uploadManager
34-
* @param MappingManager $mappingManager
35-
* @param EntityManagerInterface $em
37+
* @var EventDispatcherInterface
3638
*/
37-
public function __construct(UploadManager $uploadManager, MappingManager $mappingManager, EntityManagerInterface $em)
38-
{
39+
private $eventDispatcher;
40+
41+
/**
42+
* @param UploadManager $uploadManager
43+
* @param MappingManager $mappingManager
44+
* @param EntityManagerInterface $em
45+
* @param EventDispatcherInterface $eventDispatcher
46+
*/
47+
public function __construct(
48+
UploadManager $uploadManager,
49+
MappingManager $mappingManager,
50+
EntityManagerInterface $em,
51+
EventDispatcherInterface $eventDispatcher
52+
) {
3953
$this->uploadManager = $uploadManager;
4054
$this->mappingManager = $mappingManager;
4155
$this->em = $em;
56+
$this->eventDispatcher = $eventDispatcher;
4257
}
4358

4459
/**
@@ -55,6 +70,16 @@ public function uploadFileAction(Request $request)
5570
$mapping = $form->getData()['mapping'];
5671
$object = $form->getData()['entity'];
5772

73+
$event = new GetResponseUploadFileEvent($mapping->id, $object);
74+
if (Kernel::VERSION_ID < 40300) {
75+
$this->eventDispatcher->dispatch(get_class($event), $event);
76+
} else {
77+
$this->eventDispatcher->dispatch($event);
78+
}
79+
if ($event->getResponse() !== null) {
80+
return $event->getResponse();
81+
}
82+
5883
$form = $this->createForm(UploadFileType::class, $object, ['csrf_protection' => false, 'mapping' => $mapping]);
5984
$form->submit([$mapping->fileProperty => $request->files->get('afb_upload_file')['file']]);
6085
if ($form->isSubmitted()) {
@@ -106,6 +131,16 @@ public function removeFileAction(Request $request)
106131
$mapping = $form->getData()['mapping'];
107132
$object = $form->getData()['fileEntity'];
108133

134+
$event = new GetResponseRemoveFileEvent($mapping->id, $object);
135+
if (Kernel::VERSION_ID < 40300) {
136+
$this->eventDispatcher->dispatch(get_class($event), $event);
137+
} else {
138+
$this->eventDispatcher->dispatch($event);
139+
}
140+
if ($event->getResponse() !== null) {
141+
return $event->getResponse();
142+
}
143+
109144
if ($form->isSubmitted() && $form->isValid()) {
110145
$this->uploadManager->remove(
111146
$mapping,

Controller/TemporaryFileUploadController.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
namespace Sherlockode\AdvancedFormBundle\Controller;
44

5+
use Sherlockode\AdvancedFormBundle\Event\GetResponseRemoveTmpFileEvent;
6+
use Sherlockode\AdvancedFormBundle\Event\GetResponseUploadTmpFileEvent;
7+
use Sherlockode\AdvancedFormBundle\Event\GetResponseViewTmpFileEvent;
58
use Sherlockode\AdvancedFormBundle\Manager\UploadManager;
69
use Sherlockode\AdvancedFormBundle\Model\TemporaryUploadedFileInterface;
710
use Sherlockode\AdvancedFormBundle\Storage\StorageInterface;
811
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
913
use Symfony\Component\Form\Extension\Core\Type\FileType;
1014
use Symfony\Component\HttpFoundation\JsonResponse;
1115
use Symfony\Component\HttpFoundation\Request;
1216
use Symfony\Component\HttpFoundation\Response;
1317
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
18+
use Symfony\Component\HttpKernel\Kernel;
1419

1520
class TemporaryFileUploadController extends AbstractController
1621
{
@@ -29,18 +34,29 @@ class TemporaryFileUploadController extends AbstractController
2934
*/
3035
private $tmpStorage;
3136

37+
/**
38+
* @var EventDispatcherInterface
39+
*/
40+
private $eventDispatcher;
41+
3242
/**
3343
* FileUploadController constructor.
3444
*
35-
* @param UploadManager $uploadManager
36-
* @param string $tmpUploadClass
37-
* @param StorageInterface $tmpStorage
45+
* @param UploadManager $uploadManager
46+
* @param string $tmpUploadClass
47+
* @param StorageInterface $tmpStorage
48+
* @param EventDispatcherInterface $eventDispatcher
3849
*/
39-
public function __construct($uploadManager, $tmpUploadClass, $tmpStorage)
40-
{
50+
public function __construct(
51+
UploadManager $uploadManager,
52+
string $tmpUploadClass,
53+
StorageInterface $tmpStorage,
54+
EventDispatcherInterface $eventDispatcher
55+
) {
4156
$this->uploadManager = $uploadManager;
4257
$this->tmpUploadClass = $tmpUploadClass;
4358
$this->tmpStorage = $tmpStorage;
59+
$this->eventDispatcher = $eventDispatcher;
4460
}
4561

4662
/**
@@ -50,6 +66,16 @@ public function __construct($uploadManager, $tmpUploadClass, $tmpStorage)
5066
*/
5167
public function uploadTmpAction(Request $request)
5268
{
69+
$event = new GetResponseUploadTmpFileEvent();
70+
if (Kernel::VERSION_ID < 40300) {
71+
$this->eventDispatcher->dispatch(get_class($event), $event);
72+
} else {
73+
$this->eventDispatcher->dispatch($event);
74+
}
75+
if ($event->getResponse() !== null) {
76+
return $event->getResponse();
77+
}
78+
5379
$form = $this->createForm(FileType::class, null, ['csrf_protection' => false]);
5480
$form->submit($request->files->get('afb_upload_file')['file']);
5581

@@ -84,6 +110,16 @@ public function removeTmpFileAction(Request $request)
84110
$token = $request->get('token');
85111
$fileInfo = $this->getDoctrine()->getRepository($this->tmpUploadClass)->findOneBy(['token' => $token]);
86112
if ($fileInfo instanceof TemporaryUploadedFileInterface) {
113+
$event = new GetResponseRemoveTmpFileEvent($fileInfo);
114+
if (Kernel::VERSION_ID < 40300) {
115+
$this->eventDispatcher->dispatch(get_class($event), $event);
116+
} else {
117+
$this->eventDispatcher->dispatch($event);
118+
}
119+
if ($event->getResponse() !== null) {
120+
return $event->getResponse();
121+
}
122+
87123
$this->uploadManager->removeTemporary($fileInfo);
88124
}
89125

@@ -99,6 +135,20 @@ public function viewUploadedFileAction($token)
99135
{
100136
$fileInfo = $this->getDoctrine()->getRepository($this->tmpUploadClass)->findOneBy(['token' => $token]);
101137

138+
if (!$fileInfo instanceof TemporaryUploadedFileInterface) {
139+
throw $this->createNotFoundException();
140+
}
141+
142+
$event = new GetResponseViewTmpFileEvent($fileInfo);
143+
if (Kernel::VERSION_ID < 40300) {
144+
$this->eventDispatcher->dispatch(get_class($event), $event);
145+
} else {
146+
$this->eventDispatcher->dispatch($event);
147+
}
148+
if ($event->getResponse() !== null) {
149+
return $event->getResponse();
150+
}
151+
102152
$data = $this->tmpStorage->read($fileInfo->getKey());
103153

104154
return $this->createDownloadResponse(
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Sherlockode\AdvancedFormBundle\Event;
4+
5+
use Symfony\Component\HttpFoundation\Response;
6+
7+
class GetResponseRemoveFileEvent
8+
{
9+
/**
10+
* @var Response
11+
*/
12+
private $response;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $type;
18+
19+
/**
20+
* @var object
21+
*/
22+
private $entity;
23+
24+
/**
25+
* @param string $type
26+
* @param object $entity
27+
*/
28+
public function __construct(string $type, $entity)
29+
{
30+
$this->type = $type;
31+
$this->entity = $entity;
32+
}
33+
34+
/**
35+
* @return Response|null
36+
*/
37+
public function getResponse()
38+
{
39+
return $this->response;
40+
}
41+
42+
/**
43+
* @param Response $response
44+
*/
45+
public function setResponse(Response $response)
46+
{
47+
$this->response = $response;
48+
}
49+
50+
/**
51+
* @return object
52+
*/
53+
public function getEntity()
54+
{
55+
return $this->entity;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getType()
62+
{
63+
return $this->type;
64+
}
65+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Sherlockode\AdvancedFormBundle\Event;
4+
5+
use Sherlockode\AdvancedFormBundle\Model\TemporaryUploadedFileInterface;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
class GetResponseRemoveTmpFileEvent
9+
{
10+
/**
11+
* @var Response
12+
*/
13+
private $response;
14+
15+
/**
16+
* @var TemporaryUploadedFileInterface
17+
*/
18+
private $fileInfo;
19+
20+
/**
21+
* @param TemporaryUploadedFileInterface $fileInfo
22+
*/
23+
public function __construct(TemporaryUploadedFileInterface $fileInfo)
24+
{
25+
$this->fileInfo = $fileInfo;
26+
}
27+
28+
/**
29+
* @return Response|null
30+
*/
31+
public function getResponse()
32+
{
33+
return $this->response;
34+
}
35+
36+
/**
37+
* @param Response $response
38+
*/
39+
public function setResponse(Response $response)
40+
{
41+
$this->response = $response;
42+
}
43+
44+
/**
45+
* @return TemporaryUploadedFileInterface
46+
*/
47+
public function getFileInfo()
48+
{
49+
return $this->fileInfo;
50+
}
51+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Sherlockode\AdvancedFormBundle\Event;
4+
5+
use Symfony\Component\HttpFoundation\Response;
6+
7+
class GetResponseUploadFileEvent
8+
{
9+
/**
10+
* @var Response
11+
*/
12+
private $response;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $type;
18+
19+
/**
20+
* @var object
21+
*/
22+
private $entity;
23+
24+
/**
25+
* @param string $type
26+
* @param object $entity
27+
*/
28+
public function __construct(string $type, $entity)
29+
{
30+
$this->type = $type;
31+
$this->entity = $entity;
32+
}
33+
34+
/**
35+
* @return Response|null
36+
*/
37+
public function getResponse()
38+
{
39+
return $this->response;
40+
}
41+
42+
/**
43+
* @param Response $response
44+
*/
45+
public function setResponse(Response $response)
46+
{
47+
$this->response = $response;
48+
}
49+
50+
/**
51+
* @return object
52+
*/
53+
public function getEntity()
54+
{
55+
return $this->entity;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getType()
62+
{
63+
return $this->type;
64+
}
65+
}

0 commit comments

Comments
 (0)