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