Skip to content

Commit 8383c91

Browse files
committed
improved some var checks and added ansi color helper functions
1 parent efcb6b7 commit 8383c91

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

shell.php

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
<?php
22
// Copyright (C) 2016 Grzegorz Kowalski, see LICENSE file
33

4-
function llecho($message, $ansi_color = '31m') {
4+
function ansi_color($R, $G, $B, $bold = false) {
5+
return 30 + ($R?1:0) + ($G?2:0) + ($B?3:0) + ($bold?40:0);
6+
}
7+
8+
function ansi_str($message, $code) {
9+
return '[' . $code . 'm' . $message . '';
10+
}
11+
12+
// llecho('my message');
13+
// llech('my bold blue message', ansi_color(0, 0, 1, 1));
14+
function llecho($message, $ansi_color = '31') {
515
if (strpos(php_uname(), 'Windows 10') !== false) {
616
system('powershell "[Text.Encoding]::UTF8.GetString([convert]::FromBase64String(\"'
7-
. base64_encode('[' . $ansi_color . $message . '[0m')
17+
. base64_encode(ansi_str($message, $ansi_color))
818
.'\"))');
919
} else {
1020
echo "// $message\n";
@@ -32,19 +42,11 @@ function execute_console_app($path, $process_function, $data = null)
3242
if (is_resource($process)) {
3343
$return_value = $process_function($data, $pipes[0], $pipes[1], $pipes[2]);
3444

35-
if (isset($pipes[2]) and is_resource($pipes[2])) {
36-
fclose($pipes[2]);
37-
unset($pipes[2]);
38-
}
39-
40-
if (isset($pipes[1]) and is_resource($pipes[1])) {
41-
fclose($pipes[1]);
42-
unset($pipes[1]);
43-
}
44-
45-
if (isset($pipes[0]) and is_resource($pipes[0])) {
46-
fclose($pipes[0]);
47-
unset($pipes[0]);
45+
foreach ([2,1,0] as $i) {
46+
if (isset($pipes[$i]) and is_resource($pipes[$i])) {
47+
fclose($pipes[$i]);
48+
unset($pipes[$i]);
49+
}
4850
}
4951

5052
$results = Array(
@@ -112,9 +114,6 @@ function check_syntax($code, $print_error_message = false)
112114
/* test: $fp = fopen("foo", "w") */
113115
if (is_resource($_)) llecho('$_ is a resource of type ' . get_resource_type($_));
114116

115-
/* test: $fp = fopen("foo", "w"); fclose($fp); $fp */
116-
if (gettype($_) == 'resource (closed)') llecho('$_ is a closed resource');
117-
118117
/* test: "test" */
119118
if (is_string($_)) llecho('$_ is a string of length ' . strlen($_));
120119

@@ -129,12 +128,28 @@ function check_syntax($code, $print_error_message = false)
129128
/* test: null */
130129
if (is_null($_)) llecho('$_ is null');
131130

131+
/* test: array() */
132+
if (empty($_)) llecho('$_ is empty');
133+
132134
/* test: true */
133135
if (is_bool($_)) llecho('$_ has boolean value of ' . ($_? 'true' : 'false'));
134136

135137
/* test: 0 */
136138
if (is_numeric($_)) llecho('$_ has numeric value of ' . $_);
137139

140+
/* test: array() */
141+
if (version_compare(phpversion(), '7.1.0', '>=')) {
142+
if (is_iterable($_)) llecho('$_ is iterable');
143+
}
144+
145+
/* test: $fp = fopen("foo", "w"); fclose($fp); $fp */
146+
if (version_compare(phpversion(), '7.2.0', '>=')) {
147+
if (gettype($_) == 'resource (closed)') llecho('$_ is a closed resource');
148+
} else {
149+
if (gettype($_) == 'unknown type') llecho('$_ is a closed resource');
150+
}
151+
152+
/* test: array() */
138153
if (version_compare(phpversion(), '7.3.0', '>=')) {
139154
if (is_countable($_)) llecho('$_ is countable and has count ' . count($_));
140155
}

0 commit comments

Comments
 (0)