PHP Classes

File: bin/install.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   Chronicle   bin/install.php   Download  
File: bin/install.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Chronicle
Append arbitrary data to a storage container
Author: By
Last change: Boyscouting
Add pagination to /since
If database password not provided let it be empty
Enhance install script
Concurrent Chronicles

Add support for multiple instances via the ?instance=name parameter.

To implement, add something like this to your local/settings.json in the
instances key:

"public_prefix" => "table_name_prefix"

Then run bin/make-tables.php as normal.

Every instance is totally independent of each other. They have their own

* Clients
* Chain data
* Cross-Signing Targets and Policies
* Replications

If merged, I will document these features and roll it into v1.1.0
Type safety
Minor bugfixes.
Date: 1 year ago
Size: 3,524 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
GetOpt\{
   
GetOpt,
   
Option
};
use
ParagonIE\Sapient\CryptographyKeys\SigningSecretKey;

$root = \dirname(__DIR__);
/** @psalm-suppress UnresolvableInclude */
require_once $root . '/cli-autoload.php';

// Generate a signing key.
$signingKey = SigningSecretKey::generate();

// Store the signing key:
\file_put_contents(
   
$root . '/local/signing-secret.key',
   
$signingKey->getString()
);

/**
 * This defines the Command Line options.
 *
 * These are many examples:
 * php install.php
 * php install.php --mysql
 * php install.php --pgsql
 * php install.php --sqlite
 * php install.php --mysql --host localhost --port 3306 --username mysql_user --password mysql_password
 * php install.php --pgsql --host=localhost --port=5432 --username=pgsql_user --password=pgsql_password
 * php install.php --mysql --h localhost --port 3306 --u mysql_user --p mysql_password
 * php install.php --pgsql --h=localhost --port=5432 --u=pgsql_user --p=pgsql_password
 * php install.php --sqlite --database chronicle
 * php install.php --sqlite --database=chronicle --extension db
 */
$getopt = new Getopt([
    new
Option(null, 'mysql', Getopt::OPTIONAL_ARGUMENT),
    new
Option(null, 'pgsql', Getopt::OPTIONAL_ARGUMENT),
    new
Option(null, 'sqlite', Getopt::OPTIONAL_ARGUMENT),
    new
Option('h', 'host', Getopt::OPTIONAL_ARGUMENT),
    new
Option(null, 'port', Getopt::OPTIONAL_ARGUMENT),
    new
Option('d', 'database', Getopt::OPTIONAL_ARGUMENT),
    new
Option('e', 'extension', Getopt::OPTIONAL_ARGUMENT),
    new
Option('u', 'username', Getopt::OPTIONAL_ARGUMENT),
    new
Option('p', 'password', Getopt::OPTIONAL_ARGUMENT),
]);
$getopt->process();

/** @var string $mysql */
$mysql = $getopt->getOption('mysql') ?? false;
/** @var string $pgsql */
$pgsql = $getopt->getOption('pgsql') ?? false;
/** @var string $sqlite */
$sqlite = $getopt->getOption('sqlite') ?? (!$mysql && !$pgsql);
/** @var string $host */
$host = $getopt->getOption('host') ?? 'localhost';
/** @var string $port */
$port = $getopt->getOption('port') ?? ($mysql ? '3306' : ($pgsql ? '5432' : ''));
/** @var string $database */
$database = $getopt->getOption('database') ?? 'chronicle';
/** @var string $extension */
$extension = $getopt->getOption('extension') ?? 'db';
/** @var string $username */
$username = $getopt->getOption('username') ?? ($mysql ? 'mysqluser' : ($pgsql ? 'pgsqluser' : ''));
/** @var string $password */
$password = $getopt->getOption('password') ?? '';

// default SQLite
$databaseConfig = [
   
'dsn' => 'sqlite:' . $root . '/local/' . $database . '.' . $extension,
];

if(!
$sqlite){

   
$dbType = $mysql ? 'mysql' : 'pgsql';

   
$databaseConfig = [
       
'dsn' => $dbType . ':host=' . $host . ';port=' . $port . ';dbname=' . $database,
       
'username' => $username,
       
'password' => $password,
    ];
}

// Write the default settings to the local settings file.
$localSettings = [
   
'database' => $databaseConfig,
   
// Map 'channel-name' => 'table_prefix'
   
'instances' => [
       
'' => ''
   
],
   
'paginate-export' => null,
   
'publish-new-clients' => true,
   
'publish-revoked-clients' => true,
   
// The maximum window of opportunity for replay attacks:
   
'request-timeout' => '10 minutes',
   
'scheduled-attestation' => '7 days',
   
'signing-public-key' => $signingKey->getPublicKey()->getString()
];

\
file_put_contents(
   
$root . '/local/settings.json',
    \
json_encode($localSettings, JSON_PRETTY_PRINT)
);