From 3a68f6997067248daa7b6e3e20587b2d1eb99343 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Thu, 9 Mar 2017 17:00:17 +0100 Subject: [PATCH] Remove Loop::execute scoping This allows using `Loop::run();` instead of putting the entire application into a closure. A default loop can be setup using Composer's `"files"` autoload feature. Scoping can be implemented on top by testing frameworks or other libraries. Closes #148. --- src/Loop.php | 84 +++++--------------------------------- src/Loop/DriverFactory.php | 18 -------- test/DummyDriver.php | 48 ---------------------- test/LoopStateTest.php | 1 + test/LoopTest.php | 46 --------------------- 5 files changed, 11 insertions(+), 186 deletions(-) delete mode 100644 src/Loop/DriverFactory.php delete mode 100644 test/DummyDriver.php delete mode 100644 test/LoopTest.php diff --git a/src/Loop.php b/src/Loop.php index 27ddf28..2525cf8 100644 --- a/src/Loop.php +++ b/src/Loop.php @@ -3,7 +3,6 @@ namespace AsyncInterop; use AsyncInterop\Loop\Driver; -use AsyncInterop\Loop\DriverFactory; use AsyncInterop\Loop\InvalidWatcherException; use AsyncInterop\Loop\UnsupportedFeatureException; @@ -14,42 +13,19 @@ */ final class Loop { - /** - * @var DriverFactory - */ - private static $factory = null; - /** * @var Driver */ private static $driver = null; /** - * @var int - */ - private static $level = 0; - - /** - * Set the factory to be used to create a default drivers. - * - * Setting a factory is only allowed as long as no loop is currently running. Passing null will reset the - * default driver and remove the factory. + * Sets the driver to be used. * - * The factory will be invoked if none is passed to `Loop::execute`. A default driver will be created to - * support synchronous waits in traditional applications. - * - * @param DriverFactory|null $factory New factory to replace the previous one. + * @param Driver $driver New driver to replace the previous one if one exists. */ - public static function setFactory(DriverFactory $factory = null) + public static function set(Driver $driver) { - if (self::$level > 0) { - throw new \RuntimeException("Setting a new factory while running isn't allowed!"); - } - - self::$factory = $factory; - - // reset it here, it will be actually instantiated inside execute() or get() - self::$driver = null; + self::$driver = $driver; } /** @@ -59,50 +35,14 @@ public static function setFactory(DriverFactory $factory = null) * exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an * error handler or exceptions that would be passed to an error handler but none exists to handle them. * - * @param callable $callback The callback to execute. - * @param Driver $driver The event loop driver. If `null`, a new one is created from the set factory. - * * @return void * * @see \AsyncInterop\Loop::setFactory() */ - public static function execute(callable $callback, Driver $driver = null) - { - $previousDriver = self::$driver; - - self::$driver = $driver ?: self::createDriver(); - self::$level++; - - try { - self::$driver->defer($callback); - self::$driver->run(); - } finally { - self::$driver = $previousDriver; - self::$level--; - } - } - - /** - * Create a new driver if a factory is present, otherwise throw. - * - * @return Driver - * - * @throws \Exception If no factory is set or no driver returned from factory. - */ - private static function createDriver() + public static function run() { - if (self::$factory === null) { - throw new \Exception("No loop driver factory set; Either pass a driver to Loop::execute or set a factory."); - } - - $driver = self::$factory->create(); - - if (!$driver instanceof Driver) { - $type = is_object($driver) ? "an instance of " . get_class($driver) : gettype($driver); - throw new \Exception("Loop driver factory returned {$type}, but must return an instance of Driver."); - } - - return $driver; + $driver = self::$driver ?: self::get(); + $driver->run(); } /** @@ -112,11 +52,7 @@ private static function createDriver() */ public static function get() { - if (self::$driver) { - return self::$driver; - } - - return self::$driver = self::createDriver(); + return self::$driver; } /** @@ -170,10 +106,10 @@ public static function defer(callable $callback, $data = null) * * @return string An unique identifier that can be used to cancel, enable or disable the watcher. */ - public static function delay($time, callable $callback, $data = null) + public static function delay($delay, callable $callback, $data = null) { $driver = self::$driver ?: self::get(); - return $driver->delay($time, $callback, $data); + return $driver->delay($delay, $callback, $data); } /** diff --git a/src/Loop/DriverFactory.php b/src/Loop/DriverFactory.php deleted file mode 100644 index bb4d992..0000000 --- a/src/Loop/DriverFactory.php +++ /dev/null @@ -1,18 +0,0 @@ -defers)) { - try { - $defer(self::$id++, $data); - } catch (\Exception $e) { - if ($handler = $this->handler) { - $handler($e); - } else { - throw $e; - } - } - } - } - - public function defer(callable $callback, $data = null) { - $this->defers[] = [$callback, $data]; - } - - public function setErrorHandler(callable $callback = null) { - $this->handler = $callback; - } - - public function stop() {} - public function delay($delay, callable $callback, $data = null) { return self::$id++; } - public function repeat($interval, callable $callback, $data = null) { return self::$id++; } - public function onReadable($stream, callable $callback, $data = null) { return self::$id++; } - public function onWritable($stream, callable $callback, $data = null) { return self::$id++; } - public function onSignal($signo, callable $callback, $data = null) { return self::$id++; } - public function enable($watcherId) {} - public function disable($watcherId) {} - public function cancel($watcherId) {} - public function reference($watcherId) {} - public function unreference($watcherId) {} - public function getInfo() {} - public function getHandle() {} -} diff --git a/test/LoopStateTest.php b/test/LoopStateTest.php index 1dc2d9f..95358dd 100644 --- a/test/LoopStateTest.php +++ b/test/LoopStateTest.php @@ -4,6 +4,7 @@ class LoopStateTest extends \PHPUnit_Framework_TestCase { + /** @var Driver */ private $loop; protected function setUp() diff --git a/test/LoopTest.php b/test/LoopTest.php deleted file mode 100644 index e5d80b4..0000000 --- a/test/LoopTest.php +++ /dev/null @@ -1,46 +0,0 @@ -getMockBuilder(Loop\DriverFactory::class)->getMock(); - $factory->method("create")->willReturn($driver); - - Loop::setFactory($factory); - - Loop::execute(function () use ($factory) { - Loop::setFactory($factory); - }); - } - - /** @test */ - public function executeStackReturnsScopedDriver() { - $driver1 = new DummyDriver; - $driver2 = new DummyDriver; - - Loop::execute(function () use ($driver1, $driver2) { - $this->assertSame($driver1, Loop::get()); - - Loop::execute(function () use ($driver2) { - $this->assertSame($driver2, Loop::get()); - }, $driver2); - - $this->assertSame($driver1, Loop::get()); - }, $driver1); - } -}