update again updater

This commit is contained in:
Hesabix 2025-03-06 08:50:46 +00:00
parent a42c0a6130
commit f464d9e69a
2 changed files with 77 additions and 2 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace App\Command;
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\Process\Process;
#[AsCommand(
name: 'hesabix:test-env',
description: 'Tests the environment and runs composer install.'
)]
class TestEnvironmentCommand extends Command
{
private string $appDir;
private string $env;
public function __construct(string $name = null)
{
$this->appDir = dirname(__DIR__, 2);
// مستقیماً از .env.local.php بخوان
$envConfig = file_exists($this->appDir . '/.env.local.php') ? require $this->appDir . '/.env.local.php' : [];
$this->env = $envConfig['APP_ENV'] ?? getenv('APP_ENV') ?: 'prod';
parent::__construct($name);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln("Detected environment: " . $this->env);
$composerCommand = ['composer', 'install', '--optimize-autoloader'];
if ($this->env !== 'dev') {
$composerCommand[] = '--no-dev';
$composerCommand[] = '--no-scripts';
}
$output->writeln("Running command: " . implode(' ', $composerCommand));
$process = new Process($composerCommand, $this->appDir);
$process->setTimeout(3600);
if ($output->isVerbose()) {
$process->mustRun(function ($type, $buffer) use ($output) {
$output->write($buffer);
});
} else {
$process->mustRun();
}
$output->writeln('<info>Composer install completed successfully.</info>');
// تست اجرای cache:clear برای اطمینان
$this->runProcess(['php', 'bin/console', 'cache:clear', "--env={$this->env}"], $output);
return Command::SUCCESS;
}
private function runProcess(array $command, OutputInterface $output): void
{
$process = new Process($command, $this->appDir);
$process->setTimeout(3600);
if ($output->isVerbose()) {
$process->mustRun(function ($type, $buffer) use ($output) {
$output->write($buffer);
});
} else {
$process->mustRun();
}
$output->writeln('<info>' . implode(' ', $command) . ' completed successfully.</info>');
}
}

View file

@ -39,7 +39,9 @@ class UpdateSoftwareCommand extends Command
$this->archiveDir = $this->rootDir . '/hesabixArchive';
$this->backupDir = $this->rootDir . '/hesabixBackup';
$this->stateFile = $this->backupDir . '/' . Uuid::uuid4() . '/update_state.json';
$this->env = getenv('APP_ENV') ?: 'prod';
$envConfig = file_exists($this->appDir . '/.env.local.php') ? require $this->appDir . '/.env.local.php' : [];
$this->env = $envConfig['APP_ENV'] ?? getenv('APP_ENV') ?: 'prod';
$this->logger->info("Environment detected: " . $this->env);
parent::__construct();
}
@ -54,7 +56,7 @@ class UpdateSoftwareCommand extends Command
$uuid = Uuid::uuid4()->toString();
$this->logger->info("Starting software update with UUID: $uuid in {$this->env} mode");
$this->writeOutput($output, "Starting software update (UUID: $uuid)...");
$this->writeOutput($output, "Starting software update (UUID: $uuid) in {$this->env} mode");
if ($this->isUpToDate()) {
$this->writeOutput($output, '<info>The software is already up to date with the remote repository.</info>');
@ -124,6 +126,7 @@ class UpdateSoftwareCommand extends Command
$composerCommand = ['composer', 'install', '--optimize-autoloader'];
if ($this->env !== 'dev') {
$composerCommand[] = '--no-dev';
$composerCommand[] = '--no-scripts';
}
$this->runProcess($composerCommand, $this->appDir, $output, 3);
$state['completedSteps'][] = 'composer_install';