Conditions | 3 |
Paths | 3 |
Total Lines | 22 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
6 | function once($callback) |
||
7 | { |
||
8 | $trace = debug_backtrace( |
||
9 | DEBUG_BACKTRACE_PROVIDE_OBJECT, 2 |
||
10 | )[1]; |
||
11 | |||
12 | $backtrace = new Backtrace($trace); |
||
13 | |||
14 | if (! $object = $backtrace->getObject()) { |
||
15 | throw new Exception('Cannot use `once` outside a non-static method of a class'); |
||
16 | } |
||
17 | |||
18 | $hash = $backtrace->getHash(); |
||
19 | |||
20 | if (! Cache::has($object, $hash)) { |
||
21 | $result = call_user_func($callback, $backtrace->getArguments()); |
||
22 | |||
23 | Cache::set($object, $hash, $result); |
||
24 | } |
||
25 | |||
26 | return Cache::get($object, $hash); |
||
27 | } |
||
28 |