From 509c66070df1da7c72c0b01fb68fb815ac856e9c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 4 Feb 2024 17:36:01 +0800 Subject: [PATCH] Revert "Adds `once` memoization function (#550)" This reverts commit dd09c55010281dc0ed82ab0f116397e086af5d68. --- src/Functions.php | 18 --------- src/Once.php | 98 ----------------------------------------------- src/Onceable.php | 85 ---------------------------------------- 3 files changed, 201 deletions(-) delete mode 100644 src/Once.php delete mode 100644 src/Onceable.php diff --git a/src/Functions.php b/src/Functions.php index 7202613..6917a06 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -58,21 +58,3 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) goto beginning; } } - -/** - * Ensures a callable is only called once, and returns the result on subsequent calls. - * - * @template TReturnType - * - * @param callable(): TReturnType $callback - * @return TReturnType - */ -function once(callable $callback) -{ - $onceable = Onceable::tryFromTrace( - debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2), - $callback, - ); - - return $onceable ? Once::instance()->value($onceable) : call_user_func($callback); -} diff --git a/src/Once.php b/src/Once.php deleted file mode 100644 index cbd5136..0000000 --- a/src/Once.php +++ /dev/null @@ -1,98 +0,0 @@ -> $values - */ - protected function __construct(protected WeakMap $values) - { - } - - /** - * Create a new once instance. - * - * @return static - */ - public static function instance() - { - return static::$instance ??= new static(new WeakMap()); - } - - /** - * Get the value of the given onceable. - * - * @return mixed - */ - public function value(Onceable $onceable) - { - if (! static::$enabled) { - return call_user_func($onceable->callable); - } - - $object = $onceable->object ?: $this; - - $hash = $onceable->hash; - - if (isset($this->values[$object][$hash])) { - return $this->values[$object][$hash]; - } - - if (! isset($this->values[$object])) { - $this->values[$object] = []; - } - - return $this->values[$object][$hash] = call_user_func($onceable->callable); - } - - /** - * Re-enable the once instance if it was disabled. - */ - public static function enable() - { - static::$enabled = true; - } - - /** - * Disable the once instance. - */ - public static function disable() - { - static::$enabled = false; - } - - /** - * Flush the once instance. - */ - public static function flush() - { - static::$instance = null; - } -} diff --git a/src/Onceable.php b/src/Onceable.php deleted file mode 100644 index 734ab8e..0000000 --- a/src/Onceable.php +++ /dev/null @@ -1,85 +0,0 @@ -> $trace - * @return static|null - */ - public static function tryFromTrace(array $trace, callable $callable) - { - if (! is_null($hash = static::hashFromTrace($trace, $callable))) { - $object = static::objectFromTrace($trace); - - return new static($hash, $object, $callable); - } - - return null; - } - - /** - * Computes the object of the onceable from the given trace, if any. - * - * @param array> $trace - * @return object|null - */ - protected static function objectFromTrace(array $trace) - { - return $trace[1]['object'] ?? null; - } - - /** - * Computes the hash of the onceable from the given trace. - * - * @param array> $trace - * @return string|null - */ - protected static function hashFromTrace(array $trace, callable $callable) - { - if (str_contains($trace[0]['file'] ?? '', 'eval()\'d code')) { - return null; - } - - $uses = array_map( - fn (mixed $argument) => is_object($argument) ? spl_object_hash($argument) : $argument, - $callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [], - ); - - return md5(sprintf( - '%s@%s%s:%s (%s)', - $trace[0]['file'], - isset($trace[1]['class']) ? ($trace[1]['class'] . '@') : '', - $trace[1]['function'], - $trace[0]['line'], - serialize($uses), - )); - } -}