PHP Classes

File: src/database.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   CMS Airship   src/database.php   Download  
File: src/database.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change: Resolve code inspection issues.
Change `use \Foo` to `use Foo`. The backslash here
is unnecessary.
Date: 7 years ago
Size: 1,434 bytes
 

Contents

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

use
Airship\Engine\{
   
Database,
   
Gears,
   
State
};

/**
 * Set up the Database objects from our database.json file
 *
 * @global State $state
 */

$dbgear = Gears::getName('Database');
$databases = \Airship\loadJSON(
   
ROOT . '/config/databases.json'
);
$dbPool = [];
$dbCount = 0;

// Needed for IDE code completion:
if (IDE_HACKS) {
   
$dbgear = new Database(new \PDO('sqlite::memory:'));
}

/**
 * Initialize all of our database connections:
 */
foreach ($databases as $label => $dbs) {
   
$dbPool[$label] = [];
    foreach (
$dbs as $dbConf) {
        if (isset(
$dbConf['driver']) || isset($dbConf['dsn'])) {
           
$conf = [
                isset(
$dbConf['dsn'])
                    ?
$dbConf['dsn']
                    :
$dbConf
           
];
           
            if (isset(
$dbConf['username']) && isset($dbConf['password'])) {
               
$conf[] = $dbConf['username'];
               
$conf[] = $dbConf['password'];
                if (isset(
$dbConf['options'])) {
                   
$conf[] = $dbConf['options'];
                }
            } elseif (isset(
$dbConf['options'])) {
               
$conf[1] = '';
               
$conf[2] = '';
               
$conf[3] = $dbConf['options'];
            }
           
$dbPool[$label][] = $conf;
            ++
$dbCount;
        }
    }
}

// Cache this array for universal usage
$state->database_connections = $dbPool;