I would like to run my function hashPassword, which is in my class, from the command line. I have found many post on the topic but I never succeed to make it run apparently.
How should I run it ?
<?php
namespace App;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
class TestHashPassword
{
private $entityManager;
private $passwordHasher;
public function __construct(EntityManagerInterface $em,UserPasswordHasherInterface $passwordHasherInterface){
$this->entityManager = $em;
$this->passwordHasher = $passwordHasherInterface;
parent::__construct();
}
public function hashPassword(){
$userAll = $this->entityManager
->getRepository(User::class)
->findAll();
echo("coucou");
foreach($userAll as $user){
$comb = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array();
$combLen = strlen($comb) - 1;
for ($i = 0; $i < 8; $i ) {
$n = rand(0, $combLen);
$pass[] = $comb[$n];
};
echo("coucou");
$user->setDescription($pass);
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$pass
);
$user->setPassword($hashedPassword);
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
}
I have tried :
php -r "include 'App\TestHashPassword.php'; TestHashPassword::hashPassword();"
php -r "include 'TestHashPassword.php'; TestHashPassword::hashPassword();"
php "require 'TestHashPassword.php'; hashPassword();"
php -r "require 'TestHashPassword.php'; hashPassword();"
...
I also have tested without the require or include. Tried with another file which call the function but nothing works.
CodePudding user response:
look at the constructor, this class uses dependencies that implement the EntityManagerInterface and UserPasswordHasherInterface interfaces, if you want to use the TestHashPassword class outside of the Symfony context, you should create instances of these dependencies.
However, you probably want to use Symfony DI container. Then let's create a console command, via:
php .\bin\console make:command test-hash-password
Next, place the hashPassword method call in the execute section:
<?php
namespace App\Command;
use App\TestHashPassword;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'test-hash-password',
description: 'Add a short description for your command',
)]
class TestHashPasswordCommand extends Command
{
public function __construct(
TestHashPassword $testHashPassword,
)
{
parent::__construct();
$this->testHashPassword = $testHashPassword;
}
protected function configure(): void
{
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->testHashPassword->hashPassword();
$io->success('The hashPassword method called successfully!');
return Command::SUCCESS;
}
}
Now you can execute your new command:
php .\bin\console test-hash-password