Builder.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Acl;
  7. /**
  8. * Access Control List Builder. Retrieves required role/rule/resource loaders
  9. * and uses them to populate provided ACL object. Acl object is put to cache after creation.
  10. * On consequent requests, ACL object is deserialized from cache.
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Builder
  16. {
  17. /**
  18. * Acl object
  19. *
  20. * @var \Magento\Framework\Acl
  21. */
  22. protected $_acl;
  23. /**
  24. * Acl loader list
  25. *
  26. * @var \Magento\Framework\Acl\LoaderInterface[]
  27. */
  28. protected $_loaderPool;
  29. /**
  30. * @var \Magento\Framework\AclFactory
  31. */
  32. protected $_aclFactory;
  33. /**
  34. * @param \Magento\Framework\AclFactory $aclFactory
  35. * @param \Magento\Framework\Acl\LoaderInterface $roleLoader
  36. * @param \Magento\Framework\Acl\LoaderInterface $resourceLoader
  37. * @param \Magento\Framework\Acl\LoaderInterface $ruleLoader
  38. */
  39. public function __construct(
  40. \Magento\Framework\AclFactory $aclFactory,
  41. \Magento\Framework\Acl\LoaderInterface $roleLoader,
  42. \Magento\Framework\Acl\LoaderInterface $resourceLoader,
  43. \Magento\Framework\Acl\LoaderInterface $ruleLoader
  44. ) {
  45. $this->_aclFactory = $aclFactory;
  46. $this->_loaderPool = [$roleLoader, $resourceLoader, $ruleLoader];
  47. }
  48. /**
  49. * Build Access Control List
  50. *
  51. * @return \Magento\Framework\Acl
  52. * @throws \LogicException
  53. */
  54. public function getAcl()
  55. {
  56. if ($this->_acl instanceof \Magento\Framework\Acl) {
  57. return $this->_acl;
  58. }
  59. try {
  60. $this->_acl = $this->_aclFactory->create();
  61. foreach ($this->_loaderPool as $loader) {
  62. $loader->populateAcl($this->_acl);
  63. }
  64. } catch (\Exception $e) {
  65. throw new \LogicException('Could not create an acl object: ' . $e->getMessage());
  66. }
  67. return $this->_acl;
  68. }
  69. /**
  70. * Remove cached ACL instance.
  71. *
  72. * @return $this
  73. * @since 101.0.0
  74. */
  75. public function resetRuntimeAcl()
  76. {
  77. $this->_acl = null;
  78. return $this;
  79. }
  80. }