我有一个Symfony控制台命令,需要调用setter。 设置程序在命令的服务定义的“父”定义中指定。设置员永远不会被调用。
注意:这里显示的特定实现是更为复杂的实际实现的简化版本。这个简单的版本也存在相同的问题。
配置文件:
# config/commands.yaml
services:
AppCommandTemplate:
abstract: true
public: false
calls:
- [initCommand, ['@logger']]
App\Command\XyzzyCommand:
parent: AppCommandTemplate
tags: [console.command]
命令源:
<?php
// src/Command/XyzzyCommand.php
namespace App\Command;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class XyzzyCommand extends Command
{
protected static $defaultName = 'app:xyzzy';
protected $logger;
protected function configure(): void
{
$this->setDescription( 'Test command' );
} // configure()
protected function execute( InputInterface $in, OutputInterface $out )
{
$out->writeln( 'This is ' . __METHOD__ );
$out->writeln( 'logger is a ' . get_class( $this->logger ));
} // execute()
//
// This function is never called.
//
public function initXyzzyCommand( LoggerInterface $logger )
{
$this->logger = $logger;
} // initXyzzyCommand()
} // XyzzyCommand{}
控制台debug:container命令的输出不显示呼叫。
app> console debug:container App\Command\XyzzyCommand
Information for Service "App\Command\XyzzyCommand"
==================================================
---------------- --------------------------
Option Value
---------------- --------------------------
Service ID App\Command\XyzzyCommand
Class App\Command\XyzzyCommand
Tags console.command
Calls setName <-- This line should include initCommand
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
---------------- --------------------------
环境:
-Mac OS 10.13.6(High Sierra)
-自酿1.8.6
-PHP 7.2.6
-Symfony 4.2.2