Exemplo n.º 1
0
 public function testReset()
 {
     $data = 'WHOA! im Data';
     TempFile::writeToFile('test', $data);
     $this->assertEquals($data, TempFile::readFromFile('test'));
     TempFile::reset('test');
     $this->assertEquals('', TempFile::readFromFile('test'));
 }
Exemplo n.º 2
0
 /**
  * This is the workhorse function that processes commands entered in the shell
  * @param string $command
  * @return void
  */
 public function doCommand($command)
 {
     $this->inReadline = false;
     // detect ctl-d
     if ($command === NULL) {
         exit(0);
     }
     // no need to process empty commands
     if (trim($command) == '') {
         return;
     }
     // internal command parser
     $matches = array();
     if (preg_match("/\\s*\\{$this->commandEscapeChar}([\\w\\?]+)\\s?(.*)/", trim($command), $matches)) {
         $internalCommand = $matches[1];
         $argsString = $matches[2];
         $args = array();
         if (preg_match_all("/(?:([\\w]+)\\s?)/", $argsString, $matches)) {
             $args = $matches[1];
         }
         if (isset($this->internalCommands[$internalCommand])) {
             $this->internalCommands[$internalCommand]->run($this, $args);
         } else {
             print "Command '{$internalCommand}' does not exist.\n";
         }
         return;
     }
     // normal command
     if (!empty($command) and function_exists('readline_add_history')) {
         readline_add_history($command);
         readline_write_history($this->historyFile());
     }
     $command = preg_replace('/^\\//', '$_', $command);
     // "/" as a command will just output the last result.
     $requires = unserialize(TempFile::readFromFile('requires'));
     if (!is_array($requires)) {
         $requires = array();
     }
     $replacments = array('{$command}' => $command, '{$requires}' => var_export($requires, true), '{$requiresFile}' => TempFile::fileName('requires'), '{$stateFile}' => TempFile::fileName('state'));
     $parsedCommand = str_replace(array_keys($replacments), array_values($replacments), self::getTemplate('command'));
     try {
         $_ = $this->lastResult;
         TempFile::writeToFile('command', $parsedCommand);
         $result = NULL;
         $output = array();
         $command_array = array($this->options[self::OPT_PHP_BIN], TempFile::fileName('command'), '2>&1');
         $lastLine = exec(implode(' ', $command_array), $output, $result);
         if ($result != 0) {
             throw new Exception("Fatal error executing php: " . join("\n", $output));
         }
         // boostrap requires environment of command
         $requires = unserialize(TempFile::readFromFile('requires'));
         foreach ($requires as $require) {
             if ($require === TempFile::fileName('command')) {
                 continue;
             }
             require_once $require;
         }
         $lastState = unserialize(TempFile::readFromFile('state'));
         $this->lastResult = $lastState['_'];
         if ($lastState['__out']) {
             print $lastState['__out'] . "\n";
         } else {
             if (is_object($this->lastResult) && !is_callable(array($this->lastResult, '__toString'))) {
                 print_r($this->lastResult) . "\n";
             } else {
                 print $this->lastResult . "\n";
             }
         }
         // after the eval, we might have new classes. Only update it if real readline is enabled
         if (!empty($this->autocompleteList)) {
             $this->autocompleteList = array_merge($this->autocompleteList, get_declared_classes());
         }
     } catch (Exception $e) {
         print "Uncaught exception with command:\n" . $e->getMessage() . "\n";
     }
 }