Reputation: 18680
I am trying to build a Symfony 4.3.3 command where I want to add the ability for callback functions to be overwritten at any time. Look at the following code snippet:
namespace App\Command;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Server;
final class SwooleWsCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): bool
{
$port = $input->getArgument('port');
$server = new SwooleServer("127.0.0.1", (int)$port);
$server->on('request', static function (Request $request, Response $response) {
$response->end('Hello '.$request->rawcontent());
});
$server->start();
return true;
}
}
I want to convert this:
$server->on('request', static function (Request $request, Response $response) {
$response->end('Hello '.$request->rawcontent());
});
Into this (if it possible and it's not one crazy thing or impossible to achieve):
$server->on('request', <function>);
My idea, and I might be completely wrong, is to create an interface allowing the methods to be overwritten at any time. Below is a code snippet of it:
namespace App\Service;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
interface SwooleWsCallbackInterface
{
public function request(Request $request, Response $response): void;
public function message(Server $server, Frame $frame): void;
}
From there I should be able to inject the interface in the command, something like:
/** @var SwooleWsCallbackInterface */
private $callback;
public function __construct(SwooleWsCallbackInterface $callback)
{
$this->callback = $callback;
parent::__construct();
}
As you may notice the function request()
receives two parameters. For such reason once I invoke the method from the command as $server->on('request', $this->callback->request($request, $response));
it tries to access both parameters.
I have read Symfony Docs for How to Autowire Interfaces but still not clear to me how to properly set the parameters needed by the method to work.
If I am understanding the docs properly here:
App\Util\TransformerInterface: '@App\Util\Rot13Transformer'
They've already overwritten the methods at App\Util\Rot13Transformer
and should not have any problems. However the example does not show the method transform
from the interface TransformerInterface
being invoked.
Not sure if the proper autowiring on my scenario would be:
App\Command\SwooleWsCommand: ~
sdk.command.swoolews:
alias: App\Command\SwooleWsCommand
App\Service\SwooleWsCallbackInterface: ~
sdk.swoolews:
alias: App\Service\SwooleWsCallbackInterface
Can I get some ideas here for how to achieve this? And some explanation in how this works?
Upvotes: 3
Views: 202
Reputation: 641
You are invoking the function where you want to pass reference to the function instead. Try this:
$server->on('request', function ($request, $response) {
$this->callback->request($request, $response);
});
Upvotes: 1
Reputation: 36964
You still want to create a closure to pass to $server->on
. You can do as such with:
$onRequest = Closure::fromCallable([$this->callback, 'request']);
and then pass it:
$server->on('request', $onRequest);
Upvotes: 1