use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // Create a new request object $request = Request::createFromGlobals(); // Get the user object from the database $user = User::find($request->get('user_id')); // Store the user object in the context $context = $request->attributes->get('_context', []); $context['user'] = $user; $request->attributes->set('_context', $context); // Render the response $response = new Response('Hello, ' . $user->name . '!'); $response->send();
use React\Promise\Deferred; use React\EventLoop\Factory; use Icicle\Coroutine\Coroutine; use Icicle\Context\Context; // Create a new event loop $loop = Factory::create(); // Create a new coroutine with an attached context $coroutine = new Coroutine(function (Context $context) { // Wait for a value to be set in the context $value = yield $context->wait('my_key'); echo $value; }); // Create a new deferred object $deferred = new Deferred(); // Set a value in the context and resolve the deferred object $deferred->resolve(function (Context $context) { $context->set('my_key', 'Hello, world!'); }); // Attach the context to the coroutine and run it on the event loop $coroutine->getContext()->attach($context); $loop->run();In this example, we create a new event loop and a new coroutine using the Icicle library. We also create a new context object and attach it to the coroutine. We then create a deferred object that resolves with a function that sets a value in the context. Finally, we attach the context to the coroutine and run it on the event loop. Package library: This example uses the Icicle library, which provides tools for working with asynchronous programming in PHP. The `Coroutine` and `Context` classes are part of the Icicle library.