是否可以重写symfony2 app/console命令?例如,在FOS UserBundle中,我想添加几个字段,用它的控制台create user命令创建一个用户。这是可能的,还是我需要在我自己的包中创建自己的控制台命令?重写symfony2控制台命令?
回答
如果您创建(或已经拥有)您自己的捆绑包(该捆绑包的子项)(请参阅Bundle Inheritance),则可以覆盖捆绑包的控制台命令。然后,通过在您的包中放置一个类,并使用与原始命令相同的位置/名称,可以有效地覆盖它。例如,要覆盖FOS/UserBundle/Command/CreateUserCommand.php,创建MyCompany/UserBundle/Command/CreateUserCommand,其中MyCompanyUserBundle具有FOSUserBundle作为其父项。
您的命令类可以扩展FOS命令类以重新使用它的位(位)。然而,看过FOS CreateUserCommand,我认为你需要覆盖所有的方法来添加更多的输入字段,在这种情况下没有任何好处。当然,这也意味着你可以在任何包中创建自己的命令,但在我看来最好将FOSUserBundle的定制保存在子包中。
添加更多领域的命令,整个过程是:
1.In您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 <[email protected]>
* @author Thibault Duplessis <[email protected]>
* @author Luis Cordova <[email protected]>
*/
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 [email protected] 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);
}
}
}
注意我已经加入所谓的名称和命令里面我使用的是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 <[email protected]>
* @author Luis Cordova <co[email protected]>
*/
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.最后,在资源/ config目录定义这个新UserManipulator服务,并把它添加到DependencyInjection扩展:
services:
acme.util.user_manipulator:
class: Acme\UserBundle\Util\UserManipulator
arguments: [@fos_user.user_manager]
完成!
Hi @ nass600!感谢您的详细解答,但您是否也可以详细说明最后一部分:“将其添加到DependencyInjection扩展”。非常感谢 – Reveclair 2013-02-19 10:05:40
在Symfony (3.3)中,您可以通过关注这些链接来覆盖控制台命令。 https://symfony.com/doc/current/console/calling_commands.html 并从symfony的文档https://symfony.com/doc/current/console/input.html
代码的选项:
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);
// ...
}
- 1. Symfony2控制台命令
- 2. Symfony2自定义控制台命令
- 3. 无法测试Symfony2控制台命令
- 4. symfony2控制台的“空白”命令
- 5. 清理控制台命令
- 6. ReflectionException Laravel控制台命令
- 7. symfony2:如何在控制台命令中使用翻译器
- 8. 在Symfony2控制台命令中调试SQL查询
- 9. 从网络浏览器运行Symfony2控制台命令
- 10. 登录后运行控制台命令背景Symfony2
- 11. symfony2应用程序控制台:没有可用的命令
- 12. 如何删除symfony2中的控制台命令
- 13. 使用Symfony2中的进程组件调用控制台命令
- 14. 从控制器的Symfony3控制台运行控制台命令
- 15. Symfony2的控制器中调用控制台命令从供应商
- 16. 从控制台复制命令
- 17. symfony2控制台参数
- 18. 控制台写
- 19. 如何在Rails控制台中编写多行命令
- 20. 在控制台应用程序中编写cmd命令
- 21. 将操作系统命令的输出写入控制台
- 22. 编写脚本在控制台内运行命令
- 23. Grails。在控制台上执行命令
- 24. Heroku控制台命令错误
- 25. 监听控制台命令输出
- 26. Eclipse C++控制台打印命令linux
- 27. 没有控制台的C++ popen命令
- 28. 从控制台设置phpunit命令symfony
- 29. 停止Symfony的控制台命令
- 30. Java命令行控制台并切换
在这个问题上有用的东西。你应该标记一个答案是正确的;) – 2016-01-14 14:42:14