forked from rgranadino/Interactive-Magento-Console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imc
executable file
·147 lines (146 loc) · 4.73 KB
/
imc
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env php
<?php
declare(ticks = 1);
if (!is_readable('app/Mage.php')) {
echo "Could not find app/Mage.php\n";
exit(1);
}
require 'app/Mage.php';
if (!Mage::isInstalled()) {
echo "Application is not installed yet, please complete install wizard first.";
exit(1);
}
$baseDir = getcwd();
//not sure if this is necessary, ported over from cron.php script
$_SERVER['SCRIPT_NAME'] = $baseDir.'/index.php';
$_SERVER['SCRIPT_FILENAME'] = $baseDir.'/index.php';
try {
ini_set('display_errors', 1);
ini_set('log_errors', 1);
if (!empty($_SERVER['HOME'])) {
ini_set('error_log', $_SERVER['HOME'] . '/imc.log');
}
error_reporting(E_ALL);
$storeCode = '';
if (isset($argv[1])) {
$storeCode = $argv[1];
}
Mage::app($storeCode, 'store')->setUseSessionInUrl(false);
IMC::getInstance()->read();
} catch (Exception $e) {
Mage::printException($e);
}
class IMC {
protected static $instance = null;
protected $historyFile = null;
protected $histSize = 20;
protected $history = array();
protected $readline = true;
protected function __construct()
{
if (!function_exists('readline')) {
echo "NOTICE: no readline support detected, command line functionaltiy will be limited\n";
$this->readline = false;
}
if (!empty($_SERVER['HOME'])) {
$this->historyFile = $_SERVER['HOME'].'/.imc_history';
if (!file_exists($this->historyFile)) {
file_put_contents($this->historyFile, '');
}
if ($this->readline) {
readline_read_history($this->historyFile);
}
$this->history = explode(file_get_contents($this->historyFile), "\n");
if (isset($_ENV['HISTSIZE']) && $_ENV['HISTSIZE'] > 0) {
$this->histSize = $_ENV['HISTSIZE'];
}
}
if ($this->readline) {
readline_completion_function(array($this, 'completeCallback'));
}
register_shutdown_function(array($this, 'fatalErrorShutdown'));
# // Catch Ctrl+C, kill and SIGTERM
pcntl_signal(SIGTERM, array($this, 'sigintShutdown'));
pcntl_signal(SIGINT, array($this, 'sigintShutdown'));
}
public function fatalErrorShutdown()
{
$this->quit();
}
public function sigintShutdown($signal)
{
if ($signal === SIGINT || $signal === SIGTERM) {
$this->quit();
}
}
public function __destruct()
{
if (!empty($this->historyFile) && is_writable($this->historyFile) && $this->readline) {
readline_write_history($this->historyFile);
}
}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new IMC();
}
return self::$instance;
}
public function read()
{
while (true) {
if ($this->readline) {
$line = readline('Mage> ');
} else {
echo 'Mage> ';
$line = stream_get_line(STDIN, 1024, PHP_EOL);
}
if ($line == 'exit') {
$this->quit();
}
if (!empty($line)) {
$this->addToHistory($line);
eval($line);
echo "\n";
}
}
}
protected function addToHistory($line)
{
if ($histsize = count($this->history) > $this->histSize) {
$this->history = array_slice($this->history, $histsize - $this->histSize);
}
if ($this->readline) {
readline_add_history($line);
}
}
public function quit($code=0)
{
$this->__destruct();//just to be safe, if eval causes fatal error we have to call explicitly
exit($code);
}
protected function completeCallback($line)
{
if (!empty($line)) {
$line = preg_quote($line);
$funcs = get_defined_functions();
$constants = get_defined_constants();//use these?
$avail = array_merge(get_declared_classes(),$funcs['user'], $funcs['internal'], array());
/*$classNameRegex = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
if (substr($line, -4) == '\:\:') {
$class = substr($line,0, -4);
if (in_array($class, $avail)) {
$methods = get_class_methods($class);
foreach ($methods as $key => $method) {
$methods[$key] = $class.'::'.$method;
}
return $methods;
}
}*/
$matches = preg_grep("/^$line/", $avail);
if (!empty($matches)) {//will segfault if we return empty array after 3 times...
return $matches;
}
}
}
}