Hoa central
Memoize.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Cache;
38 
112 class Memoize
113 {
119  private static $_multiton = [];
120 
126  protected $_callable = null;
127 
133  protected $_arguments = [];
134 
135 
136 
142  private function __construct()
143  {
144  return;
145  }
146 
153  public static function getInstance($callable)
154  {
155  $callable = xcallable($callable);
156  $hash = $callable->getHash();
157 
158  if (!isset(self::$_multiton[$hash])) {
159  self::$_multiton[$hash] = new static();
160  self::$_multiton[$hash]->_callable = $callable;
161  }
162 
163  return self::$_multiton[$hash];
164  }
165 
172  public function __invoke()
173  {
174  $arguments = func_get_args();
175  $id = md5(serialize($arguments));
176 
177  if (!isset($this->_arguments[$id])) {
178  $this->_arguments[$id] = $this->_callable->distributeArguments(
179  $arguments
180  );
181  }
182 
183  return $this->_arguments[$id];
184  }
185 }
186 
193 if (!function_exists('memoize')) {
194  function memoize($callable)
195  {
196  return Memoize::getInstance($callable);
197  }
198 }
static getInstance($callable)
Definition: Memoize.php:153
static $_multiton
Definition: Memoize.php:119