Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LogException extends \Exception {
}


use Closure;
use CurlHandle;

class Updater {
Expand All @@ -38,6 +39,7 @@ class Updater {
private ?string $requestID = null;
private bool $disabled = false;
private int $previousProgress = 0;
private ?Closure $downloadProgress = null;

/**
* Updater constructor
Expand Down Expand Up @@ -561,8 +563,9 @@ private function getUpdateServerResponse(): array {
*
* @throws \Exception
*/
public function downloadUpdate(string $url = ''): void {
public function downloadUpdate(string $url = '', ?Closure $downloadProgress = null): void {
$this->silentLog('[info] downloadUpdate()');
$this->downloadProgress = $downloadProgress;

if ($url !== '') {
// If a URL is provided, use it directly
Expand Down Expand Up @@ -726,6 +729,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
// log every 2% increment for the first 10% then only log every 10% increment after that
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');

if ($this->downloadProgress) {
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions lib/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$output->write('[ ] ' . $this->checkTexts[$i] . ' ...');

$result = $this->executeStep($i);
$result = $this->executeStep($i, $output);

// Move the cursor to the beginning of the line
$output->write("\x0D");
Expand Down Expand Up @@ -387,7 +387,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* @return array{proceed:bool,response:string|list<string>} with options 'proceed' which is a boolean and defines if the step succeeded and an optional 'response' string or array
*/
protected function executeStep(int $step): array {
protected function executeStep(int $step, OutputInterface $output): array {
if ($this->updater === null) {
return ['proceed' => false, 'response' => 'Initialization problem'];
}
Expand All @@ -411,7 +411,14 @@ protected function executeStep(int $step): array {
}
break;
case 4:
$this->updater->downloadUpdate($this->urlOverride);
// Ensure that we have the same number of characters, that we want to override in the progress method
$output->write(str_pad(' 0%', 5, ' ', STR_PAD_LEFT));

$this->updater->downloadUpdate($this->urlOverride, function (int $progress, string $downloaded, string $download_size) use ($output) {
// Move cursor 5 to the left and write the new progress
$output->write("\x1B[5D");
$output->write(str_pad(' ' . $progress . '%', 5, ' ', STR_PAD_LEFT));
});
break;
case 5:
$this->updater->verifyIntegrity($this->urlOverride);
Expand Down
9 changes: 8 additions & 1 deletion lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace NC\Updater;

use Closure;
use CurlHandle;

class Updater {
Expand All @@ -20,6 +21,7 @@ class Updater {
private ?string $requestID = null;
private bool $disabled = false;
private int $previousProgress = 0;
private ?Closure $downloadProgress = null;

/**
* Updater constructor
Expand Down Expand Up @@ -543,8 +545,9 @@ private function getUpdateServerResponse(): array {
*
* @throws \Exception
*/
public function downloadUpdate(string $url = ''): void {
public function downloadUpdate(string $url = '', ?Closure $downloadProgress = null): void {
$this->silentLog('[info] downloadUpdate()');
$this->downloadProgress = $downloadProgress;

if ($url !== '') {
// If a URL is provided, use it directly
Expand Down Expand Up @@ -708,6 +711,10 @@ private function downloadProgressCallback(\CurlHandle $resource, int $download_s
// log every 2% increment for the first 10% then only log every 10% increment after that
if ($progress % 10 === 0 || ($progress < 10 && $progress % 2 === 0)) {
$this->silentLog("[info] download progress: $progress% (" . $this->formatBytes($downloaded) . ' of ' . $this->formatBytes($download_size) . ')');

if ($this->downloadProgress) {
($this->downloadProgress)($progress, $this->formatBytes($downloaded), $this->formatBytes($download_size));
}
}
}
}
Expand Down
Binary file modified updater.phar
Binary file not shown.
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'f945e59e0fa3520198947aea311fbae50bcd308f',
'reference' => 'b98632f814d1be055487cbe7763760d9846a5a93',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'f945e59e0fa3520198947aea311fbae50bcd308f',
'reference' => 'b98632f814d1be055487cbe7763760d9846a5a93',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down