-
Notifications
You must be signed in to change notification settings - Fork 16
/
TabsComponent.php
95 lines (76 loc) · 2.92 KB
/
TabsComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\AdminUi\Component;
use Ibexa\AdminUi\Tab\Event\TabEvent;
use Ibexa\AdminUi\Tab\Event\TabEvents;
use Ibexa\AdminUi\Tab\Event\TabGroupEvent;
use Ibexa\AdminUi\Tab\TabGroup;
use Ibexa\Contracts\AdminUi\Component\Renderable;
use Ibexa\Contracts\AdminUi\Tab\TabInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Twig\Environment;
class TabsComponent implements Renderable
{
/** @var \Twig\Environment */
protected $twig;
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
protected $eventDispatcher;
/** @var string */
protected $template;
/** @var string */
protected $groupIdentifier;
/** @var array */
protected $parameters;
public function __construct(
Environment $twig,
EventDispatcherInterface $eventDispatcher,
string $template,
string $groupIdentifier,
array $parameters = []
) {
$this->twig = $twig;
$this->eventDispatcher = $eventDispatcher;
$this->template = $template;
$this->groupIdentifier = $groupIdentifier;
$this->parameters = $parameters;
}
public function render(array $parameters = []): string
{
$tabGroup = new TabGroup($this->groupIdentifier);
$tabGroupEvent = new TabGroupEvent();
$tabGroupEvent->setData($tabGroup);
$tabGroupEvent->setParameters($parameters);
$this->eventDispatcher->dispatch($tabGroupEvent, TabEvents::TAB_GROUP_INITIALIZE);
$this->eventDispatcher->dispatch($tabGroupEvent, TabEvents::TAB_GROUP_PRE_RENDER);
$tabs = [];
foreach ($tabGroupEvent->getData()->getTabs() as $tab) {
$tabEvent = $this->dispatchTabPreRenderEvent($tab, $parameters);
$parameters = array_merge($parameters, $tabGroupEvent->getParameters(), $tabEvent->getParameters());
$tabs[] = $this->composeTabParameters($tabEvent->getData(), $parameters);
}
return $this->twig->render(
$this->template,
array_merge($this->parameters, $parameters, ['tabs' => $tabs, 'group' => $this->groupIdentifier])
);
}
private function dispatchTabPreRenderEvent(TabInterface $tab, array $parameters): TabEvent
{
$tabEvent = new TabEvent();
$tabEvent->setData($tab);
$tabEvent->setParameters($parameters);
$this->eventDispatcher->dispatch($tabEvent, TabEvents::TAB_PRE_RENDER);
return $tabEvent;
}
private function composeTabParameters(TabInterface $tab, array $parameters): array
{
return [
'name' => $tab->getName(),
'view' => $tab->renderView($parameters),
'identifier' => $tab->getIdentifier(),
];
}
}