1 | <?php |
||
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 | 2 | public function __construct(Filesystem $files) |
|
44 | |||
45 | /** |
||
46 | * Handle the command. |
||
47 | * |
||
48 | * @throws \InvalidArgumentException |
||
49 | */ |
||
50 | 2 | public function handle() |
|
51 | { |
||
52 | 2 | $path = $this->argument('path'); |
|
53 | 2 | $lines = (int) $this->option('lines'); |
|
54 | |||
55 | 2 | if (empty($path) === false) { |
|
56 | 1 | $root = function_exists('base_path') === true ? base_path() : getcwd(); |
|
57 | 1 | $file = rtrim($root, '/').'/'.$path; |
|
58 | } else { |
||
59 | 1 | $path = function_exists('storage_path') === true ? storage_path() : getcwd(); |
|
60 | 1 | $path = rtrim($path, '/').'/'; |
|
61 | |||
62 | 1 | $file = (new Collection($this->files->glob($path.'logs/*.log'))) |
|
63 | ->map(function ($file) { |
||
64 | 1 | return is_file($file) === true ? $file : false; |
|
65 | })->sortByDesc(function ($file) { |
||
66 | 1 | return filectime($file); |
|
67 | 1 | })->first(); |
|
68 | } |
||
69 | |||
70 | 2 | $this->readLine($file, $lines); |
|
71 | 2 | } |
|
72 | |||
73 | /** |
||
74 | * readLine. |
||
75 | * |
||
76 | * @param string $file |
||
77 | * @param int $lines |
||
78 | * @return string |
||
79 | */ |
||
80 | 2 | protected function readLine($file, $lines = 50) |
|
103 | |||
104 | /** |
||
105 | * Get the console command arguments. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 2 | protected function getArguments() |
|
115 | |||
116 | /** |
||
117 | * Get the console command options. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 2 | protected function getOptions() |
|
127 | } |
||
128 |