如何从命令行 (Symfony) 运行 PHP Class 中的函数

How to run a function in a PHP Class from Command Line (Symfony)

我想从命令行 运行 我的函数 hashPassword,它在我的 class 中。 我发现了很多关于这个话题的 post,但显然我从来没有成功 运行。

我应该怎么运行呢?

<?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();
        }
    }
}


我试过了:

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();"

...

我也在没有要求或包含的情况下进行了测试。尝试使用另一个调用该函数的文件,但没有任何效果。

看看构造函数,这个 class 使用实现 EntityManagerInterface 和 UserPasswordHasherInterface 接口的依赖项,如果你想在 Symfony 上下文之外使用 TestHashPassword class,你应该创建这些实例依赖项。

但是,您可能想要使用 Symfony DI 容器。然后让我们创建一个控制台命令,通过:

php .\bin\console make:command test-hash-password

接下来,将 hashPassword 方法调用放在执行部分:

<?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;
    }
}

现在您可以执行新命令了:

php .\bin\console test-hash-password