PHP code example of uzapoint / auditable

1. Go to this page and download the library: Download uzapoint/auditable 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/ */

    

uzapoint / auditable example snippets


'providers' => [
    Uzapoint\EventBus\EventBusServiceProvider::class,
    Uzapoint\Auditable\AuditServiceProvider::class,
],

'connections' => [
    // ...

    'rabbitmq' => [
        'host' => env('RABBITMQ_HOST', '127.0.0.1'),
        'port' => env('RABBITMQ_PORT', 5672),
        'user' => env('RABBITMQ_USER', 'guest'),
        'password' => env('RABBITMQ_PASSWORD', 'guest'),
        'vhost' => env('RABBITMQ_VHOST', '/'),
    ],
],

return [
    'service' => [
        'name' => env('AUDIT_SERVICE_NAME', config('app.name')),
        'environment' => env('AUDIT_SERVICE_ENV', config('app.env')),
    ],

    'events' => [
        'created',
        'updated',
        'deleted',
        'voided',
        'approved',
        'rejected',
        'restored',
    ],

    'sensitive_fields' => [
        'password',
        'password_confirmation',
        'token',
        'api_token',
        'secret',
        'credit_card',
        'cvv',
        'ssn',
    ],
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Uzapoint\Auditable\Traits\Auditable;

class Order extends Model
{
    use Auditable;
    use SoftDeletes;

    protected $fillable = [
        'customer_id',
        'status',
        'total',
        'void_reason',
        'rejection_reason',
    ];
}

use Uzapoint\Auditable\Facades\Audit;

Audit::log('invoice.sent', 'Invoice #123 was sent to the customer', [
    'log_name' => 'invoices',
    'subject_type' => App\Models\Invoice::class,
    'subject_id' => 123,
    'causer_id' => auth('api')->id(),
    'causer_type' => App\Models\User::class,
    'causer_name' => optional(auth('api')->user())->name,
    'causer_email' => optional(auth('api')->user())->email,
    'changes' => null,
    'properties' => [
        'channel' => 'email',
    ],
]);

use Uzapoint\Auditable\Context\UserContextResolver;

UserContextResolver::set($message['user_context']);

try {
    $order->update(['status' => 'approved']);
} finally {
    UserContextResolver::clear();
}

'user_context' => UserContextResolver::forMessage(),

use Uzapoint\Auditable\Context\BatchContext;

$batchUuid = BatchContext::set();

try {
    $invoice->update(['status' => 'paid']);
    $payment->update(['status' => 'reconciled']);
} finally {
    BatchContext::clear();
}

use Uzapoint\Auditable\Jobs\ProcessAuditEvents;

return [
    'exchanges' => [
        'audit.events',
    ],

    'queues' => [
        [
            'name' => 'audit_service.audit_events',
            'exchange' => 'audit.events',
            'routing_keys' => [
                'audit.events',
            ],
        ],
    ],

    'handlers' => [
        'audit.events' => ProcessAuditEvents::class,
    ],

    'dead_letter' => [
        'enabled' => true,
        'ttl' => 86400000,
        'max_retries' => 3,
        'exchange_prefix' => 'dlx.',
    ],
];
bash
php artisan vendor:publish --tag=auditable
bash
php artisan vendor:publish --tag=auditable-config
php artisan vendor:publish --tag=auditable-migrations
bash
php artisan vendor:publish --tag=auditable-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=eventbus-config
bash
php artisan eventbus:consume --queue=audit_service.audit_events