我一直在寻找并尝试向FOSUserBundle
添加自定义命令,但这是不可能的:(
我会对文件的结构进行三重检查,CreateUserCommand
位于Command
文件夹中,UserManipulator
位于Util
文件夹中。
我还尝试重新启动服务器并更改命令但不起作用:
命令行
php app/console fos:user:create root test@example.com admin gabriel --super-admin
错误:
[RuntimeException]
Too many arguments.
我的捆绑主文件:
<?php
namespace INCES\ComedorBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class INCESComedorBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
我的CreateUserCommand文件:
<?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);
}
}
}
我的UserManipulator文件:
<?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;
}
}
答案 0 :(得分:0)
这是一项可能有所帮助的技术:
在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));
}
并在UserManipulator中添加:
public function setNombre($nombre)
{
$this->nombre= $nombre;
}
并改为:
public function create($username, $password, $email, $active, $superadmin)