effectiveactivism/schema-api-updater
Installation
composer require effectiveactivism/schema-api-updater
Ensure your project uses Laravel 8+ (check composer.json for compatibility).
Publish Config
php artisan vendor:publish --provider="EffectiveActivism\SchemaApiUpdater\SchemaApiUpdaterServiceProvider" --tag="config"
Review config/schema-api-updater.php for API endpoints, authentication, and update strategies.
First Use Case: Fetching a Schema
use EffectiveActivism\SchemaApiUpdater\Facades\SchemaUpdater;
$schema = SchemaUpdater::fetch('https://example.com/schema.json');
fetch($url): Retrieves a schema from a remote URL.update($schema, $strategy = 'merge'): Updates a local schema with remote changes (supports merge, replace, or diff strategies).Define a Schema Model
Extend EffectiveActivism\SchemaApiUpdater\Contracts\Schema or use Eloquent:
use Illuminate\Database\Eloquent\Model;
class AppSchema extends Model implements \EffectiveActivism\SchemaApiUpdater\Contracts\Schema {
protected $fillable = ['name', 'content', 'updated_at'];
}
Create an Updater Job Use Laravel Queues to avoid blocking requests:
use EffectiveActivism\SchemaApiUpdater\Jobs\UpdateSchemaJob;
UpdateSchemaJob::dispatch(
schema: $appSchema,
url: 'https://example.com/latest.json',
strategy: 'merge'
);
Event-Driven Updates Listen for schema changes via events:
SchemaUpdater::on('schema.updated', function ($schema) {
// Trigger downstream actions (e.g., cache invalidation)
});
SchemaUpdater::fetch($url, cacheMinutes: 60);
SchemaUpdated event to notify external services (e.g., via Laravel Horizon or a queue worker).SchemaUpdater::validate($schema, ['required' => ['properties.id']]);
Authentication
config/schema-api-updater.php:
'auth' => [
'token' => env('SCHEMA_API_TOKEN'),
'header' => 'Authorization',
],
.env or a secrets manager.Strategy Conflicts
merge strategy prioritizes remote changes over local ones. Use replace cautiously:
SchemaUpdater::update($localSchema, 'replace'); // Overwrites entirely!
Rate Limiting
$this->handle()->retryAfter(10); // Retry after 10 seconds
'debug' => true in config to log API responses and errors to storage/logs/laravel.log.https://example.com/schema.json, not /schema.json).Custom Strategies
Implement EffectiveActivism\SchemaApiUpdater\Contracts\UpdateStrategy:
class CustomStrategy implements UpdateStrategy {
public function apply($local, $remote) {
// Custom logic (e.g., deep merge with conflict resolution)
}
}
Register it in config/schema-api-updater.php:
'strategies' => [
'custom' => \App\Strategies\CustomStrategy::class,
],
Pre/Post Hooks
Use the schema.updating and schema.updated events to inject logic:
SchemaUpdater::on('schema.updating', function ($schema, $remote) {
// Sanitize or transform $remote before update
});
How can I help you explore Laravel packages today?