boot.php 1.14 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * Boot the application, loading config and database connection.
 */

declare(strict_types=1);

9
use DetectLanguage\DetectLanguage;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
use RedBeanPHP\R;

/**
 * Helper to get config values.
 *
 * @param null|string $param
 * @param null|mixed  $default
 *
 * @return mixed|null
 */
function c(string $param = null, $default = null)
{
    static $config;
    if ($config === null) {
        $config = require __DIR__ . '/config.php';
        is_array($config) || die('Invalid config format.');
    }

    if ($param === null) {
        return $config;
    }

    if (array_key_exists($param, $config)) {
        return $config[$param];
    }

    return $default;
}

define('PODUPTIME', microtime(true));

require_once __DIR__ . '/vendor/autoload.php';

43 44 45 46
// Initialise language detection.
DetectLanguage::setApiKey(c('dlkey'));
DetectLanguage::setSecure(true);

47 48 49 50 51 52 53 54 55 56 57 58 59 60
// Set up global DB connection.
R::setup(
    sprintf(
        'pgsql:host=%s;port=%d;dbname=%s',
        c('pghost'),
        c('pgport', 5432),
        c('pgdb')
    ),
    c('pguser'),
    c('pgpass'),
    true
);
R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);