问题描述
我正在使用控制台命令 Symfony 进行 http 调用。
快速示例工作:
private void insertFixUp(Node x) {
Node u = TNULL;
while (x.parent.color == 1){
if(x.parent == x.parent.parent.right){
u=x.parent.parent.left;
if (u.color == 1){
u.color =0;
x.parent.color = 0;
x.parent.parent.color = 1;
x = x.parent.parent;
}
else {
if(x == x.parent.left) {
x = x.parent;
rightRotate(x);
}
x.parent.color=0;
x.parent.parent.color=1;
leftRotate(x.parent.parent);
}
} else {
u = x.parent.parent.right;
if (u.color == 1){
u.color =0;
x.parent.color = 0;
x.parent.parent.color = 1;
x = x.parent.parent;
}
else {
if(x == x.parent.right) {
x = x.parent;
leftRotate(x);
}
x.parent.color=0;
x.parent.parent.color=1;
leftRotate(x.parent.parent);
}
}
if(x == root){
break;
}
}
root.color=0;
}
<?PHP
namespace \App\Command;
use Symfony\Component\Console\Command\Command;
class MainCommand extends Command {
public function __construct(
CallApiService $api,TekConfiguration $parserConfig,LoggerInterface $logger,ParameterBagInterface $param
) {
parent::__construct();
$this->api = $api;
$this->logger = $logger;
$this->parserConfig = $parserConfig;
$this->param = $param;
}
}
<?PHP
namespace \App\Command;
class FistCommand extends MainCommand{
protected static $defaultName = 'tek:foo:bar';
protected function configure(): void
{
...
}
protected function execute(InputInterface $input,OutputInterface $output)
{
...
$this->api->callFoo();
...
}
}
为了更好地组织我的代码,我想在从 CallApiService 继承的其他类中外部化 callFoo(和 callBar、callAnother..)方法。 喜欢:
<?PHP
namespace App\Tek;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class CallApiService{
public function __construct(HttpClientInterface $tek_api,LoggerInterface $logger)
{
$this->client = $tek_api; //scoped cf. framework.yaml
$this->logger = $logger;
$this->path = $_ENV['API_URL'] . $_ENV['API_PATH'];
... other common conf ...
}
private function get(){
** do call httpclient get **
** and return response **
}
private function post(){
** do call httpclient post **
** and return response **
}
public function callFoo(array $params): array
{
$this->uri = $this->path . '/operations?operation=foo';
return $this->post($params);
}
但我不明白如何通过依赖注入避免重新声明构造函数
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)