robertsaupe/php-phar-selfupdate

php library for phar selfupdate with a manifest file

1.0.5 2024-02-21 10:27 UTC

This package is auto-updated.

Last update: 2024-12-21 12:14:54 UTC


README

Minimum PHP version: 8.2 Packagist Version Packagist Downloads License

php library for phar selfupdate with a manifest file

Supporting

GitHub | Patreon | PayPal | Amazon

Installing

composer require robertsaupe/php-phar-selfupdate

Getting started

Update

use Exception;
use robertsaupe\Phar\SelfUpdate\ManifestUpdate;

$updater = new ManifestUpdate('1.0.0', 'path_to_manifest.json');

try {
    $result = $updater->update();
    $newVersion = $updater->getNewVersion();
    $oldVersion = $updater->getOldVersion();
    if ($result) {
        print('Phar has been updated.'.PHP_EOL);
        print('Current version is: '.$newVersion.PHP_EOL);
        print('Previous version was: '.$oldVersion.PHP_EOL);
    } else {
        print('Phar is currently up to date.'.PHP_EOL);
        print('Current version is: '.$oldVersion.PHP_EOL);
    }
} catch (Exception $e) {
    print('Error: '.$e->getMessage().PHP_EOL);
}

Update-Check

use Exception;
use robertsaupe\Phar\SelfUpdate\ManifestUpdate;

$updater = new ManifestUpdate('1.0.0', 'path_to_manifest.json');

print('The current local version is: '.$updater->getCurrentLocalVersion().PHP_EOL);

try {
    $result = $updater->getCurrentRemoteVersion();
    if ($result) {
        print('The current '.$updater->getStability().' build available remotely is: '.$result.PHP_EOL);
    } else {
        print('You have the current '.$updater->getStability().' build installed.'.PHP_EOL);
    }
} catch (Exception $e) {
    print('Error: '.$e->getMessage().PHP_EOL);
}

Rollback

use Exception;
use robertsaupe\Phar\SelfUpdate\ManifestUpdate;

$updater = new ManifestUpdate('1.0.0', 'path_to_manifest.json');

try {
    $result = $updater->rollback();
    if ($result) {
        print('Phar has been rolled back to prior version.'.PHP_EOL);
    } else {
        print('Rollback failed for reasons unknown.'.PHP_EOL);
    }
} catch (Exception $e) {
    print('Error: '.$e->getMessage().PHP_EOL);
}

manifest.json

{
  "1.0.1": {
    "version": "1.0.1",
    "url": "url_or_path_to\/1.0.1\/file.phar",
    "sha1": "4f0dcc31a49417ee459abe5e73216f84a131fef3",
    "sha256": "2e82de11605f73732ddf0948a6c2b7235cb139bb974c1ab88b1b5dc21fcb571f",
    "sha512": "b356751b07e4626e043f65aeb85bf00b5a9933142652a17122abff0d43db3ce1371f212525b6aac011aa096592a5bcc85f14659fc2cd541f31ec2f4089931e91"
  },
  "1.0.0": {
    "version": "1.0.0",
    "url": "url_or_path_to\/1.0.0\/file.phar",
    "sha1": "95743c0d18bde06ee15878092dd2edbe6f1d2e76",
    "sha256": "5555f3d1f0567909e07bfc7cda29383cd63d9ad3d76aac88f43509904d916c23",
    "sha512": "c9bf8e3712cb913dc067533909117ad17722ef6b4c869e47c1ce8c56cda2a1d182657063eaa713cca331584f2ac548465e87d88895779b4da0b52c62b9f70a2a"
  }
}

HashAlgo

Bash

sha1sum file.phar
sha256sum file.phar
sha512sum file.phar

PHP

$sha1 = hash_file('sha1', 'file.phar');
$sha256 = hash_file('256', 'file.phar');
$sha512 = hash_file('512', 'file.phar');

Credits