forked from laravel/pail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPailCommand.php
106 lines (92 loc) · 3.05 KB
/
PailCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Laravel\Pail\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Process\Exceptions\ProcessTimedOutException;
use Laravel\Pail\File;
use Laravel\Pail\Guards\EnsurePcntlIsAvailable;
use Laravel\Pail\Options;
use Laravel\Pail\ProcessFactory;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use function Termwind\render;
use function Termwind\renderUsing;
#[AsCommand(name: 'pail')]
class PailCommand extends Command
{
/**
* {@inheritDoc}
*/
protected $signature = 'pail
{--filter= : Filter the logs}
{--message= : Filter the logs by the message}
{--level= : Filter the logs by the level}
{--auth= : Filter the logs by the authenticated ID}
{--user= : Filter the logs by the authenticated ID (alias for --auth)}';
/**
* {@inheritDoc}
*/
protected $description = 'Tails the application logs.';
/**
* The file instance, if any.
*/
protected ?File $file = null;
/**
* Handles the command execution.
*/
public function handle(ProcessFactory $processFactory): void
{
EnsurePcntlIsAvailable::check();
renderUsing($this->output);
render(<<<'HTML'
<div class="max-w-150 mx-2 mt-1 flex">
<div>
<span class="px-1 bg-blue uppercase text-white">INFO</span>
<span class="flex-1">
<span class="ml-1 ">Tailing application logs.</span>
</span>
</div>
<span class="flex-1"></span>
<span class="text-gray ml-1">
<span class="text-gray">Press Ctrl+C to exit</span>
</span>
</div>
HTML,
);
render(<<<'HTML'
<div class="max-w-150 mx-2 flex">
<div>
</div>
<span class="flex-1"></span>
<span class="text-gray ml-1">
<span class="text-gray">Use -v|-vv to show more details</span>
</span>
</div>
HTML,
);
$this->file = new File(storage_path('pail/'.uniqid().'.pail'));
$this->file->create();
$this->trap([SIGINT, SIGTERM], fn () => $this->file->destroy());
$options = Options::fromCommand($this);
assert($this->file instanceof File);
try {
$processFactory->run($this->file, $this->output, $this->laravel->basePath(), $options);
} catch (ProcessSignaledException $e) {
if (in_array($e->getSignal(), [SIGINT, SIGTERM], true)) {
$this->newLine();
}
} catch (ProcessTimedOutException $e) {
$this->components->info('Maximum execution time exceeded.');
} finally {
$this->file->destroy();
}
}
/**
* Handles the object destruction.
*/
public function __destruct()
{
if ($this->file) {
$this->file->destroy();
}
}
}