new appraoch
This commit is contained in:
33
pancake/system/vendor/spatie/once/src/functions.php
vendored
Executable file
33
pancake/system/vendor/spatie/once/src/functions.php
vendored
Executable file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Spatie\Once\Backtrace;
|
||||
use Spatie\Once\Cache;
|
||||
|
||||
function once($callback)
|
||||
{
|
||||
$trace = debug_backtrace(
|
||||
DEBUG_BACKTRACE_PROVIDE_OBJECT, 2
|
||||
);
|
||||
|
||||
$backtrace = new Backtrace($trace);
|
||||
|
||||
if ($backtrace->getFunctionName() === 'eval') {
|
||||
return call_user_func($callback);
|
||||
}
|
||||
|
||||
$object = $backtrace->getObject();
|
||||
|
||||
$hash = $backtrace->getHash();
|
||||
|
||||
if (! Cache::isEnabled()) {
|
||||
return call_user_func($callback, $backtrace->getArguments());
|
||||
}
|
||||
|
||||
if (! Cache::has($object, $hash)) {
|
||||
$result = call_user_func($callback, $backtrace->getArguments());
|
||||
|
||||
Cache::set($object, $hash, $result);
|
||||
}
|
||||
|
||||
return Cache::get($object, $hash);
|
||||
}
|
||||
Reference in New Issue
Block a user