File size: 4,432 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | <?php
declare(strict_types=1);
namespace Presentation\Commands;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
use Easy\Container\Attributes\Inject;
use Plugin\Domain\PluginWrapper;
use Plugin\Domain\Repositories\PluginRepositoryInterface;
use Plugin\Infrastructure\Helpers\ComposerHelper;
use Psr\Container\ContainerInterface;
use Ramsey\Uuid\Nonstandard\Uuid;
use Shared\Infrastructure\CacheManager;
use Shared\Infrastructure\FileSystem\FileSystemInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use ZipArchive;
#[AsCommand(name: 'app:update')]
class UpdateCommand extends Command
{
public function __construct(
private ContainerInterface $container,
private PluginRepositoryInterface $repo,
private ComposerHelper $helper,
private CacheManager $cache,
private FileSystemInterface $fs,
private DependencyFactory $df,
#[Inject('config.dirs.root')]
private string $rootDir,
#[Inject('config.dirs.plugins')]
private string $pluginsDir,
) {
parent::__construct();
set_time_limit(0);
}
protected function configure(): void
{
$this->addArgument(
'file',
InputArgument::REQUIRED,
'Path to the update file'
);
}
protected function execute(
InputInterface $input,
OutputInterface $output
): int {
$file = $input->getArgument('file');
$zip = new ZipArchive;
if ($zip->open($file) !== true) {
throw new InvalidArgumentException('Could not open file');
}
$backupDir = $this->rootDir . '/var/update/' . Uuid::uuid4()->toString();
// Take backup for folowing files and folders
$paths = ['locale', 'LICENSE'];
foreach ($paths as $path) {
if ($this->fs->fileExists($path)) {
$this->fs->copy('/' . $path, $backupDir . '/' . $path);
continue;
}
if ($this->fs->directoryExists($path)) {
$files = $this->fs->listContents($path, true);
foreach ($files as $f) {
if ($f->isDir()) {
continue;
}
$this->fs->copy($f->path(), $backupDir . '/' . $f->path());
}
}
}
// Extract files
$zip->extractTo($this->rootDir);
$zip->close();
// Migrate DB
$in = new ArrayInput([]);
$in->setInteractive(false);
$cmd = new MigrateCommand($this->df);
$cmd->run($in, $output);
// Reinstall plugins
$packages = [];
/** @var PluginWrapper */
foreach ($this->repo as $pw) {
$packages[] = $pw->context->name->value;
}
$this->helper->require($packages, $output);
// Move back backup files
foreach ($paths as $path) {
if ($this->fs->fileExists($backupDir . '/' . $path)) {
$this->fs->copy('/' . $backupDir . '/' . $path, $path);
$this->fs->delete($backupDir . '/' . $path);
continue;
}
if ($this->fs->directoryExists($backupDir . '/' . $path)) {
$files = $this->fs->listContents($backupDir . '/' . $path, true);
foreach ($files as $f) {
if ($f->isDir()) {
continue;
}
$this->fs->copy($f->path(), substr($f->path(), strlen($backupDir)));
}
$this->fs->deleteDirectory($backupDir . '/' . $path);
}
}
// Remove backup directory
$this->fs->deleteDirectory($backupDir);
// Extract locales
/** @var ExtractLocaleMessagesCommand */
$cmd = $this->container->get(ExtractLocaleMessagesCommand::class);
$cmd->run(new ArrayInput([]), $output);
// Clear cache
$this->cache->clearCache();
return Command::SUCCESS;
}
}
|