ci4
93 строки · 2.1 Кб
1<?php
2
3declare(strict_types=1);
4
5/**
6* This file is part of CodeIgniter 4 framework.
7*
8* (c) CodeIgniter Foundation <admin@codeigniter.com>
9*
10* For the full copyright and license information, please view
11* the LICENSE file that was distributed with this source code.
12*/
13
14namespace CodeIgniter\Commands\Housekeeping;
15
16use CodeIgniter\CLI\BaseCommand;
17use CodeIgniter\CLI\CLI;
18
19/**
20* ClearLogs command.
21*/
22class ClearLogs extends BaseCommand
23{
24/**
25* The group the command is lumped under
26* when listing commands.
27*
28* @var string
29*/
30protected $group = 'Housekeeping';
31
32/**
33* The Command's name
34*
35* @var string
36*/
37protected $name = 'logs:clear';
38
39/**
40* The Command's short description
41*
42* @var string
43*/
44protected $description = 'Clears all log files.';
45
46/**
47* The Command's usage
48*
49* @var string
50*/
51protected $usage = 'logs:clear [option';
52
53/**
54* The Command's options
55*
56* @var array<string, string>
57*/
58protected $options = [
59'--force' => 'Force delete of all logs files without prompting.',
60];
61
62/**
63* Actually execute a command.
64*/
65public function run(array $params)
66{
67$force = array_key_exists('force', $params) || CLI::getOption('force');
68
69if (! $force && CLI::prompt('Are you sure you want to delete the logs?', ['n', 'y']) === 'n') {
70// @codeCoverageIgnoreStart
71CLI::error('Deleting logs aborted.', 'light_gray', 'red');
72CLI::error('If you want, use the "-force" option to force delete all log files.', 'light_gray', 'red');
73CLI::newLine();
74
75return;
76// @codeCoverageIgnoreEnd
77}
78
79helper('filesystem');
80
81if (! delete_files(WRITEPATH . 'logs', false, true)) {
82// @codeCoverageIgnoreStart
83CLI::error('Error in deleting the logs files.', 'light_gray', 'red');
84CLI::newLine();
85
86return;
87// @codeCoverageIgnoreEnd
88}
89
90CLI::write('Logs cleared.', 'green');
91CLI::newLine();
92}
93}
94