bavix /
lumper
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bavix\Lumper; |
||
| 4 | |||
| 5 | use Bavix\Slice\Slice; |
||
| 6 | |||
| 7 | class Lumper |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var [] |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 12 | */ |
||
| 13 | protected $instances = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param callable $callback |
||
| 17 | * |
||
| 18 | * @return string |
||
| 19 | */ |
||
| 20 | 2 | protected function _string(callable $callback) |
|
| 21 | { |
||
| 22 | 2 | return new Reflection\Func($callback); |
|
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param callable $callback |
||
| 27 | * @param string $hash |
||
| 28 | * |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | 3 | protected function _unique(callable $callback, $hash) |
|
| 32 | { |
||
| 33 | 3 | if (null === $hash || $hash instanceof Slice) |
|
|
0 ignored issues
–
show
|
|||
| 34 | { |
||
| 35 | 2 | $hash .= $this->_string($callback); |
|
| 36 | } |
||
| 37 | |||
| 38 | 3 | return $hash; |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param callable $callback |
||
| 43 | * @param string $unique |
||
| 44 | * |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | 3 | public function once(callable $callback, $unique = null) |
|
| 48 | { |
||
| 49 | 3 | $_ = (string)$this->_unique($callback, $unique); |
|
| 50 | |||
| 51 | 3 | if (empty($this->instances[$_])) |
|
| 52 | { |
||
| 53 | 3 | $this->instances[$_] = |
|
| 54 | 3 | $unique instanceof Slice ? |
|
| 55 | 1 | $callback($unique) : |
|
| 56 | 2 | $callback(); |
|
| 57 | } |
||
| 58 | |||
| 59 | 3 | return $this->instances[$_]; |
|
| 60 | } |
||
| 61 | |||
| 62 | } |
||
| 63 |