1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Menu Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\MenuBundle\Provider; |
18
|
|
|
|
19
|
|
|
use Knp\Menu\Provider\MenuProviderInterface; |
20
|
|
|
use SWP\Bundle\MenuBundle\Doctrine\MenuItemRepositoryInterface; |
21
|
|
|
use SWP\Bundle\MenuBundle\Model\MenuItemInterface; |
22
|
|
|
|
23
|
|
|
final class OrmMenuProvider implements MenuProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var MenuItemRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $repository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $internalCache = []; |
34
|
|
|
|
35
|
99 |
|
/** |
36
|
|
|
* MenuProvider constructor. |
37
|
99 |
|
* |
38
|
99 |
|
* @param MenuItemRepositoryInterface $repository |
39
|
|
|
*/ |
40
|
|
|
public function __construct(MenuItemRepositoryInterface $repository) |
41
|
|
|
{ |
42
|
|
|
$this->repository = $repository; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function get($name, array $options = []) |
49
|
|
|
{ |
50
|
|
|
if (null !== $result = $this->getFromInternalCache($name)) { |
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$menuItem = $this->repository->getOneMenuItemByName($name); |
55
|
|
|
|
56
|
|
|
if (!$menuItem instanceof MenuItemInterface) { |
57
|
|
|
throw new \InvalidArgumentException(sprintf('The menu "%s" is not defined.', $name)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $menuItem; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function has($name, array $options = []) |
67
|
|
|
{ |
68
|
|
|
if (null === $name) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$menuItem = $this->repository->getOneMenuItemByName($name); |
73
|
|
|
$result = $menuItem instanceof MenuItemInterface; |
74
|
|
|
if ($result) { |
75
|
|
|
$this->addToInternalCache($name, $menuItem); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getFromInternalCache($name) |
82
|
|
|
{ |
83
|
|
|
if (array_key_exists($name, $this->internalCache)) { |
84
|
|
|
return $this->internalCache[$name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function addToInternalCache($name, $value) |
91
|
|
|
{ |
92
|
|
|
$this->internalCache[$name] = $value; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|