|
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\EventListener\Api; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Daniel West <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ComponentPositionEventListener |
|
27
|
|
|
{ |
|
28
|
|
|
private ManagerRegistry $registry; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(ManagerRegistry $registry) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->registry = $registry; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function onPreWrite(ViewEvent $event): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->removeEmptyPositions($event); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function onPostRespond(ResponseEvent $event): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->addVaryHeader($event); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function addVaryHeader(ResponseEvent $event): void |
|
46
|
|
|
{ |
|
47
|
|
|
$request = $event->getRequest(); |
|
48
|
|
|
$data = $request->attributes->get('data'); |
|
49
|
|
|
$method = $request->getMethod(); |
|
50
|
|
|
if (!$data instanceof ComponentPosition || $method !== Request::METHOD_GET) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
if ($data->getPageDataProperty()) { |
|
54
|
|
|
$response = $event->getResponse(); |
|
55
|
|
|
$response->setVary('path', false); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function removeEmptyPositions(ViewEvent $event): void |
|
60
|
|
|
{ |
|
61
|
|
|
$request = $event->getRequest(); |
|
62
|
|
|
$data = $request->attributes->get('data'); |
|
63
|
|
|
// if we are deleting a component - check the positions for deletion as well |
|
64
|
|
|
if (!$data instanceof AbstractComponent || !$request->isMethod(Request::METHOD_DELETE)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$positions = $data->getComponentPositions(); |
|
69
|
|
|
$manager = $this->registry->getManagerForClass(ComponentPosition::class); |
|
70
|
|
|
if (!$manager) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
foreach ($positions as $position) { |
|
74
|
|
|
if (!$position->pageDataProperty) { |
|
75
|
|
|
$manager->remove($position); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|