Php Symfony 4自定义容器感知命令错误
我正在尝试在symfony4项目中创建自定义命令Php Symfony 4自定义容器感知命令错误,php,symfony,symfony4,Php,Symfony,Symfony4,我正在尝试在symfony4项目中创建自定义命令 class AppOauthClientCreateCommand extends ContainerAwareCommand { protected function configure() { $this ->setName('app:oauth-client:create') ->setDescription('Create a new OAuth client');
class AppOauthClientCreateCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('app:oauth-client:create')
->setDescription('Create a new OAuth client');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setAllowedGrantTypes(array(OAuth2::GRANT_TYPE_USER_CREDENTIALS));
$clientManager->updateClient($client);
$output->writeln(sprintf('client_id=%s_%s', $client->getId(), $client->getRandomId()));
$output->writeln(sprintf('client_secret=%s', $client->getSecret()));
}
}
当我尝试运行此命令时,出现以下错误
编译容器时,fos_oauth_server.client_manager.default服务或别名已被删除或内联。你也应该
将其公开,或者停止直接使用容器,改用依赖项注入
如何公开供应商服务,或者我在命令配置中遗漏了什么?您需要检查./bin/console debug:autowiring命令的输出,以获得适当的接口,从而在构造函数中使用“fos\u oauth\u server.client\u manager.*”。应已设置自动布线,以允许容器识别并从那里插入
这将需要对SF4的FosOauthServer支持,并且在构造函数中记住也要调用parent::\u construct;以正确设置该命令。您可以这样做,但仍然可以使用ContainerWareCommand->从容器中获取其他内容,但随着时间的推移,您可能会远离它。您需要检查./bin/console debug:autowiring命令的输出,以获得适当的接口,以便在构造函数中使用“fos\u oauth\u server.client\u manager.*”。应已设置自动布线,以允许容器识别并从那里插入
这将需要对SF4的FosOauthServer支持,并且在构造函数中记住也要调用parent::\u construct;以正确设置该命令。您可以这样做,但仍然可以使用ContainerWareCommand->从容器中获取其他内容,但随着时间的推移,您可能会远离它。问题是,自Symfony 4以来,所有服务都是Deafult私有的。事实上,不可能使用服务容器的get方法获取私有服务 您应该通过扩展ContainerWareCommand来避免在服务或命令中注入整个contanier。相反,您应该只注入所需的服务:
class AppOauthClientCreateCommand
{
/**
* @var ClientManagerInterface
*/
private $clientManager;
public function __construct(ClientManagerInterface $clientManager)
{
$this->clientManager = $clientManager;
}
protected function configure()
{
...
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->clientManager->createClient();
...
}
}
如果ClientManagerInterface未自动连接,则必须在services.yaml中配置AppAuthClientCreateCommand,使其具有适当的依赖关系。比如:
services:
App\Command\AppOauthClientCreateCommand:
arguments:
$clientManager: "@fos_oauth_server.client_manager.default"
希望这能有所帮助。问题是,自Symfony 4以来,所有服务都是Deafult私有的。事实上,不可能使用服务容器的get方法获取私有服务 您应该通过扩展ContainerWareCommand来避免在服务或命令中注入整个contanier。相反,您应该只注入所需的服务:
class AppOauthClientCreateCommand
{
/**
* @var ClientManagerInterface
*/
private $clientManager;
public function __construct(ClientManagerInterface $clientManager)
{
$this->clientManager = $clientManager;
}
protected function configure()
{
...
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = $this->clientManager->createClient();
...
}
}
如果ClientManagerInterface未自动连接,则必须在services.yaml中配置AppAuthClientCreateCommand,使其具有适当的依赖关系。比如:
services:
App\Command\AppOauthClientCreateCommand:
arguments:
$clientManager: "@fos_oauth_server.client_manager.default"
希望这有帮助