1 | <?php |
||
8 | class Commands |
||
9 | { |
||
10 | /** |
||
11 | * @var Service |
||
12 | */ |
||
13 | private $healthService; |
||
14 | |||
15 | /** |
||
16 | * Commands constructor. |
||
17 | * |
||
18 | * @param Service $healthService |
||
19 | */ |
||
20 | public function __construct(HealthService $healthService) |
||
21 | { |
||
22 | $this->healthService = $healthService; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @param $rows |
||
27 | * @return \Illuminate\Support\Collection|\IlluminateAgnostic\Arr\Support\Collection|\IlluminateAgnostic\Collection\Support\Collection|\IlluminateAgnostic\Str\Support\Collection|\Tightenco\Collect\Support\Collection|\Vanilla\Support\Collection |
||
28 | * @throws \Exception |
||
29 | */ |
||
30 | protected function getTargetsFomResources($resources) |
||
31 | { |
||
32 | $targets = collect(); |
||
33 | |||
34 | foreach ($resources as $resource) { |
||
35 | foreach ($resource->targets as $target) { |
||
36 | $targets->push($target); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | return $targets; |
||
41 | } |
||
42 | |||
43 | private function normalizeMessage($message) |
||
44 | { |
||
45 | $message = str_replace("\n", '', $message); |
||
46 | $message = str_replace("\r", '', $message); |
||
47 | $message = str_replace("\t", ' ', $message); |
||
48 | $message = str_replace('<br>', ' ', $message); |
||
49 | $message = str_replace(' ', ' ', $message); |
||
50 | |||
51 | $message = wordwrap($message, 60); |
||
52 | |||
53 | return $message; |
||
54 | } |
||
55 | |||
56 | public function panel(Command $command = null) |
||
57 | { |
||
58 | $columns = ['Resource', 'State', 'Message']; |
||
59 | |||
60 | $rows = $this->getTargetsFomResources($this->healthService->health()) |
||
61 | ->map(function ($target) { |
||
62 | return [ |
||
63 | "{$target->resource->name} ({$target->display})", |
||
64 | $target->result->healthy |
||
65 | ? '<info>healthy</info>' |
||
66 | : '<fg=red>failing</fg=red>', |
||
67 | $this->normalizeMessage($target->result->errorMessage), |
||
68 | ]; |
||
69 | }) |
||
70 | ->toArray(); |
||
71 | |||
72 | $this->table($command, $columns, $rows); |
||
73 | } |
||
74 | |||
75 | public function check(Command $command = null) |
||
76 | { |
||
77 | $checker = $this->healthService->getSilentChecker(); |
||
78 | |||
79 | $errors = $this->getTargetsFomResources($checker()->filter(function ($resource) { |
||
80 | return ! $resource->isGlobal; |
||
81 | }))->reduce(function ($carry, $target) { |
||
82 | return $carry + ($target->result->healthy ? 0 : 1); |
||
83 | }, 0); |
||
84 | |||
85 | if ($errors) { |
||
86 | $this->error( |
||
87 | $command, |
||
88 | "Application needs attention, $errors ". |
||
89 | str_plural('resouce', $errors). |
||
|
|||
90 | ' '. |
||
91 | ($errors > 1 ? 'are' : 'is'). |
||
92 | ' currently failing.' |
||
93 | ); |
||
94 | } else { |
||
95 | $this->info($command, 'Check completed with no errors.'); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Format input to textual table. |
||
101 | * |
||
102 | * @param Command|null $command |
||
103 | * @param $columns |
||
104 | * @param \Illuminate\Contracts\Support\Arrayable|array $rows |
||
105 | */ |
||
106 | private function table($command, $columns, $rows) |
||
107 | { |
||
108 | if ($command) { |
||
109 | $command->table($columns, $rows); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Write a string as information output. |
||
115 | * |
||
116 | * @param Command|null $command |
||
117 | * @param $string |
||
118 | */ |
||
119 | private function info($command, $string) |
||
125 | |||
126 | /** |
||
127 | * Write a string as information output. |
||
128 | * |
||
129 | * @param Command|null $command |
||
130 | * @param $string |
||
131 | */ |
||
132 | private function error($command, $string) |
||
133 | { |
||
134 | if ($command) { |
||
135 | $command->error($string); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Write a string as information output. |
||
141 | * |
||
142 | * @param Command|null $command |
||
143 | * @param $string |
||
144 | */ |
||
145 | private function warn($command, $string) |
||
151 | } |
||
152 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.