PHP code example of popphp / pop-audit

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

    

popphp / pop-audit example snippets


use Pop\Audit\Auditor;
use Pop\Audit\Adapter\File;

$auditor = new Auditor(new File(__DIR__ . '/tmp')); // Folder passed to the File adapter
$auditor->setModel('MyApp\Model\User', 1001);       // Model name and model ID (

$old = [
    'id'       => 1,
    'username' => 'admin',
    'email'    => '[email protected]',
    'phone'    => '504-555-5555'
];

$new = [
    'id'       => 1,
    'username' => 'admin2',
    'email'    => '[email protected]',
    'phone'    => '504-555-6666'
];

$auditor->send($old, $new);

var_dump($auditor->adapter()->getStates());

var_dump($auditor->adapter()->getStateByModel('MyApp\Model\User', 1001));

$auditor->send($old, $new, false);

use Pop\Audit\Auditor;
use Pop\Audit\Adapter\File;

$old   = ['username' => 'admin'];
$new   = ['username' => 'admin2'];
$state = [
    'id'       => 1,
    'username' => 'admin2',
    'email'    => '[email protected]',
    'phone'    => '504-555-5555'
]

$auditor = new Auditor(new File(__DIR__ . '/tmp'));
$auditor->setModel('MyApp\Model\User', 1001);
$auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$auditor->setDiff($old, $new);
$auditor->setStateData($state); // optional if you want to record the final changed state
$auditor->send();

use Pop\Audit\Auditor;
use Pop\Audit\Adapter\File;

$auditor = new Auditor(new File(__DIR__ . '/tmp')); // Folder passed to the File adapter
$auditor->setModel('MyApp\Model\User', 1001);       // Model name and model ID ( 'admin',
    'email'    => '[email protected]',
    'phone'    => '504-555-5555'
];

$new = [
    'id'       => 1,
    'username' => 'admin2',
    'email'    => '[email protected]',
    'phone'    => '504-555-6666'
];

$logFile = $auditor->send($old, $new);

class AuditLog extends \Pop\Db\Record {}

AuditLog::setDb(\Pop\Db\Db::mysqlConnect([
    'database' => 'MY_DATABASE',
    'username' => 'DB_USER',
    'password' => 'DB_PASS'
]));

use Pop\Audit\Auditor;
use Pop\Audit\Adapter\Table;

$old = [
    "id"       => 1,
    'username' => 'admin',
    'email'    => '[email protected]'
];

$new = [
    "id"       => 1,
    'username' => 'admin2',
    'email'    => '[email protected]'
];

$auditor = new Auditor(new Table('AuditLog'));
$auditor->setModel('MyApp\Model\User', 1001);
$auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$row = $auditor->send($old, $new);

use Pop\Http\Client;
use Pop\Http\Auth;
use Pop\Audit\Auditor;
use Pop\Audit\Adapter\Http;

$old = [
    "id"       => 1,
    'username' => 'admin',
    'email'    => '[email protected]'
];

$new = [
    "id"       => 1,
    'username' => 'admin2',
    'email'    => '[email protected]'
];

$client = new Client(
    'http://audit.localhost',
    Auth::createBearer('AUTH_TOKEN'),
    ['method' => 'POST']
);

$auditor = new Auditor(new Http($stream));
$auditor->setModel('MyApp\Model\User', 1001);
$auditor->setUser('testuser', 101);
$auditor->setDomain('users.localhost');
$response = $auditor->send($old, $new);