1 | <?php |
||
29 | class DelegateStrategy implements MultiStrategy |
||
30 | { |
||
31 | /** |
||
32 | * @var SplFixedArray<\Caridea\Acl\Loader> The loaders to consult |
||
33 | */ |
||
34 | private $loaders; |
||
35 | |||
36 | /** |
||
37 | * Creates a new DelegateStrategy. |
||
38 | * |
||
39 | * @param \Caridea\Acl\Loader[] $loaders An array of Loaders to use |
||
40 | * @throws \InvalidArgumentException if an entry in `loaders` isn't a `Loader` |
||
41 | */ |
||
42 | 6 | public function __construct(array $loaders) |
|
43 | { |
||
44 | 6 | foreach ($loaders as $loader) { |
|
45 | 5 | if (!($loader instanceof Loader)) { |
|
46 | 5 | throw new \InvalidArgumentException("Only instances of Loader are permitted in the loaders argument"); |
|
47 | } |
||
48 | } |
||
49 | // HHVM appears to use max() inside of FixedArray which bails when empty |
||
50 | 5 | $this->loaders = empty($loaders) ? |
|
51 | 5 | new \SplFixedArray() : \SplFixedArray::fromArray($loaders); |
|
52 | 5 | } |
|
53 | |||
54 | /** |
||
55 | * Loads the ACL for a Target. |
||
56 | * |
||
57 | * @param \Caridea\Acl\Target $target The `Target` whose ACL will be loaded |
||
58 | * @param \Caridea\Acl\Subject[] $subjects An array of `Subject`s |
||
59 | * @param \Caridea\Acl\Service $service The calling service (to load parent ACLs) |
||
60 | * @return \Caridea\Acl\Acl The loaded ACL |
||
61 | * @throws \Caridea\Acl\Exception\Unloadable If the target provided is invalid |
||
62 | */ |
||
63 | 2 | public function load(Target $target, array $subjects, Service $service): Acl |
|
64 | { |
||
65 | 2 | foreach ($this->loaders as $loader) { |
|
66 | 2 | if ($loader->supports($target)) { |
|
67 | 2 | return $loader->load($target, $subjects, $service); |
|
68 | } |
||
69 | } |
||
70 | 1 | return new DenyAcl($target); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Loads the ACLs for several Targets. |
||
75 | * |
||
76 | * @since 2.1.0 |
||
77 | * @param \Caridea\Acl\Target[] $targets The `Target` whose ACL will be loaded |
||
78 | * @param \Caridea\Acl\Subject[] $subjects An array of `Subject`s |
||
79 | * @param \Caridea\Acl\Service $service The ACL service (to load parent ACLs) |
||
80 | * @return array<string,\Caridea\Acl\Acl> The loaded ACLs |
||
81 | * @throws \Caridea\Acl\Exception\Unloadable If the target provided is invalid |
||
82 | * @throws \InvalidArgumentException If the `subjects` argument contains invalid values |
||
83 | */ |
||
84 | 3 | public function loadAll(array $targets, array $subjects, Service $service): array |
|
114 | } |
||
115 |