簡體   English   中英

覆蓋symfony2控制台命令?

[英]Override symfony2 console commands?

是否可以覆蓋symfony2 app / console命令? 例如,在FOS UserBundle中,我想添加一些字段,當它使用console create user命令創建用戶時會詢問它。 這是可能的,還是我需要在自己的捆綁包中創建自己的控制台命令?

向命令添加更多字段的整個過程是:

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;
    }
}

在這個類中我只需要create函數,所以其他命令如promote,demote ..不知道你的用戶的新屬性,所以我不需要創建一個CompilerPass來覆蓋整個服務。

4.最后,在Resources / config目錄中定義這個新的UserManipulator服務,並將其添加到DependencyInjection擴展:

services:
    acme.util.user_manipulator:
        class:      Acme\UserBundle\Util\UserManipulator
        arguments:  [@fos_user.user_manager]

完成!

如果您創建(或已經擁有)您自己的捆綁包(該捆綁包的子 ),則可以覆蓋捆綁包的控制台命令(請參閱捆綁包繼承 )。 然后,通過在您的包中放置一個與原始命令具有相同位置/名稱的類,您可以有效地覆蓋它。

因此,例如,要覆蓋FOS / UserBundle / Command / CreateUserCommand.php,請創建MyCompany / UserBundle / Command / CreateUserCommand,其中MyCompanyUserBundle具有FOSUserBundle作為其父級。

您的命令類可以擴展FOS命令類,以便重用它(位)。 但是,看了FOS CreateUserCommand后,我認為您需要覆蓋所有方法來添加更多輸入字段,在這種情況下,這樣做沒有任何好處。 當然這也意味着您可以在任何捆綁中創建自己的命令,但在我看來,最好將FOSUserBundle的自定義保存在子捆綁中。

Symfony 4已經刪除了bundle繼承 ,但你仍然可以通過修改它們來覆蓋命令。 它更干凈,不需要捆綁。 只需將其添加到services.yaml

services:
    App\Command\MyCustomCommand:
        decorates: command_you_want_to_override

請注意, command_you_want_to_override是服務名稱。 對於較早的Symfony 3.3命令,它將是帶小點和下划線的小寫字母(例如doctrine_migrations.diff_command ),對於較新的命令,它將是類名。 您可以通過檢查其bundle的服務配置或通過搜索debug:container輸出來找到此名稱。 在Linux或macOS上它將是:

php bin/console debug:container | grep Command

在Symfony (3.3)中,您可以通過以下鏈接覆蓋控制台命令。 https://symfony.com/doc/current/console/calling_commands.html並在選項https://symfony.com/doc/current/console/input.html

來自symfony doc的代碼:

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);

    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM