Skip to content
This repository has been archived by the owner on May 14, 2019. It is now read-only.

Commit

Permalink
Fixing broken console.
Browse files Browse the repository at this point in the history
  • Loading branch information
bermi committed Oct 15, 2011
1 parent c488f9f commit d20b1d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
12 changes: 9 additions & 3 deletions akelos_utils/contrib/iphp/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ iphp is an interactive php shell that solves a number of painful problems with n
* support for require/include; you can load php files within iphp
* extensible command system

Example:
INSTALLATION
============

pear install apinstein.pearfarm.org/iphp

EXAMPLE
=======

php> new ArrayObject(array(1,2))

Expand All @@ -26,6 +32,6 @@ Example:
php> \help
alias(es) <help>
-------------------------------------------------------
exit,die,bye,quit No help available.
exit,die,bye,quit Quit the shell.
reload Re-initialize the iphp state so it's just as if you quit and re-started.
help,? No help available.
help,? View a list of all installed commands.
31 changes: 24 additions & 7 deletions akelos_utils/contrib/iphp/iphp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class iphp
const OPT_TMP_DIR = 'tmp_dir';
const OPT_PROMPT_HEADER = 'prompt_header';
const OPT_PHP_BIN = 'php_bin';
const OPT_COMMANDS = 'commands';

/**
* Constructor
Expand Down Expand Up @@ -77,7 +78,7 @@ public function printHelp()
{
$help = "No help available.";
}
print str_pad(join(',', $aliases), $pad, ' ', STR_PAD_RIGHT) . "{$help}\n";
print str_pad($this->commandEscapeChar . join(",{$this->commandEscapeChar}", $aliases), $pad, ' ', STR_PAD_RIGHT) . "{$help}\n";
}
}

Expand All @@ -91,6 +92,7 @@ private function initializeOptions($options = array())
self::OPT_TMP_DIR => NULL,
self::OPT_PROMPT_HEADER => $this->getPromptHeader(),
self::OPT_PHP_BIN => $this->getDefaultPhpBin(),
self::OPT_COMMANDS => array(),
), $options);
}

Expand Down Expand Up @@ -143,8 +145,11 @@ private function initializeRequires()

private function initializeCommands()
{
$commandsToLoad = array('iphp_command_exit', 'iphp_command_reload', 'iphp_command_help');
$commandsToLoad = array_merge($commandsToLoad, $this->options[self::OPT_COMMANDS]);
$this->internalCommands = array();
foreach (array(new iphp_command_exit, new iphp_command_reload, new iphp_command_help) as $command) {
foreach ($commandsToLoad as $commandName) {
$command = new $commandName;
$names = $command->name();
if (!is_array($names))
{
Expand Down Expand Up @@ -268,6 +273,12 @@ public function doCommand($command)
readline_add_history($command);
readline_write_history($this->historyFile());
}

# // Replacing echo with print()
# $command = preg_replace('/^echo (.+)$/', 'print($1)', rtrim($command, ';'));
#
# // Replacing require with require()
# $command = preg_replace('/^require (.+)$/', 'require($1)', rtrim($command, ';'));

$command = preg_replace('/^\//', '$_', $command); // "/" as a command will just output the last result.

Expand All @@ -277,6 +288,12 @@ public function doCommand($command)
$requires = array();
}

if(preg_match('/^(echo|require) /', $command)){
$command = rtrim($command, ';').';';
}else{
$command = preg_match('/^(foreach|for|if|do|while) *[\({]/', $command) ? $command : '$_ = '.$command.';';
}

$parsedCommand = "<?php
foreach (" . var_export($requires, true) . " as \$file) {
require_once(\$file);
Expand All @@ -287,7 +304,7 @@ public function doCommand($command)
extract(\$__commandState);
}
ob_start();
\$_ = {$command};
$command
\$__out = ob_get_contents();
ob_end_clean();
\$__allData = get_defined_vars();
Expand Down Expand Up @@ -315,7 +332,7 @@ public function doCommand($command)
}

$lastState = unserialize(file_get_contents($this->tmpFileShellCommandState));
$this->lastResult = $lastState['_'];
$this->lastResult = @$lastState['_'];
print $this->outputPrompt;
if ($lastState['__out'])
{
Expand Down Expand Up @@ -408,8 +425,8 @@ public function runREPL()
readline_completion_function(array($this, 'readlineCompleter'));
}

// run repl loop.
if (function_exists('readline'))
// run repl loop. libedit defines readline but not callback handlers so check for both
if (function_exists('readline') && function_exists('readline_callback_handler_install'))
{
// readline automatically re-prints the prompt after the callback runs, so the only way to prevent double-prompts is to do it this way until we figure out something better
readline_callback_handler_install($this->inputPrompt, array($this, 'doCommand'));
Expand All @@ -432,4 +449,4 @@ public static function main($options = array())
$shell = new iphp($options);
$shell->runREPL();
}
}
}
1 change: 0 additions & 1 deletion akelos_utils/contrib/iphp/iphp_commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ function help()
return "Re-initialize the iphp state so it's just as if you quit and re-started.";
}
}

2 changes: 1 addition & 1 deletion akelos_utils/scripts/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

define('AK_CONSOLE_MODE', true);
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('AK_BASE_DIR') || define('AK_BASE_DIR', str_replace(DS.'akelos'.DS.'active_support'.DS.'utils'.DS.'scripts'.DS.'console.php','',__FILE__));
defined('AK_BASE_DIR') || define('AK_BASE_DIR', $_SERVER['PWD']);

$_app_config_file = AK_BASE_DIR.DS.'config'.DS.'config.php';

Expand Down

0 comments on commit d20b1d3

Please sign in to comment.