|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dedipanel project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net> |
|
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 DP\Core\CoreBundle\Menu; |
|
13
|
|
|
|
|
14
|
|
|
use Knp\Menu\MenuFactory as BaseMenuFactory; |
|
15
|
|
|
use DP\Core\CoreBundle\Menu\MenuItem; |
|
16
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
17
|
|
|
|
|
18
|
|
|
class MenuFactory extends BaseMenuFactory |
|
19
|
|
|
{ |
|
20
|
|
|
protected $generator; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(UrlGeneratorInterface $generator) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->generator = $generator; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function createItem($name, array $options = array()) |
|
28
|
|
|
{ |
|
29
|
|
|
$item = new MenuItem($name, $this); |
|
30
|
|
|
|
|
31
|
|
|
$options = array_merge( |
|
32
|
|
|
array( |
|
33
|
|
|
'uri' => null, |
|
34
|
|
|
'label' => null, |
|
35
|
|
|
'attributes' => array(), |
|
36
|
|
|
'linkAttributes' => array(), |
|
37
|
|
|
'childrenAttributes' => array(), |
|
38
|
|
|
'labelAttributes' => array(), |
|
39
|
|
|
'extras' => array(), |
|
40
|
|
|
'display' => true, |
|
41
|
|
|
'displayChildren' => true, |
|
42
|
|
|
'pattern' => null, |
|
43
|
|
|
), |
|
44
|
|
|
$options |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
if (!empty($options['route'])) { |
|
48
|
|
|
$params = isset($options['routeParameters']) ? $options['routeParameters'] : array(); |
|
49
|
|
|
$absolute = isset($options['routeAbsolute']) ? $options['routeAbsolute'] : false; |
|
50
|
|
|
$options['uri'] = $this->generator->generate($options['route'], $params, $absolute); |
|
51
|
|
|
|
|
52
|
|
|
if (!isset($options['pattern']) || empty($options['pattern'])) { |
|
53
|
|
|
$options['pattern'] = $options['uri']; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Ajoute un dolar au pattern utilisé pour déterminer |
|
58
|
|
|
// par regex s'il s'agit de l'item actuel |
|
59
|
|
|
if (!empty($options['pattern']) && isset($options['pattern_strict']) |
|
60
|
|
|
&& $options['pattern_strict'] === true) { |
|
61
|
|
|
$options['pattern'] .= '$'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$item |
|
|
|
|
|
|
65
|
|
|
->setUri($options['uri']) |
|
66
|
|
|
->setLabel($options['label']) |
|
67
|
|
|
->setAttributes($options['attributes']) |
|
68
|
|
|
->setLinkAttributes($options['linkAttributes']) |
|
69
|
|
|
->setChildrenAttributes($options['childrenAttributes']) |
|
70
|
|
|
->setLabelAttributes($options['labelAttributes']) |
|
71
|
|
|
->setExtras($options['extras']) |
|
72
|
|
|
->setDisplay($options['display']) |
|
73
|
|
|
->setDisplayChildren($options['displayChildren']) |
|
74
|
|
|
->setPattern($options['pattern']) |
|
75
|
|
|
; |
|
76
|
|
|
|
|
77
|
|
|
return $item; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: