|
1
|
|
|
<?php namespace nyx\console; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Terminal |
|
5
|
|
|
* |
|
6
|
|
|
* @package Nyx\Console |
|
7
|
|
|
* @version 0.1.0 |
|
8
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
9
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
10
|
|
|
* @link https://github.com/unyx/nyx |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Terminal implements interfaces\Terminal |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array The Terminal's dimensions. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $dimensions; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Attempts to determine the Terminal's dimensions. |
|
21
|
|
|
* |
|
22
|
|
|
* @return array An array containing two keys - 'width' and 'height'. The values for those keys can be null |
|
23
|
|
|
* if the respective value could not be determined. |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract protected function getDimensions() : ?array; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Creates a new Terminal instance. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->flushDimensions(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getWidth(int $default = 80) : int |
|
39
|
|
|
{ |
|
40
|
|
|
if (isset($this->dimensions['width'])) { |
|
41
|
|
|
return $this->dimensions['width']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Environmental variables take priority and should be platform-agnostic. |
|
45
|
|
|
if ($width = trim(getenv('COLUMNS'))) { |
|
46
|
|
|
return $this->dimensions['width'] = (int) $width; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->getDimensions()['width'] ?? $default; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getHeight(int $default = 32) : int |
|
56
|
|
|
{ |
|
57
|
|
|
if (isset($this->dimensions['height'])) { |
|
58
|
|
|
return $this->dimensions['height']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Environmental variables take priority and should be platform-agnostic. |
|
62
|
|
|
if ($height = trim(getenv('LINES'))) { |
|
63
|
|
|
return $this->dimensions['height'] = (int) $height; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->getDimensions()['height'] ?? $default; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Resets the cached dimensions of the underlying terminal. |
|
71
|
|
|
* |
|
72
|
|
|
* In usual execution flows the dimensions are unlikely to change. However, when running REPLs or long processes, |
|
73
|
|
|
* there is a likelihood of the Terminal's window dimensions to change. For those it makes sense to manually |
|
74
|
|
|
* flush the values before requesting them, when they are to be relied upon. |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
public function flushDimensions() : Terminal |
|
79
|
|
|
{ |
|
80
|
|
|
$this->dimensions = [ |
|
81
|
|
|
'width' => null, |
|
82
|
|
|
'height' => null |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Executes a system call and returns its output, while suppressing any error output. |
|
90
|
|
|
* |
|
91
|
|
|
* Requires proc_open() to be available on the platform. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $command The command to execute. |
|
94
|
|
|
* @return string The output of the process or null if the process could not be executed. |
|
|
|
|
|
|
95
|
|
|
* @todo Use the Process component instead since we're relying on proc_open() anyways? |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function execute(string $command) : ?string |
|
98
|
|
|
{ |
|
99
|
|
|
// We require proc_open() to suppress error output. |
|
100
|
|
|
if (!function_exists('proc_open')) { |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Define the file pointers we are going to utilize. |
|
105
|
|
|
$descriptors = [ |
|
106
|
|
|
1 => ['pipe', 'w'], |
|
107
|
|
|
2 => ['pipe', 'w'] |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
// Execute the command with error suppression. Make sure we got a valid resource to work with. |
|
111
|
|
|
if (!is_resource($process = proc_open($command, $descriptors, $pipes, null, null, ['suppress_errors' => true]))) { |
|
112
|
|
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$output = stream_get_contents($pipes[1]); |
|
116
|
|
|
|
|
117
|
|
|
// Close all open resource pointers. |
|
118
|
|
|
fclose($pipes[1]); |
|
119
|
|
|
fclose($pipes[2]); |
|
120
|
|
|
proc_close($process); |
|
121
|
|
|
|
|
122
|
|
|
return $output; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.