1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MenuBundle\Builder; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MenuBundle\Exception\LocaleException; |
6
|
|
|
use Alpixel\Bundle\MenuBundle\Model\ItemInterface; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Knp\Menu\FactoryInterface; |
9
|
|
|
use Knp\Menu\MenuItem as KnpMenuItem; |
10
|
|
|
use UnexpectedValueException; |
11
|
|
|
|
12
|
|
|
class MenuBuilder |
13
|
|
|
{ |
14
|
|
|
protected $entityManager; |
15
|
|
|
protected $factory; |
16
|
|
|
protected $knpMenu; |
17
|
|
|
protected $defaultLocale; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* MenuBuilder constructor. |
22
|
|
|
* |
23
|
|
|
* @param EntityManager $entityManager |
24
|
|
|
* @param FactoryInterface $factory |
25
|
|
|
*/ |
26
|
|
|
function __construct(EntityManager $entityManager, FactoryInterface $factory) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->entityManager = $entityManager; |
29
|
|
|
$this->factory = $factory; |
30
|
|
|
$this->knpMenu = null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check if locale is valid |
35
|
|
|
* |
36
|
|
|
* @param $locale |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
protected static function isValidLocale($locale) |
40
|
|
|
{ |
41
|
|
|
if (is_string($locale) && !empty($locale)) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The parameter $locale must be defined in your |
50
|
|
|
* symfony configuration file under parameters. |
51
|
|
|
* |
52
|
|
|
* @param $locale String |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setDefaultLocale($locale) |
56
|
|
|
{ |
57
|
|
|
if(self::isValidLocale($locale)) { |
58
|
|
|
$this->defaultLocale = $locale; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new LocaleException(' |
64
|
|
|
The $locale parameter must be a non empty string or the locale is not defined |
65
|
|
|
under the Symfony parameters configuration. |
66
|
|
|
'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if the machineName is valid |
71
|
|
|
* |
72
|
|
|
* @param $machineName |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public static function isValidMachineNamme($machineName) |
76
|
|
|
{ |
77
|
|
|
if (is_string($machineName) && !empty($machineName)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrun null or a KnpMenuItem instance |
86
|
|
|
* |
87
|
|
|
* @return null|KnpMenuItem |
88
|
|
|
*/ |
89
|
|
|
public function getKnpMenu() |
90
|
|
|
{ |
91
|
|
|
return $this->knpMenu; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the KnpMenuItem instance |
96
|
|
|
* |
97
|
|
|
* @param KnpMenuItem $knpMenu |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setKnpMenu(KnpMenuItem $knpMenu) |
101
|
|
|
{ |
102
|
|
|
$this->knpMenu = $knpMenu; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create KnpMenuItem |
109
|
|
|
* |
110
|
|
|
* @param string $machineName The name of menu |
111
|
|
|
* @param string $locale Language code (Recommanded ISO-639) |
112
|
|
|
* |
113
|
|
|
* @return KnpMenuItem Get formatted menu |
114
|
|
|
*/ |
115
|
|
|
public function createKnpMenu($machineName, $locale = null) |
116
|
|
|
{ |
117
|
|
|
if (!self::isValidMachineNamme($machineName)) { |
118
|
|
|
throw new UnexpectedValueException('The parameter $machineName must be a non empty string'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($locale === null) { |
122
|
|
|
$locale = $this->defaultLocale; |
123
|
|
|
} else if (!self::isValidLocale($locale)) { |
124
|
|
|
throw new LocaleException(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$repository = $this->entityManager->getRepository('AlpixelMenuBundle:Menu'); |
128
|
|
|
$menu = $repository->findOneMenuByMachineNameAndLocale($machineName, $locale); |
129
|
|
|
$items = $menu->getItems()->toArray(); |
130
|
|
|
|
131
|
|
|
$this->setKnpMenu($this->factory->createItem('root')); |
|
|
|
|
132
|
|
|
foreach ($items as $item) { |
133
|
|
|
if($item->getParent() === null) { |
134
|
|
|
$this->getTree($this->knpMenu, $item); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->getKnpMenu(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Create tree un KnpMenuItem |
143
|
|
|
* |
144
|
|
|
* @param KnpMenuItem $knpMenu |
145
|
|
|
* @param ItemInterface $item |
146
|
|
|
* @param KnpMenuItem|null $parent |
147
|
|
|
* |
148
|
|
|
* @return KnpMenuItem A formatted KnpMenu |
149
|
|
|
*/ |
150
|
|
|
protected function getTree(KnpMenuItem $knpMenu, ItemInterface $item, KnpMenuItem $parent = null) |
151
|
|
|
{ |
152
|
|
|
$menuItem = ($parent === null) ? $knpMenu->addChild($item) : $parent->addChild($item); |
153
|
|
|
|
154
|
|
|
if ($uri = $item->getUri() !== null) { |
155
|
|
|
$menuItem->setUri($uri); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($item->getChidlren() as $child) { |
159
|
|
|
$this->getTree($knpMenu, $child, $menuItem); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $menuItem; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.