Locker.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\framework\Support;
  3. /**
  4. * 储物柜(跨作用域保存遍历)
  5. * !!!只限于不需要扩展的二开项目中使用,禁止在标准产品中使用!!!
  6. * Class Locker
  7. * @package app\framework\Support
  8. */
  9. class Locker
  10. {
  11. private $grids = [];
  12. private $logs = [];
  13. /**
  14. * @var self
  15. */
  16. static $current;
  17. /**
  18. * constructor.
  19. */
  20. public function __construct()
  21. {
  22. self::$current = $this;
  23. }
  24. static public function current()
  25. {
  26. if (!isset(self::$current)) {
  27. return new static();
  28. }
  29. return self::$current;
  30. }
  31. /**
  32. * 取走
  33. * @param $key
  34. * @return mixed
  35. */
  36. private function take($key)
  37. {
  38. $result = $this->grids[$key];
  39. unset($this->grids[$key]);
  40. return $result;
  41. }
  42. /**
  43. * 借用
  44. * @param $key
  45. * @return mixed
  46. */
  47. private function borrow($key)
  48. {
  49. return $this->grids[$key];
  50. }
  51. /**
  52. * 丢掉
  53. * @param $key
  54. * @return mixed
  55. */
  56. private function drop($key)
  57. {
  58. unset($this->grids[$key]);
  59. }
  60. /**
  61. * 存
  62. * @param $key
  63. * @param $value
  64. * @return mixed
  65. */
  66. private function store($key, $value)
  67. {
  68. return $this->grids[$key] = $value;
  69. }
  70. private function _log($key, $action, $path)
  71. {
  72. $this->logs[$key] = [$action, $path];
  73. }
  74. public function trace($key)
  75. {
  76. return $this->logs[$key];
  77. }
  78. public function __call($name, $arguments)
  79. {
  80. $this->_log($arguments[0], $name, debug_backtrace(0, 1));
  81. return $this->$name(...$arguments);
  82. }
  83. }