Guest User

Untitled

a guest
Nov 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. <?php
  2.  
  3. class Registry implements ArrayAccess {
  4. public $this = null;
  5.  
  6. protected $impls = array();
  7. protected $curImpl = 0;
  8.  
  9. public function &__get($key) {
  10. if (isset($this->__onUndefinedProperty)) {
  11. $_ = $this->__onUndefinedProperty($key);
  12. return $_;
  13. }
  14.  
  15. return $this->$key = new self;
  16. }
  17.  
  18. public function __call($key, $args) {
  19. if (!isset($this->$key)) {
  20. if (isset($this->__onUndefinedMethod)) {
  21. return $this->__onUndefinedMethod($key, $args);
  22. }
  23.  
  24. throw new BadMethodCallException(sprintf('Method "%s" does not exist.', $key));
  25. }
  26.  
  27. if (!is_callable($this->$key)) {
  28. throw new BadMethodCallException(sprintf('Method "%s" is not callable.', $key));
  29. }
  30.  
  31. $µThis =& µ()->this;
  32. $prevThis = $µThis;
  33.  
  34. $µThis = $this;
  35. $return = call_user_func_array($this->$key, $args);
  36. $µThis = $prevThis;
  37.  
  38. return $return;
  39. }
  40.  
  41. public function __invoke() {
  42. $backtrace = debug_backtrace();
  43. foreach ($backtrace as $info) {
  44. if (isset($info['class'], $info['type'], $info['function'], $info['args'], $info['args'][0])
  45. && 'Registry' == $info['class'] && '->' == $info['type'] && '__call' == $info['function']
  46. && 'this' != $info['args'][0]
  47. ) {
  48. return call_user_func_array($this->{$info['args'][0]}, func_get_args());
  49. }
  50. }
  51. }
  52.  
  53. public function offsetSet($key, $impl) {
  54. if (!is_array($impl)) {
  55. throw new InvalidArgumentException('To change the implementation you must pass an array.');
  56. }
  57.  
  58. if (null === $key) {
  59. if (get_object_vars($this) != array('this' => null, 'impl' => array(), 'curImpl' => 0)) {
  60. $this->impls[$this->curImpl++] = clone $this;
  61. }
  62.  
  63. $queuedKeys = array();
  64. foreach ($impl as $key => $value) {
  65. if (!is_string($key)) {
  66. $queuedKeys[] = $value;
  67. } else {
  68. if (!empty($queuedKeys)) {
  69. foreach ($queuedKeys as $queuedKey) {
  70. $this->$queuedKey = $value;
  71. }
  72. $queuedKeys = array();
  73. }
  74.  
  75. $this->$key = $value;
  76. }
  77. }
  78. } else {
  79. throw new Exception('Not implemented');
  80. }
  81. }
  82.  
  83. public function offsetGet($key) {
  84. return $this->curImpl === $key ? $this : $this->impls[$this->toImplKey($key)];
  85. }
  86.  
  87. public function offsetExists($key) {
  88. return $this->curImpl === $key || isset($this->impls[$this->toImplKey($key)]);
  89. }
  90.  
  91. public function offsetUnset($key) {
  92. throw new Exception('Not implemented');
  93. }
  94.  
  95. protected function toImplKey($key) {
  96. if (!is_int($key)) {
  97. throw new InvalidArgumentException(sprintf('Implementation key "%s" must be an integer.', $key));
  98. }
  99.  
  100. if ($key < 0) {
  101. $key = $this->curImpl + $key;
  102. }
  103.  
  104. return $key;
  105. }
  106. }
  107.  
  108. function µ() {
  109. static $registry = null;
  110.  
  111. if (null === $registry) {
  112. $registry = new Registry;
  113. }
  114.  
  115. return $registry;
  116. }
  117.  
  118. µ()->__onUndefinedProperty = function&($key) {
  119. µ()->this->$key = new Registry;
  120.  
  121. is_file($file = __DIR__ . '/lib/' . $key . '.php') && require_once $file;
  122.  
  123. return µ()->this->$key;
  124. };
Add Comment
Please, Sign In to add comment