PHP code example of laravel-chronicle / core
1. Go to this page and download the library: Download laravel-chronicle/core library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laravel-chronicle / core example snippets
use Chronicle\Facades\Chronicle;
Chronicle::record()
->actor($user)
->action('order.created')
->subject($order)
->metadata([
'total' => 1000,
'currency' => 'USD',
])
->tags(['orders', 'billing'])
->commit();
use Chronicle\Eloquent\HasChronicle;
class Invoice extends Model
{
use HasChronicle;
}
// config/chronicle.php
'signing' => [
'active' => env('CHRONICLE_ACTIVE_KEY', 'chronicle-key-1'),
'keys' => [
'chronicle-key-1' => [
'provider' => Chronicle\Signing\Ed25519SigningProvider::class,
'algorithm' => 'ed25519',
'private_key' => env('CHRONICLE_PRIVATE_KEY'), // null once retired
'public_key' => env('CHRONICLE_PUBLIC_KEY'), // keep for verification
],
],
],
use Chronicle\Entry\Entry;
Entry::forActor($user)->get();
Entry::forSubject($order)->get();
Entry::action('order.created')->get();
Entry::withTag('orders')->get();
Entry::stream()->each(fn ($entry) => /* process */);
Entry::cursorPaginateLedger(50);
use Chronicle\Facades\Chronicle;
$ref = Chronicle::resolveReference($entry->subject_type, $entry->subject_id);
$ref->class; // resolved FQCN, or null when unknown
$ref->label; // e.g. "Order #123" - humanised basename + id
$ref->exists();
Chronicle::referenceLabel($entry->actor_type, $entry->actor_id); // "System", "User #5", …
$model = Chronicle::referenceModel($entry->subject_type, $entry->subject_id); // ?Model
$name = Chronicle::referenceLabel($entry->subject_type, $entry->subject_id, hydrate: true);
use Chronicle\Testing\LedgerSeeder;
$seeded = LedgerSeeder::make()
->count(1000)
->checkpointEvery(100) // periodic signed checkpoints (+ a final one)
->action(fn (int $i) => "order.$i")
->subject(fn (int $i) => Order::factory()->create())
->seed();
$seeded->entries; // 1000
$seeded->checkpoints; // 10
$seeded->lastCheckpointId; // string|null
// config/chronicle.php
'anchoring' => [
'enabled' => env('CHRONICLE_ANCHORING_ENABLED', false),
'providers' => [
'tsa' => [
'provider' => Chronicle\Anchoring\Rfc3161TimestampAnchor::class,
'tsa_url' => env('CHRONICLE_TSA_URL'),
'tsa_certificate' => env('CHRONICLE_TSA_CERTIFICATE'),
],
],
],
use Chronicle\Verification\IntegrityVerifier;
$result = app(IntegrityVerifier::class)->verifyEntryRange($fromSequence, $toSequence);
// config/chronicle.php
'encryption' => [
'enabled' => env('CHRONICLE_ENCRYPTION_ENABLED', false),
'fields' => ['metadata', 'context', 'diff'], // PII-bearing fields, per-subject DEK
'kek' => [
'provider' => Chronicle\Encryption\LocalKeyEncryptionProvider::class,
'key' => env('CHRONICLE_ENCRYPTION_KEY'), // dedicated base64 32-byte key, NOT the app key
'id' => env('CHRONICLE_ENCRYPTION_KEK_ID', 'local'),
],
],
// config/chronicle.php
'models' => [
'entry' => \App\Models\AuditEntry::class,
],
namespace App\Models;
use Chronicle\Entry\Entry;
class AuditEntry extends Entry
{
// add accessors / relationships here
}
bash
php artisan chronicle:checkpoint
bash
php artisan chronicle:checkpoint --anchor # anchor synchronously
php artisan chronicle:anchor:retry # retry pending/failed anchors
php artisan chronicle:anchor:verify # attest stored anchors against their sinks
bash
php artisan chronicle:verify --checkpoints-only --anchors
bash
php artisan chronicle:export storage/app/chronicle-export
php artisan chronicle:verify-export storage/app/chronicle-export