| @@ 8-69 (lines=62) @@ | ||
| 5 | use Exception; |
|
| 6 | use LAG\AdminBundle\Action\ActionInterface; |
|
| 7 | ||
| 8 | class Registry |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * Actions registry. |
|
| 12 | * |
|
| 13 | * @var ActionInterface[] |
|
| 14 | */ |
|
| 15 | protected $actions = []; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * Add an Action to the registry. |
|
| 19 | * |
|
| 20 | * @param $id |
|
| 21 | * @param ActionInterface $action |
|
| 22 | * |
|
| 23 | * @throws Exception |
|
| 24 | */ |
|
| 25 | public function add($id, ActionInterface $action) |
|
| 26 | { |
|
| 27 | if (array_key_exists($id, $this->actions)) { |
|
| 28 | throw new Exception('An Action with the name "'.$id.'" has already been registered'); |
|
| 29 | } |
|
| 30 | $this->actions[$id] = $action; |
|
| 31 | } |
|
| 32 | ||
| 33 | /** |
|
| 34 | * Return an Action from the registry. |
|
| 35 | * |
|
| 36 | * @param string $id |
|
| 37 | * @return ActionInterface |
|
| 38 | * @throws Exception |
|
| 39 | */ |
|
| 40 | public function get($id) |
|
| 41 | { |
|
| 42 | if (!array_key_exists($id, $this->actions)) { |
|
| 43 | throw new Exception('No Action with the service id "'.$id.'" has been found'); |
|
| 44 | } |
|
| 45 | ||
| 46 | return $this->actions[$id]; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Return true if an Action with the name $id has been registered. |
|
| 51 | * |
|
| 52 | * @param string $id |
|
| 53 | * @return bool |
|
| 54 | */ |
|
| 55 | public function has($id) |
|
| 56 | { |
|
| 57 | return array_key_exists($id, $this->actions); |
|
| 58 | } |
|
| 59 | ||
| 60 | /** |
|
| 61 | * Return all the registered Actions. |
|
| 62 | * |
|
| 63 | * @return ActionInterface[] |
|
| 64 | */ |
|
| 65 | public function all() |
|
| 66 | { |
|
| 67 | return $this->actions; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 11-70 (lines=60) @@ | ||
| 8 | /** |
|
| 9 | * Admin registry to store Admins along the request. |
|
| 10 | */ |
|
| 11 | class Registry |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * Admins registry. |
|
| 15 | * |
|
| 16 | * @var AdminInterface[] |
|
| 17 | */ |
|
| 18 | protected $admins = []; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Add an Admin to the registry. |
|
| 22 | * |
|
| 23 | * @param AdminInterface $admin |
|
| 24 | * @throws Exception |
|
| 25 | */ |
|
| 26 | public function add(AdminInterface $admin) |
|
| 27 | { |
|
| 28 | if (array_key_exists($admin->getName(), $this->admins)) { |
|
| 29 | throw new Exception('An Admin with the name "'.$admin->getName().'" has already been registered'); |
|
| 30 | } |
|
| 31 | $this->admins[$admin->getName()] = $admin; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Return an Admin from the registry. |
|
| 36 | * |
|
| 37 | * @param string $name |
|
| 38 | * @return AdminInterface |
|
| 39 | * @throws Exception |
|
| 40 | */ |
|
| 41 | public function get($name) |
|
| 42 | { |
|
| 43 | if (!array_key_exists($name, $this->admins)) { |
|
| 44 | throw new Exception('No Admin with the name "'.$name.'" has been found'); |
|
| 45 | } |
|
| 46 | ||
| 47 | return $this->admins[$name]; |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * Return true if an Admin with the name $name has been registered. |
|
| 52 | * |
|
| 53 | * @param string $name |
|
| 54 | * @return bool |
|
| 55 | */ |
|
| 56 | public function has($name) |
|
| 57 | { |
|
| 58 | return array_key_exists($name, $this->admins); |
|
| 59 | } |
|
| 60 | ||
| 61 | /** |
|
| 62 | * Return all the registered Admins. |
|
| 63 | * |
|
| 64 | * @return AdminInterface[] |
|
| 65 | */ |
|
| 66 | public function all() |
|
| 67 | { |
|
| 68 | return $this->admins; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||