Registry.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework;
  7. /**
  8. * Registry model. Used to manage values in registry
  9. *
  10. * Registry usage as a shared service introduces temporal, hard to detect coupling into system.
  11. * It's usage should be avoid. Use service classes or data providers instead.
  12. *
  13. * @api
  14. * @deprecated 102.0.0
  15. * @since 100.0.2
  16. */
  17. class Registry
  18. {
  19. /**
  20. * Registry collection
  21. *
  22. * @var array
  23. */
  24. private $_registry = [];
  25. /**
  26. * Retrieve a value from registry by a key
  27. *
  28. * @param string $key
  29. * @return mixed
  30. *
  31. * @deprecated 102.0.0
  32. */
  33. public function registry($key)
  34. {
  35. if (isset($this->_registry[$key])) {
  36. return $this->_registry[$key];
  37. }
  38. return null;
  39. }
  40. /**
  41. * Register a new variable
  42. *
  43. * @param string $key
  44. * @param mixed $value
  45. * @param bool $graceful
  46. * @return void
  47. * @throws \RuntimeException
  48. *
  49. * @deprecated 102.0.0
  50. */
  51. public function register($key, $value, $graceful = false)
  52. {
  53. if (isset($this->_registry[$key])) {
  54. if ($graceful) {
  55. return;
  56. }
  57. throw new \RuntimeException('Registry key "' . $key . '" already exists');
  58. }
  59. $this->_registry[$key] = $value;
  60. }
  61. /**
  62. * Unregister a variable from register by key
  63. *
  64. * @param string $key
  65. * @return void
  66. *
  67. * @deprecated 102.0.0
  68. */
  69. public function unregister($key)
  70. {
  71. if (isset($this->_registry[$key])) {
  72. if (is_object($this->_registry[$key])
  73. && method_exists($this->_registry[$key], '__destruct')
  74. && is_callable([$this->_registry[$key], '__destruct'])
  75. ) {
  76. $this->_registry[$key]->__destruct();
  77. }
  78. unset($this->_registry[$key]);
  79. }
  80. }
  81. /**
  82. * Destruct registry items
  83. */
  84. public function __destruct()
  85. {
  86. $keys = array_keys($this->_registry);
  87. array_walk($keys, [$this, 'unregister']);
  88. }
  89. }