Tail::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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