Last active
December 21, 2015 01:29
-
-
Save texdc/6228103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$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', | |
); | |
function reader($loc) { | |
if ($f = fopen($loc, 'r')) { | |
stream_set_blocking($f, 0); | |
while ($line = fgets($f)) { | |
yield $line; | |
} | |
fclose($f); | |
} else { | |
throw new \InvalidArgumentException("Invalid location [$loc]"); | |
} | |
} | |
function writer($loc) { | |
if ($f = fopen($loc, 'w')) { | |
stream_set_blocking($f, 0); | |
while (true) { | |
$line = yield; | |
fwrite($f, $line); | |
} | |
fclose($f); | |
} else { | |
throw new \InvalidArgumentException("Invalid location [$loc]"); | |
} | |
} | |
function process_files(array $files) { | |
foreach ($files as $file => $url) { | |
writer($file)->send(reader($url)); | |
yield $file; | |
} | |
} | |
function file_status($seconds) { | |
while (true) { | |
$file = yield; | |
if (file_exists($file)) { | |
sleep($seconds * 1000); | |
$mbytes = filesize($file) / (1024^2); | |
$formatted = number_format($mbytes, 3); | |
yield "$file: $formatted MiB" . PHP_EOL; | |
} | |
} | |
yield "Finished downloading $file" . PHP_EOL; | |
} | |
foreach (process_files($files) as $file) { | |
echo file_status(5)->send($file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment