1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Packy. |
5
|
|
|
* |
6
|
|
|
* (c) Peter Nijssen |
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
|
|
|
namespace AppBundle\Menu\Voter; |
13
|
|
|
|
14
|
|
|
use Knp\Menu\ItemInterface; |
15
|
|
|
use Knp\Menu\Matcher\Voter\VoterInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
17
|
|
|
|
18
|
|
|
class RouteVoter implements VoterInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var RequestStack |
22
|
|
|
*/ |
23
|
|
|
private $requestStack; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param RequestStack $requestStack |
29
|
|
|
*/ |
30
|
|
|
public function __construct(RequestStack $requestStack) |
31
|
|
|
{ |
32
|
|
|
$this->requestStack = $requestStack; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* match item to route. |
37
|
|
|
* |
38
|
|
|
* @param ItemInterface $item |
39
|
|
|
* |
40
|
|
|
* @return null|bool true if the item is current, null if not |
41
|
|
|
*/ |
42
|
|
|
public function matchItem(ItemInterface $item) |
43
|
|
|
{ |
44
|
|
|
if (null === $this->requestStack->getCurrentRequest()) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$route = $this->requestStack->getCurrentRequest()->attributes->get('_route'); |
49
|
|
|
if (null === $route) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$routes = (array) $item->getExtra('routes', []); |
54
|
|
|
|
55
|
|
|
foreach ($routes as $itemRoute) { |
56
|
|
|
if (isset($itemRoute['route'])) { |
57
|
|
|
if (is_string($itemRoute['route'])) { |
58
|
|
|
$itemRoute = [ |
59
|
|
|
'route' => $itemRoute['route'], |
60
|
|
|
'pattern' => '/' . $itemRoute['route'] . '/', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
if ($this->isMatchingRoute($itemRoute)) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if we can match a route. |
75
|
|
|
* |
76
|
|
|
* @param array $itemRoute An array with the route and the route pattern |
77
|
|
|
* |
78
|
|
|
* @return bool true if a match was found, false if not |
79
|
|
|
*/ |
80
|
|
|
private function isMatchingRoute(array $itemRoute) |
81
|
|
|
{ |
82
|
|
|
$route = $this->requestStack->getCurrentRequest()->attributes->get('_route'); |
83
|
|
|
$route = $this->getBaseRoute($route); |
84
|
|
|
|
85
|
|
|
if (!empty($itemRoute['route'])) { |
86
|
|
|
if ($this->getBaseRoute($itemRoute['route']) === $route) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the base of the route. |
96
|
|
|
* |
97
|
|
|
* @param $route |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function getBaseRoute($route) |
102
|
|
|
{ |
103
|
|
|
$chunks = explode('_', $route); |
104
|
|
|
|
105
|
|
|
return implode('_', array_slice($chunks, 0, 2)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|