/** * @param string $path * @return string */ function parse($path) { if (!file_exists($this->pathMails = $this->config['cacheDir'] . DIRECTORY_SEPARATOR . $this->siteHash . DIRECTORY_SEPARATOR . $path)) { mkdir($this->pathMails); } foreach ($this->getLinks() as $file => $url) { $readStream = fopen($url, 'r'); $writeStream = fopen($this->pathSiteHash . DIRECTORY_SEPARATOR . $file, 'w'); stream_set_blocking($readStream, 0); stream_set_blocking($writeStream, 0); $read = new \React\Stream\Stream($readStream, $this->loop); $write = new \React\Stream\Stream($writeStream, $this->loop); $read->on('end', function () use($file, &$files) { $path = $this->pathSiteHash . DIRECTORY_SEPARATOR . $file; $crawler = new Crawler(); $crawler->add(file_get_contents($path)); $arrLinks = $crawler->filter('a')->each(function (Crawler $nodeCrawler) { return [$nodeCrawler->filter('a')->attr('href')]; }); $validMails = []; foreach ($arrLinks as $k => $url) { if (filter_var($url[0], FILTER_VALIDATE_EMAIL)) { $validMails[] = $url[0]; } else { if (filter_var($m = str_replace('mailto:', '', $url[0]), FILTER_VALIDATE_EMAIL)) { $validMails[] = $m; } } } $mails = []; foreach ($validMails as $m) { array_push($mails, str_replace('mailto:', '', $m)); } file_put_contents($this->pathMails . DIRECTORY_SEPARATOR . $file, implode(PHP_EOL, $mails)); unset($files[$file]); }); $read->pipe($write); } // каждые $this->config['periodTime'] секунд выполнять какое-то действие $this->loop->addPeriodicTimer($this->config['periodTime'], function ($timer) use(&$files) { if (0 === count($files)) { $timer->cancel(); } echo PHP_EOL . "Passed {$this->config['periodTime']} sec. " . PHP_EOL; }); echo "This script will show the download status every {$this->config['periodTime']} seconds." . PHP_EOL; $this->loop->run(); return 'Dir of result in: ' . $this->config['cacheDir'] . DIRECTORY_SEPARATOR . $this->siteHash . DIRECTORY_SEPARATOR . $path; }
public function start(Output\OutputInterface $output) { /* @var $_loop \React\EventLoop\LoopInterface*/ $loop = \React\EventLoop\Factory::create(); // create a new socket $socket = new \React\Socket\Server($loop); // pipe a connection into itself $socket->on('connection', function (\React\Socket\Connection $conn) use($output, $loop) { $output->writeln('CONNECTION ESTABLISHED: ' . $conn->getRemoteAddress()); //$infiniteStreamHandle = fopen('/tmp/random', 'r'); $infiniteStreamHandle = fopen('/dev/urandom', 'r'); $fileToStream = new \React\Stream\Stream($infiniteStreamHandle, $loop); $output->writeln('streaming ...'); //$conn->pipe($infiniteStreamHandle); $fileToStream->pipe($conn); }); echo "Socket server listening on port 4000.\n"; echo "You can connect to it by running: telnet localhost 4000\n"; $socket->listen(4000); $loop->run(); }
$t = isset($args['t']) ? $args['t'] : 1; // passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465) $if = str_replace('/dev/fd/', 'php://fd/', $if); $of = str_replace('/dev/fd/', 'php://fd/', $of); $loop = new React\EventLoop\StreamSelectLoop(); // setup information stream $info = new React\Stream\Stream(STDERR, $loop); $info->pause(); if (extension_loaded('xdebug')) { $info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL); } $info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...' . PHP_EOL); // setup input and output streams and pipe inbetween $in = new React\Stream\Stream(fopen($if, 'r'), $loop); $out = new React\Stream\Stream(fopen($of, 'w'), $loop); $out->pause(); $in->pipe($out); // stop input stream in $t seconds $start = microtime(true); $timeout = $loop->addTimer($t, function () use($in, &$bytes) { $in->close(); }); // print stream position once stream closes $in->on('close', function () use($in, $start, $timeout, $info) { $t = microtime(true) - $start; $timeout->cancel(); $bytes = ftell($in->stream); $info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL); $info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL); }); $loop->run();
// downloading the two best technologies ever in parallel require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $files = array('node-v0.6.18.tar.gz' => 'http://nodejs.org/dist/v0.6.18/node-v0.6.18.tar.gz', 'php-5.4.3.tar.gz' => 'http://it.php.net/get/php-5.4.3.tar.gz/from/this/mirror'); $buffers = array(); foreach ($files as $file => $url) { $readStream = fopen($url, 'r'); $writeStream = fopen($file, 'w'); stream_set_blocking($readStream, 0); stream_set_blocking($writeStream, 0); $read = new React\Stream\Stream($readStream, $loop); $write = new React\Stream\Stream($writeStream, $loop); $read->on('end', function () use($file, &$files) { unset($files[$file]); echo "Finished downloading {$file}\n"; }); $read->pipe($write); } $loop->addPeriodicTimer(5, function ($timer) use(&$files) { if (0 === count($files)) { $timer->cancel(); } foreach ($files as $file => $url) { $mbytes = filesize($file) / (1024 * 1024); $formatted = number_format($mbytes, 3); echo "{$file}: {$formatted} MiB\n"; } }); echo "This script will show the download status every 5 seconds.\n"; $loop->run();
$gifServer->addFrame(createGifFrame([''])); }; } $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket); $gifServer = new React\Gifsocket\Server($loop); $messages = []; $addMessage = function ($message) use($gifServer, &$messages) { $messages[] = $message; if (count($messages) > 18) { $messages = array_slice($messages, count($messages) - 18); } $frame = createGifFrame($messages); $gifServer->addFrame($frame); }; $router = new React\Gifsocket\Router(['/socket.gif' => sendEmptyFrameAfter($gifServer), '/' => function ($request, $response) use($loop) { $response->writeHead(200, ['Content-Type' => 'text/html']); $fd = fopen(__DIR__ . '/views/index.html', 'r'); $template = new React\Stream\Stream($fd, $loop); $template->pipe($response); }, '/message' => function ($request, $response) use($addMessage) { $message = $request->getQuery()['message']; $addMessage($message); $response->writeHead(200); $response->end(); }]); $http->on('request', $router); echo "Webserver running on localhost:8080\n"; $socket->listen(8080); $loop->run();
<?php require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $in = new React\Stream\Stream(STDIN, $loop); $out = new React\Stream\Stream(STDOUT, $loop); $compressor = Clue\React\Zlib\ZlibFilterStream::createGzipCompressor(1); $in->pipe($compressor)->pipe($out); $loop->run();
<?php require 'vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $read = new \React\Stream\Stream(STDIN, $loop); $read->on('data', function ($data) use($loop) { $data = trim($data); if ($data == 15) { $loop->stop(); } }); $read->pipe(new \React\Stream\Stream(STDOUT, $loop)); $loop->run();