Resque\Socket\Server::fwrite PHP Method

fwrite() public static method

Write a string to a resource
public static fwrite ( resource $fh, string $string )
$fh resource The resource to write to
$string string The string to write
    public static function fwrite($fh, $string)
    {
        for ($written = 0; $written < strlen($string); $written += $fwrite) {
            if (($fwrite = fwrite($fh, substr($string, $written))) === false) {
                return $written;
            }
        }
        return $written;
    }

Usage Example

Beispiel #1
1
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $host = $this->getConfig('connecthost');
     $port = $this->getConfig('connectport');
     $timeout = $this->getConfig('connecttimeout');
     $conn = $host . ':' . $port;
     $prompt = 'php-resque ' . $conn . '> ';
     $output->writeln('<comment>Connecting to ' . $conn . '...</comment>');
     if (!($fh = @fsockopen('tcp://' . $host, $port, $errno, $errstr, $timeout))) {
         $output->writeln('<error>[' . $errno . '] ' . $errstr . ' host ' . $conn . '</error>');
         return;
     }
     // Set socket timeout to 200ms
     stream_set_timeout($fh, 0, 200 * 1000);
     $stdin = fopen('php://stdin', 'r');
     $prompting = false;
     Resque\Socket\Server::fwrite($fh, 'shell');
     while (true) {
         if (feof($fh)) {
             $output->writeln('<comment>Connection to ' . $conn . ' closed.</comment>');
             break;
         }
         $read = array($fh, $stdin);
         $write = null;
         $except = null;
         $selected = @stream_select($read, $write, $except, 0);
         if ($selected > 0) {
             foreach ($read as $r) {
                 if ($r == $stdin) {
                     $input = trim(fgets($stdin));
                     if (empty($input)) {
                         $output->write($prompt);
                         $prompting = true;
                     } else {
                         Resque\Socket\Server::fwrite($fh, $input);
                         $prompting = false;
                     }
                 } elseif ($r == $fh) {
                     $input = '';
                     while (($buffer = fgets($fh, 1024)) !== false) {
                         $input .= $buffer;
                     }
                     if ($prompting) {
                         $output->writeln('');
                     }
                     $output->writeln('<pop>' . trim($input) . '</pop>');
                     if (!feof($fh)) {
                         $output->write($prompt);
                         $prompting = true;
                     }
                 }
             }
         }
         // Sleep for 10ms to stop CPU spiking
         usleep(10 * 1000);
     }
     fclose($fh);
 }
All Usage Examples Of Resque\Socket\Server::fwrite