One Hat Cyber Team
Your IP :
3.144.39.0
Server IP :
192.145.235.60
Server :
Linux ngx365.inmotionhosting.com 5.14.0-427.33.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 30 09:45:56 EDT 2024 x86_64
Server Software :
Apache
PHP Version :
8.2.27
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
centos12
/
www
/
centosuit
/
app
/
Console
/
Commands
/
View File Name :
ClearLogs.php
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; class ClearLogs extends Command { protected $signature = 'log:clean {--keep-last : Whether the last log file should be kept} {--keep=* : Log files to keep (without extension)}'; protected $description = 'Remove every log files in the log directory'; public function __construct(private Filesystem $disk) // phpcs:ignore { parent::__construct(); } public function handle(): int { $files = $this->getLogFiles(); $filesToKeep = $this->option('keep'); if ($this->option('keep-last') && $files->count() >= 1) { $files->shift(); } $files = $this->filesToDelete($files, $filesToKeep); $deleted = $this->delete($files); if (!$deleted) { $this->info('There was no log file to delete in the log folder'); } elseif ($deleted == 1) { $this->info('1 log file has been deleted'); } else { $this->info($deleted . ' log files have been deleted'); } return Command::SUCCESS; } /** * Get a collection of log files sorted by their last modification date. */ private function getLogFiles(): Collection { return Collection::make( $this->disk->allFiles(storage_path('logs')) )->sortBy('mtime'); } /** * Remove specified files from deletion list */ private function filesToDelete(Collection $files, array $fileNames): Collection { return $files->filter(function ($value, $key) use ($fileNames) { return !in_array($value->getFilenameWithoutExtension(), $fileNames); }); } /** * Delete the given files. */ private function delete(Collection $files): int { return $files->each(function ($file) { $this->disk->delete($file); })->count(); } }