more polishing
[pharext/replicator.pharext.org] / bin / fetch
1 #!/usr/bin/php
2 <?php
3
4 ini_set("display_errors", 0);
5 ini_set("log_errors", 1);
6
7 use http\Url;
8 use http\Client;
9 use http\Client\Request;
10
11 $procs = [];
12 $client = new Client;
13 $client->setOptions([
14 "timetout" => 60,
15 "connecttimeout" => 6
16 ]);
17
18 while (!feof(STDIN)) {
19 if (strlen($line = rtrim(fgets(STDIN)))) {
20 if (list($pkg, $ver, $url) = explode("\t", $line)) {
21 $url = new Url($url, null, 0);
22 $skp = sprintf("%s/../build/%s-%s.skip", __DIR__, $pkg, $ver);
23 $tgz = sprintf("%s/../build/%s-%s.tgz", __DIR__, $pkg, $ver);
24 $ext = sprintf("%s/../public/phars/%s/%s-%s.ext.phar", __DIR__, $pkg, $pkg, $ver);
25
26 if (is_file($skp)) {
27 // skip
28 } elseif (is_file($tgz) || is_file($ext)) {
29 printf("%s\t%s\t%s\n", $pkg, $ver, $tgz);
30 } else {
31 $client->enqueue(new Request("GET", $url), function($res) use($tgz, $pkg, $ver) {
32 $res->getBody()->toStream(fopen($tgz, "w"));
33 printf("%s\t%s\t%s\n", $pkg, $ver, $tgz);
34 return true;
35 });
36 }
37 }
38 }
39 }
40
41 while (count($client)) {
42 try {
43 $client->send();
44 } catch (Exception $e) {
45 error_log($e->getMessage());
46 }
47 }