是否可以覆盖symfony2 app / console命令?例如,在FOS UserBundle中,我想添加更多的字段,它会在使用其控制台创建用户命令创建用户时询问.这是可能的,还是需要在自己的捆绑包中创建自己的控制台命令?
为命令添加更多字段的整个过程是:

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','The email'),new InputArgument('password','The password'),new InputArgument('name','The name'),new Inputoption('super-admin',null,Inputoption::VALUE_NONE,'Set the user as super admin'),new Inputoption('inactive','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服务,而不是原来的一个os,我也要处理用户的名字.

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,$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函数,所以其他的命令,如promotion,demote ..不知道你的用户的新属性,所以我不需要创建一个CompilerPass来覆盖整个服务.

4.最后,在Resources / config目录中定义新的UserManipulator服务,并将其添加到DependencyInjection扩展中:

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

完成!

php – 覆盖symfony2控制台命令?的更多相关文章

  1. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  2. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. 使用placeholder属性设置input文本框的提示信息

    这篇文章主要介绍了使用placeholder属性设置input文本框的提示信息,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  4. Bootstrap File Input文件上传组件

    这篇文章主要介绍了Bootstrap File Input文件上传组件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. HTML5中input输入框默认提示文字向左向右移动的示例代码

    这篇文章主要介绍了HTML5中input输入框默认提示文字向左向右移动,本文通过实例代码给大家介绍的非常详细对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. ios – 如何将PhoneGap调试控制台与CLI集成?

    我需要在config.xml中添加任何内容吗?

  7. 无法使用xCode 4.4启动控制台应用程序

    我有一个包含两个目标的项目–一个iOS应用程序和一个OSX控制台应用程序.后者是使用XcodeFile->NewTarget并选择“CommandLineTool”创建的.此控制台应用程序用于准备iOS应用程序所需的默认数据库–使用CoreData.这一直很好,直到我升级到MountainLion和xCode4.4.现在,当我尝试运行命令行工具时,我收到“无法启动–权限被拒绝”错误.我试过玩签名证

  8. ios – 从Live Photo中提取视频部分

    有没有人想出如何从LivePhoto中提取视频部分?

  9. ios – 使用Swift的Lumberjack 2.0记录器

    我以前使用物镜C的Lumberjack记录器,我喜欢它.现在我开始学习Swift,我不能在那里使用我最喜欢的记录器.有人可以一步一步地写出我能做到的事吗?在Lumberjack2.0发布之前,我尝试在这里找到一些东西,但所有主题都是自定义包装器.我做了什么:>我用Cocoapods添加了Lumberjack;>我将“#import”添加到Bridging-Header文件中.我不知道接下来该怎么办?因为在ObjC中我有宏:staticconstintddLogLevel=LOG_LEVEL_INFO;el

  10. 使用XCode进行调试时如何生成SIGINT?

    我的控制台应用程序捕获SIGINT,以便它可以正常退出.但是,在调试程序时按XCode中的CTRLC无效.我可以找到进程并使用终端窗口向我的进程发送SIGINT,但是我希望有一个更简单的解决方案,我可以在XCode中完成.解决方法调试器控制台的暂停按钮实际上会向您的应用发送SIGINT.如果您想让调试器将信号传递给您的应用程序,您可以执行以下操作:>按调试器的暂停按钮,等待调试控制台获得焦点>键入

随机推荐

  1. PHP个人网站架设连环讲(一)

    先下一个OmnihttpdProffesinalV2.06,装上就有PHP4beta3可以用了。PHP4给我们带来一个简单的方法,就是使用SESSION(会话)级变量。但是如果不是PHP4又该怎么办?我们可以假设某人在15分钟以内对你的网页的请求都不属于一个新的人次,这样你可以做个计数的过程存在INC里,在每一个页面引用,访客第一次进入时将访问时间送到cookie里。以后每个页面被访问时都检查cookie上次访问时间值。

  2. PHP函数学习之PHP函数点评

    PHP函数使用说明,应用举例,精简点评,希望对您学习php有所帮助

  3. ecshop2.7.3 在php5.4下的各种错误问题处理

    将方法内的函数,分拆为2个部分。这个和gd库没有一点关系,是ecshop程序的问题。会出现这种问题,不外乎就是当前会员的session或者程序对cookie的处理存在漏洞。进过本地测试,includes\modules\integrates\ecshop.php这个整合自身会员的类中没有重写integrate.php中的check_cookie()方法导致,验证cookie时返回的username为空,丢失了登录状态,在ecshop.php中重写了此方法就可以了。把他加到ecshop.php的最后面去就可

  4. NT IIS下用ODBC连接数据库

    $connection=intodbc_connect建立数据库连接,$query_string="查询记录的条件"如:$query_string="select*fromtable"用$cur=intodbc_exec检索数据库,将记录集放入$cur变量中。再用while{$var1=odbc_result;$var2=odbc_result;...}读取odbc_exec()返回的数据集$cur。最后是odbc_close关闭数据库的连接。odbc_result()函数是取当前记录的指定字段值。

  5. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  6. zen_cart实现支付前生成订单的方法

    这篇文章主要介绍了zen_cart实现支付前生成订单的方法,结合实例形式详细分析了zen_cart支付前生成订单的具体步骤与相关实现技巧,需要的朋友可以参考下

  7. Thinkphp5框架实现获取数据库数据到视图的方法

    这篇文章主要介绍了Thinkphp5框架实现获取数据库数据到视图的方法,涉及thinkPHP5数据库配置、读取、模型操作及视图调用相关操作技巧,需要的朋友可以参考下

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 基于win2003虚拟机中apache服务器的访问

    下面小编就为大家带来一篇基于win2003虚拟机中apache服务器的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. Yii2中组件的注册与创建方法

    这篇文章主要介绍了Yii2之组件的注册与创建的实现方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下

返回
顶部