Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ HELP REQUEST ] Continue already running generator to implement middleware flow #14031

Closed
6562680 opened this issue Apr 21, 2024 · 1 comment

Comments

@6562680
Copy link

6562680 commented Apr 21, 2024

Description

Sorry, i just dont know where to put that question, guess only strong devs could help me with.

The following code:

<?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?

Or maybe correct way is create function that could always create new generator from some $queue = []; and if i need to "continue" generator, actually create new one and send signal to break previous one replacing global variable to newly created?

PHP Version

PHP 7.2

Operating System

Windows 10

@damianwadley
Copy link
Member

Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you.

Thank you for your interest in PHP.

@damianwadley damianwadley closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants