是否可以重寫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