PHP code example of getgrav / grav-db-kit

1. Go to this page and download the library: Download getgrav/grav-db-kit 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/ */

    

getgrav / grav-db-kit example snippets


use TrilbyMedia\GravDbKit\Database\ConnectionFactory;
use TrilbyMedia\GravDbKit\Database\KitOptions;

$db = ConnectionFactory::sqlite('/path/to/user/data/helpdesk-pro/db/helpdesk.sqlite', new KitOptions(savepointPrefix: 'hd_sp_'));

// or from a plugin config array: ['type' => 'sqlite'|'mysql'|'pgsql', 'sqlite' => ['path' => …], 'mysql' => […], 'pgsql' => […]]
$db = ConnectionFactory::create($config['database'], $options);

use TrilbyMedia\GravDbKit\Database\KitTables;

$tables = KitTables::withPrefix('helpdesk');
// helpdesk_migrations (+ constraint uq_helpdesk_migrations), helpdesk_locks, helpdesk_kv, helpdesk_jobs, helpdesk_rate_limits

$tables = new KitTables(migrations: 'forum_migrations', locks: 'forum_locks', kv: 'forum_kv');

use TrilbyMedia\GravDbKit\Database\Connection;
use TrilbyMedia\GravDbKit\Database\Dialect\Dialect;
use TrilbyMedia\GravDbKit\Database\KitTables;
use TrilbyMedia\GravDbKit\Database\Migration;
use TrilbyMedia\GravDbKit\Schema\InfraTables;

return new class implements Migration {
    public function name(): string
    {
        return '0001_infra';
    }

    public function steps(Dialect $d): array
    {
        $t = KitTables::withPrefix('helpdesk');

        return [
            'create_helpdesk_kv' => InfraTables::kv($d, $t),
            'create_helpdesk_jobs' => InfraTables::jobs($d, $t),
            'create_helpdesk_rate_limits' => InfraTables::rateLimits($d, $t),
            'create_helpdesk_people' => function (Connection $c) use ($d): void {
                if (!$d->tableExists($c->pdo(), 'helpdesk_people')) {
                    $c->run("CREATE TABLE helpdesk_people (id {$d->primaryKey()}, …) {$d->tableOptions()}");
                }
            },
        ];
    }
};

use TrilbyMedia\GravDbKit\Database\Migrator;

$migrator = new Migrator($db, [$basePath, ...$addonPaths], $tables, $options);
$migrator->pending();    // ['0002_people' => ['create_helpdesk_people', …], …]
$migrator->isUpToDate();
$migrator->migrate(fn (string $migration, string $step) => print("{$migration}:{$step}\n"));

use TrilbyMedia\GravDbKit\Database\Lease;

$lease = new Lease($db, $tables, $options, $clock);

if ($lease->acquire('catalog_index', 900)) {   // false: somebody else holds it
    try { … $lease->renew('catalog_index', 900); … } finally { $lease->release('catalog_index'); }
}

$lease->run('refund:order:7', fn () => $refunds->refund(…), 60);  // throws LeaseUnavailable when held

$result = $limiter->hit('login', RateLimiter::anonymize($ip), limit: 10, windowSeconds: 900);
if (!$result->allowed) {
    header('Retry-After: ' . $result->retryAfter);
}