herrera-io/phar-update
Self-update library for PHP applications distributed as PHAR files. Loads a remote manifest, checks available versions, and upgrades to the next compatible release. Modular design lets you customize the update workflow for your PHAR app.
Installation:
composer require herrera-io/phar-update:1.*
(Note: The package is archived, but version 1.* remains functional.)
Basic Usage:
Create a manifest.json file (hosted publicly) with your Phar update metadata:
{
"version": "1.2.0",
"url": "https://example.com/path/to/your.phar",
"checksum": "sha256:abc123...",
"require": ["php": ">=7.4"]
}
Load it in your Phar’s bootstrap:
use Herrera\Phar\Update\Manager;
$manager = new Manager(Manifest::loadFile('https://yourdomain.com/manifest.json'));
First Use Case: Check for updates and auto-update if available:
$manager->update('1.0.0', true); // Auto-update if newer than 1.0.0
Version-Based Updates:
update($currentVersion, $autoUpdate) to trigger updates.true for $autoUpdate to silently replace the Phar if a newer version exists.Pre-Update Hooks:
Herrera\Phar\Update\Manifest to add custom validation:
$manifest = new Manifest([
'version' => '1.2.0',
'url' => '...',
'preUpdate' => function() {
// Backup user data, log, etc.
}
]);
Phar-Specific Integration:
Manager in your Phar’s bootstrap (e.g., phar://your-app.phar/bootstrap.php).Phar::running() to detect if the script is running from a Phar:
if (Phar::running()) {
$manager->update('1.0.0', true);
}
Fallback Logic:
try {
$manager->update('1.0.0', true);
} catch (Exception $e) {
error_log("Update failed: " . $e->getMessage());
// Fallback to local cache or user prompt
}
Custom Update Sources:
Override Herrera\Phar\Update\Manifest::loadFile() to fetch manifests from private APIs or databases.
Delta Updates:
Combine with phar.io/phar to implement delta updates (e.g., only patch changed files).
User Feedback:
Use Manifest::setOptions(['verbose' => true]) to log update steps to stderr.
Phar Permissions:
www-data) has write access:
chown www-data:www-data /path/to/phar/
chmod 755 /path/to/phar/
Manifest Validation:
checksum field in manifest.json to prevent tampering.hash_file('sha256', $pharPath) to verify the downloaded Phar.Phar Signature:
phar.io/phar), the update will break the signature.Phar::mapPhar().PHP Version Mismatches:
require field in the manifest is not enforced by this library.if (version_compare(PHP_VERSION, '7.4') < 0) {
throw new RuntimeException("PHP 7.4+ required");
}
Archived Package:
Enable Verbose Mode:
$manager->setOptions(['verbose' => true]);
Logs will show:
[DEBUG] Downloading manifest from https://example.com/manifest.json
[DEBUG] Current version: 1.0.0, Available: 1.2.0
[DEBUG] Downloading update from https://example.com/app.phar
Check HTTP Errors:
Use Manifest::loadFile() with a custom HTTP client (e.g., Guzzle) to debug 404/500 errors:
$client = new \GuzzleHttp\Client();
$manifest = Manifest::loadFile('https://...', $client);
Custom Manifest Sources:
Implement Herrera\Phar\Update\ManifestSourceInterface for non-HTTP sources (e.g., S3, local files).
Post-Update Actions:
Extend the Manager to run scripts after update:
$manager->update('1.0.0', true, function() {
// Clear cache, restart services, etc.
});
Offline Updates:
Cache manifests locally and use Manifest::loadArray() to avoid network calls:
$manifestData = file_get_contents('cache/manifest.json');
$manager = new Manager(Manifest::loadArray(json_decode($manifestData, true)));
How can I help you explore Laravel packages today?