1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RolesProvider.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:Permissions! |
9
|
|
|
* @subpackage Providers |
10
|
|
|
* @since 2.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 30.11.16 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Permissions\Providers; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Utils; |
21
|
|
|
|
22
|
|
|
use IPub\Permissions\Entities; |
23
|
|
|
use IPub\Permissions\Exceptions; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Basic resources provider |
27
|
|
|
* |
28
|
|
|
* @package iPublikuj:Permissions! |
29
|
|
|
* @subpackage Providers |
30
|
|
|
* |
31
|
|
|
* @author Adam Kadlec <[email protected]> |
32
|
|
|
*/ |
33
|
1 |
|
class ResourcesProvider extends Nette\Object implements IResourcesProvider |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Entities\IResource[] |
37
|
|
|
*/ |
38
|
|
|
private $resources = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $id |
42
|
|
|
* @param Entities\IResource|NULL $parent |
43
|
|
|
* |
44
|
|
|
* @return Entities\IResource |
45
|
|
|
*/ |
46
|
|
|
public function addResource(string $id, Entities\IResource $parent = NULL) : Entities\IResource |
47
|
|
|
{ |
48
|
1 |
|
if (array_key_exists($id, $this->resources)) { |
49
|
|
|
return $this->resources[$id]; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$resource = new Entities\Resource($id); |
53
|
|
|
|
54
|
1 |
|
if ($parent) { |
55
|
|
|
$resource->setParent($parent); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$this->resources[$id] = $resource; |
59
|
|
|
|
60
|
1 |
|
return $resource; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $id |
65
|
|
|
* |
66
|
|
|
* @return Entities\IResource |
67
|
|
|
*/ |
68
|
|
|
public function getResource(string $id) : Entities\IResource |
69
|
|
|
{ |
70
|
1 |
|
if (!array_key_exists($id, $this->resources)) { |
71
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Resource "%s" is not registered.', $id)); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $this->resources[$id]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Entities\IResource[] |
79
|
|
|
*/ |
80
|
|
|
public function findAll() : array |
81
|
|
|
{ |
82
|
1 |
|
return $this->resources; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|