Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

Commit

Permalink
#11 コンソールをreadlineを使い本格化
Browse files Browse the repository at this point in the history
  • Loading branch information
miyukki committed Jan 9, 2014
1 parent 8b6e220 commit a238af3
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,68 @@

require_once(dirname(__FILE__).'/Core.php');

class ConsoleController extends Controller {
public function exec() {
while(1){
echo '>>> ';
$a = eval(trim(fgets(STDIN)).';');
var_dump($a);
class ConsoleController extends Controller
{
public $user_vars = array();
public function exec()
{
// term terminal
// make c
// make t
$this->console();
}

public function vars_completion($input, $index) {
if(empty($input))
{
return array();
}

$matches = array();

$keys = array_keys($this->user_vars);
$functions = get_defined_functions();
$keys = array_merge($keys, array_values($functions['internal']), array_values(get_declared_classes()));

foreach ($keys as $key) {
if(strpos($key, $input) === 0)
{
$matches[] = $key;
}
}

return $matches;
}

private function console()
{
readline_completion_function(array($this, 'vars_completion'));
while(1)
{
$_line = readline('>>> ');
if(empty($_line))
{
continue;
}

readline_add_history($_line);

extract($this->user_vars, EXTR_SKIP);

$_code = 'return '.trim($_line, ';').';';
echo '<<< ' . var_export(@eval($_code), true) . PHP_EOL;

$this->user_vars = array();
$_vars = get_defined_vars();
foreach ($_vars as $_key => $_value)
{
if(strpos($_key, '_') === 0)
{
continue;
}
$this->user_vars[$_key] = $_value;
}

echo PHP_EOL;
}
}
Expand Down

0 comments on commit a238af3

Please sign in to comment.