[php-src] Issue #14031: [ HELP REQUEST ] Continue already running generator to implement middleware flow

From: Date: Sun, 21 Apr 2024 20:33:03 +0000
Subject: [php-src] Issue #14031: [ HELP REQUEST ] Continue already running generator to implement middleware flow
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-246800@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/14031 Author: 6562680 ### Description Sorry, i just dont know where to put that question, guess only strong devs could help me with. The following code: ```php <?php function gen($fnMiddleware) { global $gen; global $i; yield $i++; var_dump('--1'); yield from $fnMiddleware($gen); yield $i++; var_dump('--4'); return $i; } $fnMiddleware = function (\Generator $gen) { global $i; $i++; var_dump('--2'); $getReturn = yield from $gen; $i++; var_dump('--3'); return $getReturn; }; global $gen; global $i; $gen = gen($fnMiddleware); $i = 0; foreach ( $gen as $key => $value ) { var_dump([ $key, $value ]); $gen->next(); } var_dump($gen->getReturn()); ``` Gives the output: ``` // > Output: // array(2) { // [0]=> // int(0) // [1]=> // int(0) // } // string(3) "--1" // string(3) "--2" // Fatal error: Uncaught Error: Impossible to yield from the Generator being currently run in ~/test.php:24 ``` I know that that is correct exception. But whats wrong i did understand in Generators idea... How to correctly implement middleware stuff with generators? Should there be some ->send('signal_name') support or like that? ### PHP Version PHP 7.2 ### Operating System Windows 10

« previous php.bugs (#246800) next »