Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Registry.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Acl/Role/Interface.php';
27 
28 
36 {
42  protected $_roles = array();
43 
63  public function add(Zend_Acl_Role_Interface $role, $parents = null)
64  {
65  $roleId = $role->getRoleId();
66 
67  if ($this->has($roleId)) {
71  #require_once 'Zend/Acl/Role/Registry/Exception.php';
72  throw new Zend_Acl_Role_Registry_Exception("Role id '$roleId' already exists in the registry");
73  }
74 
75  $roleParents = array();
76 
77  if (null !== $parents) {
78  if (!is_array($parents)) {
79  $parents = array($parents);
80  }
84  #require_once 'Zend/Acl/Role/Registry/Exception.php';
85  foreach ($parents as $parent) {
86  try {
87  if ($parent instanceof Zend_Acl_Role_Interface) {
88  $roleParentId = $parent->getRoleId();
89  } else {
90  $roleParentId = $parent;
91  }
92  $roleParent = $this->get($roleParentId);
94  throw new Zend_Acl_Role_Registry_Exception("Parent Role id '$roleParentId' does not exist", 0, $e);
95  }
96  $roleParents[$roleParentId] = $roleParent;
97  $this->_roles[$roleParentId]['children'][$roleId] = $role;
98  }
99  }
100 
101  $this->_roles[$roleId] = array(
102  'instance' => $role,
103  'parents' => $roleParents,
104  'children' => array()
105  );
106 
107  return $this;
108  }
109 
119  public function get($role)
120  {
121  if ($role instanceof Zend_Acl_Role_Interface) {
122  $roleId = $role->getRoleId();
123  } else {
124  $roleId = (string) $role;
125  }
126 
127  if (!$this->has($role)) {
131  #require_once 'Zend/Acl/Role/Registry/Exception.php';
132  throw new Zend_Acl_Role_Registry_Exception("Role '$roleId' not found");
133  }
134 
135  return $this->_roles[$roleId]['instance'];
136  }
137 
146  public function has($role)
147  {
148  if ($role instanceof Zend_Acl_Role_Interface) {
149  $roleId = $role->getRoleId();
150  } else {
151  $roleId = (string) $role;
152  }
153 
154  return isset($this->_roles[$roleId]);
155  }
156 
171  public function getParents($role)
172  {
173  $roleId = $this->get($role)->getRoleId();
174 
175  return $this->_roles[$roleId]['parents'];
176  }
177 
193  public function inherits($role, $inherit, $onlyParents = false)
194  {
198  #require_once 'Zend/Acl/Role/Registry/Exception.php';
199  try {
200  $roleId = $this->get($role)->getRoleId();
201  $inheritId = $this->get($inherit)->getRoleId();
202  } catch (Zend_Acl_Role_Registry_Exception $e) {
203  throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
204  }
205 
206  $inherits = isset($this->_roles[$roleId]['parents'][$inheritId]);
207 
208  if ($inherits || $onlyParents) {
209  return $inherits;
210  }
211 
212  foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
213  if ($this->inherits($parentId, $inheritId)) {
214  return true;
215  }
216  }
217 
218  return false;
219  }
220 
230  public function remove($role)
231  {
235  #require_once 'Zend/Acl/Role/Registry/Exception.php';
236  try {
237  $roleId = $this->get($role)->getRoleId();
238  } catch (Zend_Acl_Role_Registry_Exception $e) {
239  throw new Zend_Acl_Role_Registry_Exception($e->getMessage(), $e->getCode(), $e);
240  }
241 
242  foreach ($this->_roles[$roleId]['children'] as $childId => $child) {
243  unset($this->_roles[$childId]['parents'][$roleId]);
244  }
245  foreach ($this->_roles[$roleId]['parents'] as $parentId => $parent) {
246  unset($this->_roles[$parentId]['children'][$roleId]);
247  }
248 
249  unset($this->_roles[$roleId]);
250 
251  return $this;
252  }
253 
259  public function removeAll()
260  {
261  $this->_roles = array();
262 
263  return $this;
264  }
265 
266  public function getRoles()
267  {
268  return $this->_roles;
269  }
270 
271 }
inherits($role, $inherit, $onlyParents=false)
Definition: Registry.php:193
$roleId
Definition: webapi_user.php:22
add(Zend_Acl_Role_Interface $role, $parents=null)
Definition: Registry.php:63