first
This commit is contained in:
64
system/vendor/spatie/once/src/Backtrace.php
vendored
Executable file
64
system/vendor/spatie/once/src/Backtrace.php
vendored
Executable file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Once;
|
||||
|
||||
class Backtrace
|
||||
{
|
||||
/** @var array */
|
||||
protected $trace;
|
||||
|
||||
/** @var array */
|
||||
protected $zeroStack;
|
||||
|
||||
public function __construct(array $trace)
|
||||
{
|
||||
$this->trace = $trace[1];
|
||||
$this->zeroStack = $trace[0];
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return $this->trace['args'];
|
||||
}
|
||||
|
||||
public function getFunctionName(): string
|
||||
{
|
||||
return $this->trace['function'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getObject()
|
||||
{
|
||||
if ($this->globalFunction()) {
|
||||
return $this->zeroStack['file'];
|
||||
}
|
||||
|
||||
return $this->staticCall() ? $this->trace['class'] : $this->trace['object'];
|
||||
}
|
||||
|
||||
public function getHash(): string
|
||||
{
|
||||
$normalizedArguments = array_map(function ($argument) {
|
||||
return is_object($argument) ? spl_object_hash($argument) : $argument;
|
||||
}, $this->getArguments());
|
||||
|
||||
$prefix = $this->getFunctionName();
|
||||
if (strpos($prefix, '{closure}') !== false) {
|
||||
$prefix = $this->zeroStack['line'];
|
||||
}
|
||||
|
||||
return md5($prefix.serialize($normalizedArguments));
|
||||
}
|
||||
|
||||
protected function staticCall()
|
||||
{
|
||||
return $this->trace['type'] == '::';
|
||||
}
|
||||
|
||||
protected function globalFunction()
|
||||
{
|
||||
return ! isset($this->trace['type']);
|
||||
}
|
||||
}
|
||||
111
system/vendor/spatie/once/src/Cache.php
vendored
Executable file
111
system/vendor/spatie/once/src/Cache.php
vendored
Executable file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Once;
|
||||
|
||||
class Cache
|
||||
{
|
||||
/** @var array */
|
||||
public static $values = [];
|
||||
|
||||
/** @var bool */
|
||||
protected static $enabled = true;
|
||||
|
||||
/**
|
||||
* Determine if a value exists for a given object / hash.
|
||||
*
|
||||
* @param mixed $object
|
||||
* @param string $backtraceHash
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($object, string $backtraceHash): bool
|
||||
{
|
||||
$objectHash = static::objectHash($object);
|
||||
|
||||
if (! isset(static::$values[$objectHash])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return array_key_exists($backtraceHash, static::$values[$objectHash]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a value for an object / hash.
|
||||
*
|
||||
* @param mixed $object
|
||||
* @param string $backtraceHash
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($object, string $backtraceHash)
|
||||
{
|
||||
return static::$values[static::objectHash($object)][$backtraceHash];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a cached value for an object / hash.
|
||||
*
|
||||
* @param mixed $object
|
||||
* @param string $backtraceHash
|
||||
* @param mixed $value
|
||||
*/
|
||||
public static function set($object, string $backtraceHash, $value)
|
||||
{
|
||||
static::addDestroyListener($object);
|
||||
|
||||
static::$values[static::objectHash($object)][$backtraceHash] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forget the stored items for the given objectHash.
|
||||
*
|
||||
* @param string $objectHash
|
||||
*/
|
||||
public static function forget(string $objectHash)
|
||||
{
|
||||
unset(static::$values[$objectHash]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the entire cache.
|
||||
*/
|
||||
public static function flush()
|
||||
{
|
||||
static::$values = [];
|
||||
}
|
||||
|
||||
protected static function objectHash($object): string
|
||||
{
|
||||
return is_string($object) ? $object : spl_object_hash($object);
|
||||
}
|
||||
|
||||
protected static function addDestroyListener($object)
|
||||
{
|
||||
if (is_string($object)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$randomPropertyName = '___once_listener__'.rand(1, 1000000);
|
||||
|
||||
if (isset($object->$randomPropertyName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$object->$randomPropertyName = new Listener($object);
|
||||
}
|
||||
|
||||
public static function disable()
|
||||
{
|
||||
static::$enabled = false;
|
||||
}
|
||||
|
||||
public static function enable()
|
||||
{
|
||||
static::$enabled = true;
|
||||
}
|
||||
|
||||
public static function isEnabled(): bool
|
||||
{
|
||||
return static::$enabled;
|
||||
}
|
||||
}
|
||||
26
system/vendor/spatie/once/src/Listener.php
vendored
Executable file
26
system/vendor/spatie/once/src/Listener.php
vendored
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\Once;
|
||||
|
||||
class Listener
|
||||
{
|
||||
/** @var bool */
|
||||
private $hasBeenSerialized = false;
|
||||
|
||||
public function __construct($object)
|
||||
{
|
||||
$this->objectHash = spl_object_hash($object);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if (! $this->hasBeenSerialized) {
|
||||
Cache::forget($this->objectHash);
|
||||
}
|
||||
}
|
||||
|
||||
public function __wakeup()
|
||||
{
|
||||
$this->hasBeenSerialized = true;
|
||||
}
|
||||
}
|
||||
33
system/vendor/spatie/once/src/functions.php
vendored
Executable file
33
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