是否可以覆盖 symfony2 应用程序/控制台命令?例如,在 FOS UserBundle 中,我想添加更多字段,它会在使用控制台创建用户命令创建用户时询问它。这可能吗,还是我需要在自己的包中创建自己的控制台命令?
4 回答
向命令中添加更多字段的整个过程是:
1.在您的 AcmeDemoBundle 类中,您必须将 FOSUser 设置为父级:
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
2.一旦你这样做了,你可以在你的包中重新创建 CreateUserCommand:
<?php
namespace Acme\UserBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use FOS\UserBundle\Model\User;
/**
* @author Matthieu Bontemps <matthieu@knplabs.com>
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Luis Cordova <cordoval@gmail.com>
*/
class CreateUserCommand extends ContainerAwareCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('fos:user:create')
->setDescription('Create a user.')
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('email', InputArgument::REQUIRED, 'The email'),
new InputArgument('password', InputArgument::REQUIRED, 'The password'),
new InputArgument('name', InputArgument::REQUIRED, 'The name'),
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
))
->setHelp(<<<EOT
The <info>fos:user:create</info> command creates a user:
<info>php app/console fos:user:create matthieu</info>
This interactive shell will ask you for an email and then a password.
You can alternatively specify the email and password as the second and third arguments:
<info>php app/console fos:user:create matthieu matthieu@example.com mypassword</info>
You can create a super admin via the super-admin flag:
<info>php app/console fos:user:create admin --super-admin</info>
You can create an inactive user (will not be able to log in):
<info>php app/console fos:user:create thibault --inactive</info>
EOT
);
}
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$name = $input->getArgument('name');
$inactive = $input->getOption('inactive');
$superadmin = $input->getOption('super-admin');
$manipulator = $this->getContainer()->get('acme.util.user_manipulator');
$manipulator->create($username, $password, $email, $name, !$inactive, $superadmin);
$output->writeln(sprintf('Created user <comment>%s</comment>', $username));
}
/**
* @see Command
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('username')) {
$username = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a username:',
function($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
}
);
$input->setArgument('username', $username);
}
if (!$input->getArgument('email')) {
$email = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose an email:',
function($email) {
if (empty($email)) {
throw new \Exception('Email can not be empty');
}
return $email;
}
);
$input->setArgument('email', $email);
}
if (!$input->getArgument('password')) {
$password = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a password:',
function($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}
return $password;
}
);
$input->setArgument('password', $password);
}
if (!$input->getArgument('name')) {
$name = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a name:',
function($name) {
if (empty($name)) {
throw new \Exception('Name can not be empty');
}
return $name;
}
);
$input->setArgument('name', $name);
}
}
}
注意我添加了一个名为 name 的新输入参数,在命令中我使用的是 acme.util.user_manipulator 服务而不是原来的操作系统,我还将处理用户名。
3.创建自己的UserManipulator:
<?php
namespace Acme\UserBundle\Util;
use FOS\UserBundle\Model\UserManagerInterface;
/**
* Executes some manipulations on the users
*
* @author Christophe Coevoet <stof@notk.org>
* @author Luis Cordova <cordoval@gmail.com>
*/
class UserManipulator
{
/**
* User manager
*
* @var UserManagerInterface
*/
private $userManager;
public function __construct(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}
/**
* Creates a user and returns it.
*
* @param string $username
* @param string $password
* @param string $email
* @param string $name
* @param Boolean $active
* @param Boolean $superadmin
*
* @return \FOS\UserBundle\Model\UserInterface
*/
public function create($username, $password, $email, $name, $active, $superadmin)
{
$user = $this->userManager->createUser();
$user->setUsername($username);
$user->setEmail($email);
$user->setName($name);
$user->setPlainPassword($password);
$user->setEnabled((Boolean)$active);
$user->setSuperAdmin((Boolean)$superadmin);
$this->userManager->updateUser($user);
return $user;
}
}
在这个类中,我只需要创建函数,因此其他命令(如提升、降级..)不知道您的用户的新属性,因此我不需要创建 CompilerPass 来覆盖整个服务。
4.最后,在 Resources/config 目录中定义这个新的 UserManipulator 服务,并将其添加到 DependencyInjection Extension 中:
services:
acme.util.user_manipulator:
class: Acme\UserBundle\Util\UserManipulator
arguments: [@fos_user.user_manager]
完毕!!!
Symfony 4移除了包继承,但你仍然可以通过装饰命令来覆盖它们。它更干净,不需要捆绑。只需将其添加到services.yaml
:
services:
App\Command\MyCustomCommand:
decorates: command_you_want_to_override
请注意,这command_you_want_to_override
是一个服务名称。对于旧的,Symfony 3.3 之前的命令,它将是带有点和下划线的小写字母(例如。doctrine_migrations.diff_command
),对于较新的命令,它将是一个类名。您可以通过检查其捆绑包的服务配置或搜索debug:container
. 在 Linux 或 macOS 上,它将是:
php bin/console debug:container | grep Command
如果您创建(或已经拥有)您自己的作为该捆绑包子的捆绑包(请参阅捆绑包继承),您可以覆盖捆绑包的控制台命令。然后通过在你的包中放置一个与原始命令具有相同位置/名称的类,你可以有效地覆盖它。
例如,要覆盖 FOS/UserBundle/Command/CreateUserCommand.php,创建 MyCompany/UserBundle/Command/CreateUserCommand,其中 MyCompanyUserBundle 将 FOSUserBundle 作为其父级。
您的命令类可以扩展 FOS 命令类以重用(部分)它。但是,查看 FOS CreateUserCommand 后,我认为您需要覆盖所有方法以添加更多输入字段,在这种情况下,这样做没有任何好处。当然,这也意味着您可以在任何包中创建自己的命令,但我认为最好将 FOSUserBundle 的自定义保留在子包中。
在 Symfony (3.3)中,您可以通过以下链接覆盖控制台命令。 https://symfony.com/doc/current/console/calling_commands.html和https://symfony.com/doc/current/console/input.html 上的选项
来自 symfony 文档的代码:
use Symfony\Component\Console\Input\ArrayInput;
// ...
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
'command' => 'demo:greet',
'name' => 'Fabien',
'--yell' => true,
);
$greetInput = new ArrayInput($arguments);
$returnCode = $command->run($greetInput, $output);
// ...
}