forked from ktrain/mageshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mageshell.php
114 lines (96 loc) · 2.76 KB
/
Mageshell.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
107
108
109
110
111
112
113
<?php
class Mageshell
{
protected $_prompt = 'magesh> ';
public function setPrompt($prompt)
{
$this->_prompt = $prompt;
return $this;
}
public function start()
{
$histfile = '.magesh_history';
if (PHP_OS == 'Linux') {
readline_read_history($histfile);
}
do {
$input = $this->_getInput($this->_prompt);
if ($input === false) {
break;
}
$cmd = $this->_formatInput($input);
if (PHP_OS == 'Linux') {
readline_add_history($input);
readline_write_history($histfile);
}
echo "\n";
$result = null;
try {
$result = eval($cmd);
} catch (Exception $e) {
$result = $e->getMessage() . "\n\n" . $e->getTraceAsString() . "\n\n";
}
$output = $this->_formatOutput($result);
echo "Result: " . $output . "\n\n";
} while (true);
return true;
}
protected function _getInput()
{
if (PHP_OS == 'Linux') {
$input = readline($this->_prompt);
} else {
echo $this->_prompt;
$input = fgets(STDIN);
}
return $input;
}
/**
* If input is non-empty, ensure that there's a semicolon at the end.
*/
protected function _formatInput($input)
{
if (!$input) {
return 'return;';
}
return rtrim('return (' . rtrim($input), ';') . ');';
}
protected function _getPrintableData($obj)
{
if ($obj instanceof Varien_Object) {
$data = $obj->getData();
} elseif ($obj instanceof Varien_Data_Collection) {
$data = array(
'COLLECTION CONTENTS NOT PRINTED' => 'Do getItems(), toArray(), etc.'
);
} elseif (is_array($obj)) {
$data = $obj;
} else {
return $obj;
}
foreach ($data as $i => $item) {
if (is_object($item)) {
$data [$i] = get_class($item);
}
if (is_array($item)) {
if (empty($item)) {
$data [$i] = "Array()";
} else {
$data [$i] = "Array(...)";
}
}
}
return $data;
}
protected function _formatOutput($result)
{
if (is_object($result)) {
$classString = '[' . get_class($result) . ']';
$data = $this->_getPrintableData($result);
return $classString . "\nData:\n" . print_r($data, true);
} else if (is_array($result)) {
return print_r($result, true);
}
return $result;
}
}