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

Breadcrumb

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

class DriverFactory

Hierarchy

  • class \Revolt\EventLoop\DriverFactory

Expanded class hierarchy of DriverFactory

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

File

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

Namespace

Revolt\EventLoop
View source
final class DriverFactory {
    
    /**
     * Creates a new loop instance and chooses the best available driver.
     *
     * @return Driver
     *
     * @throws \Error If an invalid class has been specified via REVOLT_LOOP_DRIVER
     */
    public function create() : Driver {
        $driver = (function () {
            if ($driver = $this->createDriverFromEnv()) {
                return $driver;
            }
            if (UvDriver::isSupported()) {
                return new UvDriver();
            }
            if (EvDriver::isSupported()) {
                return new EvDriver();
            }
            if (EventDriver::isSupported()) {
                return new EventDriver();
            }
            return new StreamSelectDriver();
        })();
        if (\getenv("REVOLT_DRIVER_DEBUG_TRACE")) {
            return new TracingDriver($driver);
        }
        return $driver;
    }
    
    /**
     * @return Driver|null
     */
    private function createDriverFromEnv() : ?Driver {
        $driver = \getenv("REVOLT_DRIVER");
        if (!$driver) {
            return null;
        }
        if (!\class_exists($driver)) {
            throw new \Error(\sprintf("Driver '%s' does not exist.", $driver));
        }
        if (!\is_subclass_of($driver, Driver::class)) {
            throw new \Error(\sprintf("Driver '%s' is not a subclass of '%s'.", $driver, Driver::class));
        }
        return new $driver();
    }

}

Members

Title Sort descending Modifiers Object type Summary
DriverFactory::create public function Creates a new loop instance and chooses the best available driver.
DriverFactory::createDriverFromEnv private function
RSS feed
Powered by Drupal