ActionFlag.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. /**
  8. * Request processing flag that allows to stop request dispatching in action controller from an observer
  9. * Downside of this approach is temporal coupling and global communication.
  10. * Will be deprecated when Action Component is decoupled.
  11. *
  12. * Please use plugins to prevent action dispatching instead.
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class ActionFlag
  18. {
  19. /**
  20. * @var RequestInterface
  21. */
  22. protected $_request;
  23. /**
  24. * @var array
  25. */
  26. protected $_flags = [];
  27. /**
  28. * @param RequestInterface $request
  29. */
  30. public function __construct(\Magento\Framework\App\RequestInterface $request)
  31. {
  32. $this->_request = $request;
  33. }
  34. /**
  35. * Setting flag value
  36. *
  37. * @param string $action
  38. * @param string $flag
  39. * @param string $value
  40. * @return void
  41. */
  42. public function set($action, $flag, $value)
  43. {
  44. if ('' === $action) {
  45. $action = $this->_request->getActionName();
  46. }
  47. $this->_flags[$this->_getControllerKey()][$action][$flag] = $value;
  48. }
  49. /**
  50. * Retrieve flag value
  51. *
  52. * @param string $action
  53. * @param string $flag
  54. * @return bool
  55. *
  56. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  57. */
  58. public function get($action, $flag = '')
  59. {
  60. if ('' === $action) {
  61. $action = $this->_request->getActionName();
  62. }
  63. if ('' === $flag) {
  64. return $this->_flags[$this->_getControllerKey()] ?? [];
  65. } elseif (isset($this->_flags[$this->_getControllerKey()][$action][$flag])) {
  66. return $this->_flags[$this->_getControllerKey()][$action][$flag];
  67. } else {
  68. return false;
  69. }
  70. }
  71. /**
  72. * Get controller key
  73. *
  74. * @return string
  75. */
  76. protected function _getControllerKey()
  77. {
  78. return $this->_request->getRouteName() . '_' . $this->_request->getControllerName();
  79. }
  80. }