webman_duanju/app/command/Logzip.php

77 lines
2.5 KiB
PHP

<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
class Logzip extends Command
{
protected static $defaultName = 'logzip';
protected static $defaultDescription = 'logzip';
/**
* @return void
*/
protected function configure()
{
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dir_arr = [
'runtime/admin/log/',
'runtime/api/log/',
'runtime/czg/log/',
'runtime/log/',
];
$yestermonth = date("Ym");
// 如果今天是1号
if(date('d') == 01 || date('d') == 1) {
$yestermonth = date("Ym", strtotime('-1 month'));
}
foreach ($dir_arr as $dir_a) {
$file_arr = getYesterdayFiles($dir_a . $yestermonth . '/');
if(!empty($file_arr['files'])) {
foreach ($file_arr['files'] as $k => $file_p) {
$file = $dir_a . $yestermonth . '/' . $file_p;
$zip_filename = str_replace('.log', '.zip', $file_p);
$file_zip = $dir_a . $yestermonth . '/' . $zip_filename;
$is_close = false;
if(!file_exists($file_zip)) {
$zip = new ZipArchive();
if($zip->open($file_zip, ZipArchive::CREATE) === TRUE) {
if(file_exists($file)) {
if($zip->addFile($file, $file_p)) {
$is_close = true;
print_r($file . "\r\n");
}
}
if ($is_close) {
if($zip->close()) {
if($is_close) {
unlink($file);
}
}
}
}
}
}
}
}
$output->writeln('ok');
return self::SUCCESS;
}
}