我創建了一個帶有我想從命令視窗運行的 PHP 函式的類。
因此,我創建了一個類擴展命令,但我遇到了麻煩,因為我必須呼叫EntityManagerInterface和UserPasswordHasherInterface.
如果我將它們放在建構式中并創建一個新實體,我會被要求提供兩個引數,我猜不出來。那么當我呼叫我的方法時使用這兩個介面的解決方案是什么?
這是我的 2 個檔案:TestHashPassword
<?php
namespace App;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class TestHashPassword
{
private $entityManager;
private $passwordHasher;
public function __construct(EntityManagerInterface $em,UserPasswordHasherInterface $passwordHasherInterface){
$this->entityManager = $em;
$this->passwordHasher = $passwordHasherInterface;
parent::__construct();
}
public function hashPassword(){
$em = $this->entityManager; // will throw an error "Using $this when not in object context"
$userAll = $em
->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();
}
}
}
TestHashPasswordCommand.php
<?php
namespace App\Command;
use App\Entity\User;
use App\TestHashPassword;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class TestHashPasswordCommand extends Command
{
protected static $defaultName = 'test-hash-password';
protected static $defaultDescription = 'Add a short description for your command';
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
if ($arg1) {
$io->note(sprintf('You passed an argument: %s', $arg1));
}
if ($input->getOption('option1')) {
// ...
}
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
$foobar = new TestHashPassword(); // 2 parameters necessary
$foobar->hashPassword();
// TestHashPassword::hashPassword(); // will give an error saying I cannot use $this there
return Command::SUCCESS;
}
}
我在命令 CLI 中運行:php .\bin\console test-hash-password
uj5u.com熱心網友回復:
您也應該能夠為您的命令使用自動裝配(使用默認的 services.yaml 配置)。
而不是創建一個new TestHashPassword();,使用自動連接將其作為服務注入。
在您的命令中添加__construct:
use App\TestHashPassword;
class TestHashPasswordCommand extends Command
{
protected $testHashPassword;
protected static $defaultName = 'test-hash-password';
protected static $defaultDescription = 'Add a short description for your command';
public function __construct(TestHashPassword $testHashPassword)
{
$this->testHashPassword = $testHashPassword;
}
然后將它與您的初始化變數一起使用:
$this->testHashPassword->hashPassword();
parent::__construct();PS:如果它不擴展類,為什么你的服務建構式中有?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/475560.html
