-
Notifications
You must be signed in to change notification settings - Fork 4
/
TreeNodeConfigurator.php
117 lines (100 loc) · 3.68 KB
/
TreeNodeConfigurator.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
* This file is part of the phlexible package.
*
* (c) Stephan Wentz <sw@brainbits.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Phlexible\Bundle\TreeBundle\Configurator;
use Phlexible\Bundle\ElementBundle\ElementService;
use Phlexible\Bundle\ElementRendererBundle\Configurator\Configuration;
use Phlexible\Bundle\ElementRendererBundle\Configurator\ConfiguratorInterface;
use Phlexible\Bundle\ElementRendererBundle\ElementRendererEvents;
use Phlexible\Bundle\ElementRendererBundle\Event\ConfigureEvent;
use Phlexible\Bundle\TreeBundle\ContentTree\ContentTreeContext;
use Phlexible\Bundle\TreeBundle\Model\TreeNodeInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* TreeNode configurator.
*
* @author Stephan Wentz <sw@brainbits.net>
*/
class TreeNodeConfigurator implements ConfiguratorInterface
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ElementService
*/
private $elementService;
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
/**
* @param EventDispatcherInterface $dispatcher
* @param LoggerInterface $logger
* @param ElementService $elementService
* @param AuthorizationCheckerInterface $authorizationChecker
*/
public function __construct(
EventDispatcherInterface $dispatcher,
LoggerInterface $logger,
ElementService $elementService,
AuthorizationCheckerInterface $authorizationChecker)
{
$this->dispatcher = $dispatcher;
$this->logger = $logger;
$this->elementService = $elementService;
$this->authorizationChecker = $authorizationChecker;
}
/**
* {@inheritdoc}
*/
public function configure(Request $request, Configuration $renderConfiguration)
{
if (!$request->attributes->has('contentDocument') || !$request->attributes->get('contentDocument') instanceof TreeNodeInterface) {
return;
}
/* @var $treeNode TreeNodeInterface */
$treeNode = $originalTreeNode = $request->attributes->get('contentDocument');
$tree = $treeNode->getTree();
$version = -1;
if (!$request->attributes->get('_preview')) {
$version = $tree->getPublishedVersion($treeNode, $request->getLocale());
if (!$version) {
throw new NotFoundHttpException('TreeNode not published.');
}
}
$renderConfiguration
->addFeature('treeNode')
->setVariable('treeNode', $treeNode)
->setVariable('treeContext', new ContentTreeContext($treeNode))
->addFeature('eid')
->set('eid', $treeNode->getTypeId())
->set('version', $version)
->set('language', $request->getLocale());
if ($treeNode->getTemplate()) {
$renderConfiguration
->addFeature('template')
->setVariable('template', $treeNode->getTemplate());
}
$renderConfiguration
->setVariable('siteroot', $request->attributes->get('siterootUrl')->getSiteroot());
$event = new ConfigureEvent($renderConfiguration);
$this->dispatcher->dispatch(ElementRendererEvents::CONFIGURE_TREE_NODE, $event);
return true;
}
}