3 namespace pharext\Task
;
10 * Fetch a remote archive
12 class StreamFetch
implements Task
25 * @param string $source remote file location
26 * @param callable $progress progress callback
28 public function __construct($source, callable
$progress) {
29 $this->source
= $source;
30 $this->progress
= $progress;
33 private function createStreamContext() {
34 $progress = $this->progress
;
36 /* avoid bytes_max bug of older PHP versions */
38 return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress, &$maxbytes) {
39 if ($bytes_max > $maxbytes) {
40 $maxbytes = $bytes_max;
42 switch ($notification) {
43 case STREAM_NOTIFY_CONNECT
:
46 case STREAM_NOTIFY_PROGRESS
:
47 $progress($maxbytes > 0 ?
$bytes_cur/$maxbytes : .5);
49 case STREAM_NOTIFY_COMPLETED
:
50 /* this is sometimes not generated, why? */
58 * @param bool $verbose
59 * @return \pharext\Task\Tempfile
60 * @throws \pharext\Exception
62 public function run($verbose = false) {
63 if ($verbose !== false) {
64 printf("Fetching %s ...\n", $this->source
);
66 $context = $this->createStreamContext();
68 if (!$remote = fopen($this->source
, "r", false, $context)) {
72 $local = new Tempfile("remote");
73 if (!stream_copy_to_stream($remote, $local->getStream())) {
76 $local->closeStream();
78 /* STREAM_NOTIFY_COMPLETED is not generated, see above */
79 call_user_func($this->progress
, 1);