feat: enhance purge commands with environment handling and testing improvements

This commit is contained in:
hamed
2026-07-20 11:54:02 +03:30
parent 28725792d7
commit a8d33ceeaf
5 changed files with 70 additions and 25 deletions
+10 -6
View File
@@ -9,6 +9,7 @@ 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\DependencyInjection\Attribute\Autowire;
/**
* پاک‌سازی کامل همهٔ پزشکان و داده‌های وابسته — برای ساختِ یک دیتابیس تمیزِ تست.
@@ -34,8 +35,11 @@ class PurgeDoctorsCommand extends Command
'holidays', 'comments', 'rates', 'appointments', 'doctors',
];
public function __construct(private readonly Connection $conn)
{
public function __construct(
private readonly Connection $conn,
#[Autowire('%kernel.environment%')]
private readonly string $env,
) {
parent::__construct();
}
@@ -50,10 +54,10 @@ class PurgeDoctorsCommand extends Command
$force = (bool) $input->getOption('force');
// فقط و فقط dev/test. این کامند همهٔ پزشکان + نوبت/نظر/پرداخت را پاک می‌کند و
// هیچ راه فراری برای prod ندارد.
$env = $_ENV['APP_ENV'] ?? 'dev';
if (!in_array($env, ['dev', 'test'], true)) {
$io->error(sprintf('این کامند فقط در محیط dev اجرا می‌شود (APP_ENV فعلی: %s).', $env));
// هیچ راه فراری برای prod ندارد. محیط از kernel می‌آید (نه $_ENV) تا اگر APP_ENV
// از راهی غیر از dotenv ست شده باشد، به اشتباه به dev fallback نشود.
if (!in_array($this->env, ['dev', 'test'], true)) {
$io->error(sprintf('این کامند فقط در محیط dev اجرا می‌شود (APP_ENV فعلی: %s).', $this->env));
return Command::FAILURE;
}
@@ -9,6 +9,7 @@ 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\DependencyInjection\Attribute\Autowire;
/**
* حذف پزشکانِ ایمپورت‌شدهٔ خزنده که هنوز مالکیتشان گرفته نشده (`owner_status='unclaimed'`).
@@ -35,8 +36,11 @@ class PurgeUnclaimedDoctorsCommand extends Command
'holidays', 'comments', 'rates', 'appointments',
];
public function __construct(private readonly Connection $conn)
{
public function __construct(
private readonly Connection $conn,
#[Autowire('%kernel.environment%')]
private readonly string $env,
) {
parent::__construct();
}
@@ -54,7 +58,9 @@ class PurgeUnclaimedDoctorsCommand extends Command
$io = new SymfonyStyle($input, $output);
$force = (bool) $input->getOption('force');
if (($_ENV['APP_ENV'] ?? 'dev') === 'prod' && !$input->getOption('i-know-this-is-prod')) {
// محیط از kernel می‌آید (نه $_ENV) تا گاردِ prod با ست‌شدن APP_ENV از راهی
// غیر از dotenv دور زده نشود.
if ($this->env === 'prod' && !$input->getOption('i-know-this-is-prod')) {
$io->error('روی prod بدون --i-know-this-is-prod اجرا نمی‌شود.');
return Command::FAILURE;
}