0

I have been looking and trying to add a custom command to FOSUserBundle but have been impossible :(

I do triple check the structure of my files, CreateUserCommand is in Command folder and UserManipulator is in Util folder.

I also tried to relaunch the server and to change the command but is not working:

The command line

php app/console fos:user:create root test@example.com admin gabriel --super-admin

The error:

[RuntimeException]   
  Too many arguments. 

My Bundle main file:

<?php

namespace INCES\ComedorBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class INCESComedorBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

My CreateUserCommand File:

<?php

namespace INCES\ComedorBundle\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('nombre', InputArgument::REQUIRED, 'The nombre'),
                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
//..
            );
    }

    /**
     * @see Command
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $username   = $input->getArgument('username');
        $email      = $input->getArgument('email');
        $password   = $input->getArgument('password');
        $nombre     = $input->getArgument('nombre');
        $inactive   = $input->getOption('inactive');
        $superadmin = $input->getOption('super-admin');

        $manipulator = $this->getContainer()->get('inces_comedor.util.user_manipulator');
        $manipulator->create($username, $password, $email, $nombre, !$inactive, $superadmin);

        $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
    }

    /**
     * @see Command
     */
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        // ...

        if (!$input->getArgument('nombre')) {
            $nombre = $this->getHelper('dialog')->askAndValidate(
                $output,
                'Please choose a nombre:',
                function($nombre) {
                    if (empty($nombre)) {
                        throw new \Exception('Nombre can not be empty');
                    }

                    return $nombre;
                }
            );
            $input->setArgument('nombre', $nombre);
        }
    }
}

My UserManipulator file:

<?php

namespace INCES\ComedorBundle\Util;

use FOS\UserBundle\Model\UserInterface;
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  $nombre
     * @param Boolean $active
     * @param Boolean $superadmin
     *
     * @return \FOS\UserBundle\Model\UserInterface
     */
    public function create($username, $password, $email, $nombre, $active, $superadmin)
    {
        $user = $this->userManager->createUser();
        $user->setUsername($username);
        $user->setEmail($email);
        $user->setNombre($nombre);
        $user->setPlainPassword($password);
        $user->setEnabled((Boolean) $active);
        $user->setSuperAdmin((Boolean) $superadmin);
        $this->userManager->updateUser($user);

        return $user;
    }
}
Gabriel Muñumel
  • 1,876
  • 6
  • 34
  • 57
  • I don't think you can override a command. Why not just give it a different name? – qooplmao Jan 28 '15 at 13:51
  • Actually I don't want to override a command but add a custom field to the command line. I know can be done because the documentation, the problem is I cannot made it works :(. I changed the title to clarify more about the problem. – Gabriel Muñumel Jan 28 '15 at 14:14
  • Where is the documentation for this? – qooplmao Jan 28 '15 at 15:13
  • This https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md , plus http://stackoverflow.com/questions/11595261/override-symfony2-console-commands – Gabriel Muñumel Jan 28 '15 at 19:52

1 Answers1

0

Here's a technique that may help:

In CreateUserCommand

protected function execute(InputInterface $input, OutputInterface $output)
{
    $username   = $input->getArgument('username');
    $email      = $input->getArgument('email');
    $password   = $input->getArgument('password');
    $nombre     = $input->getArgument('nombre');
    $inactive   = $input->getOption('inactive');
    $superadmin = $input->getOption('super-admin');

    $manipulator = $this->getContainer()->get('inces_comedor.util.user_manipulator');
    $manipulator->setNombre($nombre);
    $manipulator->create($username, $password, $email, $nombre, !$inactive, $superadmin);

    $output->writeln(sprintf('Created user <comment>%s</comment>', $username));
}

and in UserManipulator add:

public function setNombre($nombre)
{
    $this->nombre= $nombre;
}

and change to:

public function create($username, $password, $email, $active, $superadmin)
geoB
  • 4,578
  • 5
  • 37
  • 70
  • Doesn't work. I ended up setting `nullable` my field. :( – Gabriel Muñumel Jan 29 '15 at 00:08
  • Curious. What exactly does "Doesn't work" mean? I used the above to make my own create user command. The only difference is I'm using PUGXMultiUserBundle so there's a different user manager plus a class discriminator. – geoB Jan 29 '15 at 00:18
  • Well I did try it, but not working. It seems like my overriding classes are not taken by FOSUserBundle – Gabriel Muñumel Jan 29 '15 at 00:28
  • I had overridden the FOSUserBundle itself, plus a number of other changes that may or may not be relevant. If you're interested I can post my working code. – geoB Jan 29 '15 at 00:31
  • Ok buddy, would be nice. Actually I don't know if is relevant but I'm using a `symfony 2.0 - code` that was migrated to `symfony 2.6`. – Gabriel Muñumel Jan 29 '15 at 00:37
  • Easiest is to see the Truckee/UserBunde [here](https://github.com/truckee/volunteer) – geoB Jan 29 '15 at 00:38
  • If this is still unresolved, you might want to take a look at [this answer](http://stackoverflow.com/questions/28706656/overriding-fosuserbundle/28733350#28733350). – geoB Feb 28 '15 at 18:47
  • thanks but it doesn't help me. I think is because I was previously working on in an old version of `symfony`. – Gabriel Muñumel Feb 28 '15 at 19:15