fix normal/verbose/quiet output
[pharext/pharext] / src / pharext / Task / StreamFetch.php
index 4f090efb74579b0b21970ca87ad7ee6f76a859cc..d2a9e821d9856a80012f985f3e4d29f6af69f4a8 100644 (file)
@@ -33,16 +33,21 @@ class StreamFetch implements Task
        private function createStreamContext() {
                $progress = $this->progress;
 
-               return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress) {
+               /* avoid bytes_max bug of older PHP versions */
+               $maxbytes = 0;
+               return stream_context_create([],["notification" => function($notification, $severity, $message, $code, $bytes_cur, $bytes_max) use($progress, &$maxbytes) {
+                       if ($bytes_max > $maxbytes) {
+                               $maxbytes = $bytes_max;
+                       }
                        switch ($notification) {
                                case STREAM_NOTIFY_CONNECT:
                                        $progress(0);
                                        break;
                                case STREAM_NOTIFY_PROGRESS:
-                                       $progress($bytes_max ? $bytes_cur/$bytes_max : .5);
+                                       $progress($maxbytes > 0 ? $bytes_cur/$maxbytes : .5);
                                        break;
                                case STREAM_NOTIFY_COMPLETED:
-                                       /* this is not generated, why? */
+                                       /* this is sometimes not generated, why? */
                                        $progress(1);
                                        break;
                        }
@@ -55,9 +60,12 @@ class StreamFetch implements Task
         * @throws \pharext\Exception
         */
        public function run($verbose = false) {
+               if ($verbose !== false) {
+                       printf("Fetching %s ...\n", $this->source);
+               }
                $context = $this->createStreamContext();
 
-               if (!$remote = fopen($this->source, "r", false, $context)) {
+               if (!$remote = @fopen($this->source, "r", false, $context)) {
                        throw new Exception;
                }