1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\EventListener; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\NoResultException; |
17
|
|
|
use Eccube\Common\EccubeConfig; |
18
|
|
|
use Eccube\Entity\AuthorityRole; |
19
|
|
|
use Eccube\Entity\Master\DeviceType; |
20
|
|
|
use Eccube\Entity\Member; |
21
|
|
|
use Eccube\Repository\AuthorityRoleRepository; |
22
|
|
|
use Eccube\Repository\BaseInfoRepository; |
23
|
|
|
use Eccube\Repository\Master\DeviceTypeRepository; |
24
|
|
|
use Eccube\Repository\PageRepository; |
25
|
|
|
use Eccube\Request\Context; |
26
|
|
|
use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector; |
27
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
28
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
29
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
30
|
|
|
use Twig\Environment; |
31
|
|
|
|
32
|
|
|
class TwigInitializeListener implements EventSubscriberInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Environment |
36
|
|
|
*/ |
37
|
|
|
protected $twig; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var BaseInfoRepository |
41
|
|
|
*/ |
42
|
|
|
protected $baseInfoRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DeviceTypeRepository |
46
|
|
|
*/ |
47
|
|
|
protected $deviceTypeRepository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var PageRepository |
51
|
|
|
*/ |
52
|
|
|
protected $pageRepository; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Context |
56
|
|
|
*/ |
57
|
|
|
protected $requestContext; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var AuthorityRoleRepository |
61
|
|
|
*/ |
62
|
|
|
private $authorityRoleRepository; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var EccubeConfig |
66
|
|
|
*/ |
67
|
|
|
private $eccubeConfig; |
68
|
431 |
|
|
69
|
|
|
/** |
70
|
|
|
* @var MobileDetector |
71
|
|
|
*/ |
72
|
|
|
private $mobileDetector; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* TwigInitializeListener constructor. |
76
|
|
|
* |
77
|
431 |
|
* @param Environment $twig |
78
|
431 |
|
* @param BaseInfoRepository $baseInfoRepository |
79
|
431 |
|
* @param PageRepository $pageRepository |
80
|
431 |
|
* @param DeviceTypeRepository $deviceTypeRepository |
81
|
431 |
|
* @param AuthorityRoleRepository $authorityRoleRepository |
82
|
431 |
|
* @param EccubeConfig $eccubeConfig |
83
|
431 |
|
* @param Context $context |
84
|
|
|
* @param MobileDetector $mobileDetector |
85
|
|
|
*/ |
86
|
|
|
public function __construct( |
87
|
|
|
Environment $twig, |
88
|
|
|
BaseInfoRepository $baseInfoRepository, |
89
|
429 |
|
PageRepository $pageRepository, |
90
|
|
|
DeviceTypeRepository $deviceTypeRepository, |
91
|
429 |
|
AuthorityRoleRepository $authorityRoleRepository, |
92
|
429 |
|
EccubeConfig $eccubeConfig, |
93
|
429 |
|
Context $context, |
94
|
|
|
MobileDetector $mobileDetector |
95
|
|
|
) { |
96
|
429 |
|
$this->twig = $twig; |
97
|
118 |
|
$this->baseInfoRepository = $baseInfoRepository; |
98
|
|
|
$this->pageRepository = $pageRepository; |
99
|
|
|
$this->deviceTypeRepository = $deviceTypeRepository; |
100
|
428 |
|
$this->authorityRoleRepository = $authorityRoleRepository; |
101
|
270 |
|
$this->eccubeConfig = $eccubeConfig; |
102
|
|
|
$this->requestContext = $context; |
103
|
159 |
|
$this->mobileDetector = $mobileDetector; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param GetResponseEvent $event |
108
|
|
|
* |
109
|
|
|
* @throws NoResultException |
110
|
159 |
|
* @throws \Doctrine\ORM\NonUniqueResultException |
111
|
|
|
*/ |
112
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
113
|
159 |
|
{ |
114
|
159 |
|
$globals = $this->twig->getGlobals(); |
115
|
159 |
|
if (array_key_exists('BaseInfo', $globals) && $globals['BaseInfo'] === null) { |
116
|
2 |
|
$this->twig->addGlobal('BaseInfo', $this->baseInfoRepository->get()); |
117
|
2 |
|
} |
118
|
|
|
|
119
|
|
|
if (!$event->isMasterRequest()) { |
120
|
|
|
return; |
121
|
159 |
|
} |
122
|
|
|
|
123
|
|
|
if ($this->requestContext->isAdmin()) { |
124
|
159 |
|
$this->setAdminGlobals($event); |
125
|
159 |
|
} else { |
126
|
159 |
|
$this->setFrontVaribales($event); |
127
|
159 |
|
} |
128
|
159 |
|
} |
129
|
159 |
|
|
130
|
159 |
|
/** |
131
|
159 |
|
* @param GetResponseEvent $event |
132
|
159 |
|
* |
133
|
159 |
|
* @throws \Doctrine\ORM\NonUniqueResultException |
134
|
159 |
|
*/ |
135
|
159 |
|
public function setFrontVaribales(GetResponseEvent $event) |
136
|
159 |
|
{ |
137
|
93 |
|
/** @var \Symfony\Component\HttpFoundation\ParameterBag $attributes */ |
138
|
93 |
|
$attributes = $event->getRequest()->attributes; |
139
|
|
|
$route = $attributes->get('_route'); |
140
|
|
|
if ($route == 'user_data') { |
141
|
159 |
|
$routeParams = $attributes->get('_route_params', []); |
142
|
149 |
|
$route = isset($routeParams['route']) ? $routeParams['route'] : $attributes->get('route', ''); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$type = DeviceType::DEVICE_TYPE_PC; |
146
|
|
|
if ($this->mobileDetector->isMobile()) { |
147
|
|
|
$type = DeviceType::DEVICE_TYPE_SP; |
148
|
270 |
|
} |
149
|
|
|
$DeviceType = $this->deviceTypeRepository->find($type); |
150
|
|
|
|
151
|
270 |
|
try { |
152
|
270 |
|
$Page = $this->pageRepository->getByUrl($DeviceType, $route); |
|
|
|
|
153
|
|
|
} catch (NoResultException $e) { |
154
|
|
|
try { |
155
|
270 |
|
log_info('fallback to PC layout'); |
156
|
270 |
|
$DeviceType = $this->deviceTypeRepository->find(DeviceType::DEVICE_TYPE_PC); |
157
|
270 |
|
$Page = $this->pageRepository->getByUrl($DeviceType, $route); |
|
|
|
|
158
|
269 |
|
} catch (NoResultException $e) { |
159
|
|
|
$Page = $this->pageRepository->newPage($DeviceType); |
|
|
|
|
160
|
270 |
|
} |
161
|
3 |
|
} |
162
|
270 |
|
|
163
|
270 |
|
$this->twig->addGlobal('Page', $Page); |
164
|
|
|
$this->twig->addGlobal('title', $Page->getName()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param GetResponseEvent $event |
169
|
1 |
|
*/ |
170
|
|
|
public function setAdminGlobals(GetResponseEvent $event) |
171
|
|
|
{ |
172
|
1 |
|
// メニュー表示用配列. |
173
|
|
|
$menus = []; |
174
|
|
|
$this->twig->addGlobal('menus', $menus); |
175
|
|
|
|
176
|
|
|
// メニューの権限制御. |
177
|
|
|
$Member = $this->requestContext->getCurrentUser(); |
178
|
|
|
$AuthorityRoles = []; |
179
|
|
|
if ($Member instanceof Member) { |
180
|
|
|
$AuthorityRoles = $this->authorityRoleRepository->findBy(['Authority' => $this->requestContext->getCurrentUser()->getAuthority()]); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
$roles = array_map(function (AuthorityRole $AuthorityRole) use ($event) { |
183
|
|
|
return $event->getRequest()->getBaseUrl().'/'.$this->eccubeConfig['eccube_admin_route'].$AuthorityRole->getDenyUrl(); |
184
|
|
|
}, $AuthorityRoles); |
185
|
|
|
$this->twig->addGlobal('AuthorityRoles', $roles); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public static function getSubscribedEvents() |
192
|
|
|
{ |
193
|
|
|
return [ |
194
|
|
|
KernelEvents::REQUEST => [ |
195
|
|
|
// SecurityServiceProviderで、認証処理が完了した後に実行. |
196
|
|
|
['onKernelRequest', 6], |
197
|
|
|
], |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: