website/src/Command/RefreshCommand.php

65 lines
1.9 KiB
PHP

<?php
namespace JeroenED\Website\Command;
use JeroenED\Framework\Kernel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class RefreshCommand extends Command
{
protected static $defaultName = 'refresh';
protected $kernel;
public function __construct(Kernel $kernel)
{
$this->kernel = $kernel;
parent::__construct();
}
protected function configure()
{
$this
->setDescription('Cleanup runs')
->setHelp('This command cleans the runs table');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $kernel;
$zip = $kernel->getCacheDir() . 'website.zip';
$mddir = $kernel->getCacheDir() . 'pages';
$assetdir = $kernel->getProjectDir() . '/public/assets/';
file_put_contents($zip, file_get_contents($_ENV['DATADIR']));
$zipobj = new \ZipArchive();
$res = $zipobj->open($zip);
if ($res === TRUE) {
if(is_dir($mddir)) $this->recursiveRemoveDirectory($mddir);
if(is_dir($assetdir)) $this->recursiveRemoveDirectory($assetdir);
$zipobj->extractTo($kernel->getCacheDir());
$zipobj->close();
rename($kernel->getCacheDir() . 'Website/assets', $assetdir);
rename($kernel->getCacheDir() . 'Website', $mddir);
return Command::SUCCESS;
}
return Command::FAILURE;
}
function recursiveRemoveDirectory($directory)
{
foreach(glob($directory . "/*") as $file)
{
if(is_dir($file)) {
$this->recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}
}