public function handleMessage(PhutilConsoleMessage $message)
 {
     $data = $message->getData();
     $type = $message->getType();
     switch ($type) {
         case PhutilConsoleMessage::TYPE_CONFIRM:
             $ok = phutil_console_confirm($data['prompt'], !$data['default']);
             return $this->buildMessage(PhutilConsoleMessage::TYPE_INPUT, $ok);
         case PhutilConsoleMessage::TYPE_PROMPT:
             $response = phutil_console_prompt($data['prompt'], idx($data, 'history'));
             return $this->buildMessage(PhutilConsoleMessage::TYPE_INPUT, $response);
         case PhutilConsoleMessage::TYPE_OUT:
             $this->writeText(STDOUT, $data);
             return null;
         case PhutilConsoleMessage::TYPE_ERR:
             $this->writeText(STDERR, $data);
             return null;
         case PhutilConsoleMessage::TYPE_LOG:
             if ($this->enableLog) {
                 $this->writeText(STDERR, $data);
             }
             return null;
         default:
             if ($this->handler) {
                 return call_user_func($this->handler, $message);
             } else {
                 throw new Exception("Received unknown console message of type '{$type}'.");
             }
     }
 }
 public function handleMessage(PhutilConsoleMessage $message)
 {
     $data = $message->getData();
     $type = $message->getType();
     switch ($type) {
         case PhutilConsoleMessage::TYPE_CONFIRM:
             $ok = phutil_console_confirm($data['prompt'], !$data['default']);
             return $this->buildMessage(PhutilConsoleMessage::TYPE_INPUT, $ok);
         case PhutilConsoleMessage::TYPE_PROMPT:
             $response = phutil_console_prompt($data['prompt'], idx($data, 'history'));
             return $this->buildMessage(PhutilConsoleMessage::TYPE_INPUT, $response);
         case PhutilConsoleMessage::TYPE_OUT:
             $this->writeText(STDOUT, $data);
             return null;
         case PhutilConsoleMessage::TYPE_ERR:
             $this->writeText(STDERR, $data);
             return null;
         case PhutilConsoleMessage::TYPE_LOG:
             if ($this->enableLog) {
                 $this->writeText(STDERR, $data);
             }
             return null;
         case PhutilConsoleMessage::TYPE_ENABLED:
             switch ($data['which']) {
                 case PhutilConsoleMessage::TYPE_LOG:
                     $enabled = $this->enableLog;
                     break;
                 default:
                     $enabled = true;
                     break;
             }
             return $this->buildMessage(PhutilConsoleMessage::TYPE_IS_ENABLED, $enabled);
         case PhutilConsoleMessage::TYPE_TTY:
         case PhutilConsoleMessage::TYPE_COLS:
             switch ($data['which']) {
                 case PhutilConsoleMessage::TYPE_OUT:
                     $which = STDOUT;
                     break;
                 case PhutilConsoleMessage::TYPE_ERR:
                     $which = STDERR;
                     break;
             }
             switch ($type) {
                 case PhutilConsoleMessage::TYPE_TTY:
                     if (function_exists('posix_isatty')) {
                         $is_a_tty = posix_isatty($which);
                     } else {
                         $is_a_tty = null;
                     }
                     return $this->buildMessage(PhutilConsoleMessage::TYPE_IS_TTY, $is_a_tty);
                 case PhutilConsoleMessage::TYPE_COLS:
                     // TODO: This is an approximation which might not be perfectly
                     // accurate.
                     $width = phutil_console_get_terminal_width();
                     return $this->buildMessage(PhutilConsoleMessage::TYPE_COL_WIDTH, $width);
             }
             break;
         default:
             if ($this->handler) {
                 return call_user_func($this->handler, $message);
             } else {
                 throw new Exception(pht("Received unknown console message of type '%s'.", $type));
             }
     }
 }
 private function writeMessage(PhutilConsoleMessage $message)
 {
     if ($this->disabledTypes[$message->getType()]) {
         return $this;
     }
     if ($this->flushing) {
         ob_flush();
     }
     if ($this->channel) {
         $this->channel->write($message);
         $this->channel->flush();
     } else {
         $response = $this->server->handleMessage($message);
         if ($response) {
             $this->messages[] = $response;
         }
     }
     return $this;
 }