Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FiberLocal.php

class FiberLocal

Fiber local storage.

Each instance stores data separately for each fiber. Usage examples include contextual logging data.

@template T

Hierarchy

  • class \Revolt\EventLoop\FiberLocal

Expanded class hierarchy of FiberLocal

1 file declares its use of FiberLocal
AbstractDriver.php in vendor/revolt/event-loop/src/EventLoop/Internal/AbstractDriver.php

File

vendor/revolt/event-loop/src/EventLoop/FiberLocal.php, line 14

Namespace

Revolt\EventLoop
View source
final class FiberLocal {
    
    /** @var \Fiber|null Dummy fiber for {main} */
    private static ?\Fiber $mainFiber = null;
    private static ?\WeakMap $localStorage = null;
    public static function clear() : void {
        if (self::$localStorage === null) {
            return;
        }
        $fiber = \Fiber::getCurrent() ?? self::$mainFiber;
        if ($fiber === null) {
            return;
        }
        unset(self::$localStorage[$fiber]);
    }
    private static function getFiberStorage() : \WeakMap {
        $fiber = \Fiber::getCurrent();
        if ($fiber === null) {
            $fiber = self::$mainFiber ??= new \Fiber(static function () : void {
                // dummy fiber for main, as we need some object for the WeakMap
            });
        }
        $localStorage = self::$localStorage ??= new \WeakMap();
        return $localStorage[$fiber] ??= new \WeakMap();
    }
    
    /**
     * @param \Closure():T $initializer
     */
    public function __construct(\Closure $initializer) {
    }
    
    /**
     * @param T $value
     */
    public function set(mixed $value) : void {
        self::getFiberStorage()[$this] = [
            $value,
        ];
    }
    public function unset() : void {
        unset(self::getFiberStorage()[$this]);
    }
    
    /**
     * @return T
     */
    public function get() : mixed {
        $fiberStorage = self::getFiberStorage();
        if (!isset($fiberStorage[$this])) {
            $fiberStorage[$this] = [
                ($this->initializer)(),
            ];
        }
        return $fiberStorage[$this][0];
    }

}

Members

Title Sort descending Modifiers Object type Summary
FiberLocal::$localStorage private static property
FiberLocal::$mainFiber private static property @var \Fiber|null Dummy fiber for {main}
FiberLocal::clear public static function
FiberLocal::get public function
FiberLocal::getFiberStorage private static function
FiberLocal::set public function
FiberLocal::unset public function
FiberLocal::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal