1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
|
11
|
|
|
class Tail extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'tail'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'tail command'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* $files. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
31
|
|
|
*/ |
32
|
|
|
protected $files; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* __construct. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(Filesystem $files) |
40
|
|
|
{ |
41
|
2 |
|
parent::__construct(); |
42
|
|
|
|
43
|
2 |
|
$this->files = $files; |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Handle the command. |
48
|
|
|
* |
49
|
2 |
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
2 |
|
public function handle() |
52
|
2 |
|
{ |
53
|
|
|
$path = $this->argument('path'); |
54
|
2 |
|
$lines = (int) $this->option('lines'); |
55
|
1 |
|
|
56
|
1 |
|
if (empty($path) === false) { |
57
|
1 |
|
$root = function_exists('base_path') === true ? base_path() : getcwd(); |
58
|
1 |
|
$file = rtrim($root, '/').'/'.$path; |
59
|
1 |
|
} else { |
60
|
|
|
$path = function_exists('storage_path') === true ? storage_path() : getcwd(); |
61
|
1 |
|
$path = rtrim($path, '/').'/'; |
62
|
|
|
|
63
|
1 |
|
$file = (new Collection($this->files->glob($path.'logs/*.log'))) |
64
|
1 |
|
->map(function ($file) { |
65
|
1 |
|
return is_file($file) === true ? $file : false; |
66
|
1 |
|
})->sortByDesc(function ($file) { |
67
|
|
|
return filectime($file); |
68
|
|
|
})->first(); |
69
|
2 |
|
} |
70
|
2 |
|
|
71
|
|
|
$this->readLine($file, $lines); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* readLine. |
76
|
|
|
* |
77
|
|
|
* @param string $file |
78
|
|
|
* @param int $lines |
79
|
2 |
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
protected function readLine($file, $lines = 50) |
82
|
|
|
{ |
83
|
|
|
if (is_file($file) === false) { |
84
|
|
|
$this->error('tail: cannot open ‘'.$file.'’ for reading: No such file or directory'); |
85
|
|
|
|
86
|
|
|
return; |
87
|
2 |
|
} |
88
|
2 |
|
|
89
|
2 |
|
$fp = fopen($file, 'r'); |
90
|
2 |
|
$i = 1; |
91
|
2 |
|
$result = []; |
92
|
2 |
|
while (! feof($fp)) { |
93
|
|
|
if ($i > $lines) { |
94
|
2 |
|
break; |
95
|
2 |
|
} |
96
|
2 |
|
$content = fgets($fp); |
97
|
2 |
|
$result[] = $content; |
98
|
2 |
|
++$i; |
99
|
|
|
} |
100
|
2 |
|
fclose($fp); |
101
|
2 |
|
|
102
|
|
|
$this->line(implode('', $result)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the console command arguments. |
107
|
|
|
* |
108
|
2 |
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function getArguments() |
111
|
2 |
|
{ |
112
|
2 |
|
return [ |
113
|
|
|
['path', InputArgument::OPTIONAL, 'path'], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the console command options. |
119
|
|
|
* |
120
|
2 |
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function getOptions() |
123
|
2 |
|
{ |
124
|
2 |
|
return [ |
125
|
|
|
['lines', null, InputOption::VALUE_OPTIONAL, 'output the last K lines, instead of the last 50', 50], |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|