diff --git a/src/Utils/Coroutine/Coroutine.php b/src/Utils/Coroutine/Coroutine.php index 6c4fe55..b2a1622 100644 --- a/src/Utils/Coroutine/Coroutine.php +++ b/src/Utils/Coroutine/Coroutine.php @@ -18,6 +18,7 @@ /** * @method mixed origin() + * @method string|int id() */ class Coroutine { @@ -42,15 +43,18 @@ class Coroutine /** * 构造方法 - */ - /** + * * @param Closure $func + * @link CoroutineInterface::__construct */ public function __construct(Closure $func) { $this->_interface = new (self::$_handlers[Factory::getCurrentEventLoop()] ?? DefaultCoroutine::class)($func); } + /** + * 析构 + */ public function __destruct() { $this->_interface = null; diff --git a/src/Utils/Coroutine/Handlers/CoroutineInterface.php b/src/Utils/Coroutine/Handlers/CoroutineInterface.php index d71520d..24cf16b 100644 --- a/src/Utils/Coroutine/Handlers/CoroutineInterface.php +++ b/src/Utils/Coroutine/Handlers/CoroutineInterface.php @@ -11,6 +11,8 @@ interface CoroutineInterface { /** * 创建协程 + * + * @param \Closure $func = function (mixed $coroutineId) {} */ public function __construct(\Closure $func); diff --git a/src/Utils/Coroutine/Handlers/DefaultCoroutine.php b/src/Utils/Coroutine/Handlers/DefaultCoroutine.php index 0f9db3f..156191b 100644 --- a/src/Utils/Coroutine/Handlers/DefaultCoroutine.php +++ b/src/Utils/Coroutine/Handlers/DefaultCoroutine.php @@ -18,8 +18,8 @@ class DefaultCoroutine implements CoroutineInterface /** @inheritdoc */ public function __construct(\Closure $func) { - call_user_func($func); - $this->id = spl_object_hash($func); + call_user_func($func, $id = spl_object_hash($func)); + $this->id = $id; } /** @inheritdoc */ diff --git a/src/Utils/Coroutine/Handlers/RippleCoroutine.php b/src/Utils/Coroutine/Handlers/RippleCoroutine.php index 38aae4a..b0bb029 100644 --- a/src/Utils/Coroutine/Handlers/RippleCoroutine.php +++ b/src/Utils/Coroutine/Handlers/RippleCoroutine.php @@ -27,9 +27,9 @@ class RippleCoroutine implements CoroutineInterface */ public function __construct(\Closure $func) { - $this->_promise = $this->_async(function () use (&$promise, $func) { + $this->_promise = $this->_async(function () use ($func) { try { - call_user_func($func); + call_user_func($func, $this->_id); } finally { // 移除协程id及promise $this->_promise = null; diff --git a/src/Utils/Coroutine/Handlers/SwooleCoroutine.php b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php index 4d9c3d8..5278a9f 100644 --- a/src/Utils/Coroutine/Handlers/SwooleCoroutine.php +++ b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php @@ -22,8 +22,10 @@ class SwooleCoroutine implements CoroutineInterface public function __construct(\Closure $func) { while (1) { - if ($res = Coroutine::create($func)) { - $this->_id = $res; + if ($id = Coroutine::create(function () use ($func) { + call_user_func($func, $this->_id); + })) { + $this->_id = $id; break; } } diff --git a/src/Utils/Coroutine/Handlers/SwowCoroutine.php b/src/Utils/Coroutine/Handlers/SwowCoroutine.php index af235e8..8e90dd8 100644 --- a/src/Utils/Coroutine/Handlers/SwowCoroutine.php +++ b/src/Utils/Coroutine/Handlers/SwowCoroutine.php @@ -21,12 +21,12 @@ class SwowCoroutine implements CoroutineInterface */ protected int $_id; - /** @inheritdoc - * @param \Closure $func - */ + /** @inheritDoc */ public function __construct(\Closure $func) { - $this->_coroutine = Coroutine::run($func); + $this->_coroutine = Coroutine::run(function () use ($func) { + call_user_func($func, $this->_id); + }); $this->_id = $this->_coroutine->getId(); } diff --git a/tests/UtilsCase/Coroutine/DefaultCoroutineTest.php b/tests/UtilsCase/Coroutine/DefaultCoroutineTest.php index 25b5c26..2420512 100644 --- a/tests/UtilsCase/Coroutine/DefaultCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/DefaultCoroutineTest.php @@ -12,12 +12,15 @@ class DefaultCoroutineTest extends TestCase public function testConstruct() { $executed = false; - $func = function() use (&$executed) { + $id = null; + $func = function ($coroutineId) use (&$id, &$executed) { $executed = true; + $id = $coroutineId; }; $coroutine = new DefaultCoroutine($func); $this->assertTrue($executed); $this->assertEquals(spl_object_hash($func), $coroutine->id()); + $this->assertEquals($coroutine->id(), $id); } public function testOrigin() diff --git a/tests/UtilsCase/Coroutine/RippleCoroutineTest.php b/tests/UtilsCase/Coroutine/RippleCoroutineTest.php index e0823ca..4dc9f61 100644 --- a/tests/UtilsCase/Coroutine/RippleCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/RippleCoroutineTest.php @@ -18,8 +18,10 @@ protected function tearDown(): void public function testConstruct() { $executed = false; - $func = function() use (&$executed) { + $id = null; + $func = function ($coroutineId) use (&$id, &$executed) { $executed = true; + $id = $coroutineId; }; $promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise'); @@ -37,6 +39,8 @@ public function testConstruct() $this->assertTrue($executed); $this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin()); $this->assertIsString($coroutine->id()); + $this->assertEquals(spl_object_hash($promiseMock), $coroutine->id()); + $this->assertEquals($coroutine->id(), $id); } public function testDestruct() diff --git a/tests/UtilsCase/Coroutine/SwooleCoroutineTest.php b/tests/UtilsCase/Coroutine/SwooleCoroutineTest.php index 23a8911..bec560b 100644 --- a/tests/UtilsCase/Coroutine/SwooleCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/SwooleCoroutineTest.php @@ -19,8 +19,10 @@ protected function tearDown(): void public function testConstruct() { $executed = false; - $func = function() use (&$executed) { + $id = null; + $func = function ($coroutineId) use (&$id, &$executed) { $executed = true; + $id = $coroutineId; }; $coroutineMock = Mockery::mock('alias:Swoole\Coroutine'); @@ -34,6 +36,7 @@ public function testConstruct() $this->assertTrue($executed); $this->assertEquals(123, $coroutine->origin()); $this->assertEquals(123, $coroutine->id()); + $this->assertEquals(123, $id()); } public function testDestruct() diff --git a/tests/UtilsCase/Coroutine/SwowCoroutineTest.php b/tests/UtilsCase/Coroutine/SwowCoroutineTest.php index b2f9c9b..fb2fd9c 100644 --- a/tests/UtilsCase/Coroutine/SwowCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/SwowCoroutineTest.php @@ -19,8 +19,10 @@ protected function tearDown(): void public function testConstruct() { $executed = false; - $func = function() use (&$executed) { + $id = null; + $func = function ($coroutineId) use (&$id, &$executed) { $executed = true; + $id = $coroutineId; }; $coroutineMock = Mockery::mock('alias:Swow\Coroutine'); @@ -35,6 +37,7 @@ public function testConstruct() $this->assertTrue($executed); $this->assertInstanceOf('Swow\Coroutine', $coroutine->origin()); $this->assertEquals(123, $coroutine->id()); + $this->assertEquals(123, $id); } public function testDestruct()