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 return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress) {
37 switch ($notification) {
38 case STREAM_NOTIFY_CONNECT
:
41 case STREAM_NOTIFY_PROGRESS
:
42 $progress($bytes_max ?
$bytes_cur/$bytes_max : .5);
44 case STREAM_NOTIFY_COMPLETED
:
45 /* this is not generated, why? */
53 * @param bool $verbose
54 * @return \pharext\Task\Tempfile
55 * @throws \pharext\Exception
57 public function run($verbose = false) {
58 $context = $this->createStreamContext();
60 if (!$remote = fopen($this->source
, "r", false, $context)) {
64 $local = new Tempfile("remote");
65 if (!stream_copy_to_stream($remote, $local->getStream())) {
68 $local->closeStream();
70 /* STREAM_NOTIFY_COMPLETED is not generated, see above */
71 call_user_func($this->progress
, 1);