PDO4You\PDO4You::getInstance PHP Метод

getInstance() публичный статический Метод

Method to get a single instance of the database per connection
public static getInstance ( string $alias = 'standard', string $type = null, string $user = null, string $pass = null, array $option = null ) : object
$alias string Pseudonym that will be used as a pointer to an instance of established connection
$type string Connection type if using "Initial Setup" or "Full DSN"
$user string Username of the database
$pass string Password of the database
$option array Configuration the connection driver
Результат object
    public static function getInstance($alias = 'standard', $type = null, $user = null, $pass = null, array $option = null)
    {
        try {
            try {
                if (!array_key_exists($alias, self::$handle)) {
                    // INI file containing the initial settings of the adapters the database
                    $ini_file = self::getSettings();
                    // Checks if the INI file exists
                    if (file_exists($ini_file)) {
                        $_dir_file = dirname($ini_file);
                        $_ini_file = basename($ini_file);
                        // Checks whether the file is readable
                        if (is_readable($ini_file)) {
                            // Interprets the file containing the initial settings
                            $datafile = self::parse_ini_file_advanced($ini_file);
                            // Initial settings for database adapters
                            if (isset($datafile['PDO4YOU_ADAPTER'])) {
                                // Captures all the names of the keys of an array
                                $Keys = function ($array) {
                                    $keys = array();
                                    foreach ($array as $key => $value) {
                                        $slice_item = array_slice($value, 0, 1);
                                        $first_item = array_shift($slice_item);
                                        if (is_array($first_item)) {
                                            foreach ($value as $key2 => $value2) {
                                                $keys[] = $key . '.' . $key2;
                                            }
                                        } else {
                                            $keys[] = $key;
                                        }
                                    }
                                    return $keys;
                                };
                                // List with the names of adapters available
                                $adapters = $Keys($datafile['PDO4YOU_ADAPTER']);
                            } else {
                                exit('The settings for existing databases, were not configured in the <strong>' . $_ini_file . '</strong>.');
                            }
                        } else {
                            exit('The <strong>' . $_ini_file . '</strong> file can not be read in the directory:<br /> ' . $_dir_file);
                        }
                    }
                    // Checks the selected adapter
                    if (isset($adapters)) {
                        $adapter = $alias == 'standard' ? static::PDO4YOU_ADAPTER : $alias;
                        if (empty($adapter)) {
                            return;
                        }
                        if ($adapter == 'vcap') {
                            $json = json_decode(getenv("VCAP_SERVICES"), true);
                            $data = $datafile['PDO4YOU_ADAPTER']['vcap'];
                            $part = preg_split('~[|]~', $data['vcap']);
                            $conf = $json[$part[0]][$part[1]]['credentials'];
                            $type = isset($data['type']) ? $data['type'] : null;
                            $host = isset($conf['hostname']) ? $conf['hostname'] : null;
                            $port = isset($conf['port']) ? $conf['port'] : null;
                            $user = isset($conf['username']) ? $conf['username'] : null;
                            $pass = isset($conf['password']) ? $conf['password'] : null;
                            $base = isset($conf['name']) ? $conf['name'] : null;
                        } else {
                            $part = preg_split('~[.]~', preg_replace('~[\\s]{1,}~', null, $adapter));
                            $conf = count($part) == 2 ? @$datafile['PDO4YOU_ADAPTER'][$part[0]][$part[1]] : @$datafile['PDO4YOU_ADAPTER'][$part[0]];
                            // Checks if the selected adapter in the instance exists
                            if (in_array($adapter, $adapters)) {
                                $type = isset($conf['type']) ? $conf['type'] : null;
                                $host = isset($conf['host']) ? $conf['host'] : null;
                                $port = isset($conf['port']) ? $conf['port'] : null;
                                $user = isset($conf['user']) ? $conf['user'] : null;
                                $pass = isset($conf['pass']) ? $conf['pass'] : null;
                                $base = isset($conf['base']) ? $conf['base'] : null;
                            }
                        }
                    }
                    // Checks the type of adapter and mounts the DNS
                    $type = strtolower($type);
                    switch ($type) {
                        case 'maria':
                            $driver = 'mysql:' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';port=' . $port . ';';
                            break;
                        case 'mysql':
                        case 'pgsql':
                        case 'cubrid':
                            $driver = $type . ':' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';port=' . $port . ';';
                            break;
                        case 'mssql':
                        case 'dblib':
                        case 'sybase':
                            $driver = $type . ':' . (!empty($base) ? 'dbname=' . $base . ';' : null) . 'host=' . $host . ';';
                            break;
                        case 'sqlsrv':
                            $driver = 'sqlsrv:' . (!empty($base) ? 'database=' . $base . ';' : null) . 'server=' . $host . ';';
                            break;
                        case 'oracle':
                            $driver = 'oci:' . (!empty($base) ? 'dbname=' . $base : null);
                            break;
                        case 'sqlite':
                            $driver = 'sqlite:' . (!empty($base) ? $base : null);
                            break;
                        default:
                            $driver = $type;
                    }
                    // Initializes the Singleton connection
                    self::singleton($alias, $driver, $user, $pass, $option);
                }
            } catch (\PDOException $e) {
                $error = self::getErrorInfo($e);
                if ($error['state'] == '42000') {
                    throw new \PDOException(self::$exception['no-database']);
                } else {
                    throw $e;
                }
            }
        } catch (\PDOException $e) {
            self::stackTrace($e);
        }
        return self::$instance;
    }

Usage Example

Пример #1
0
<?php

// Loading all the necessary files
require __DIR__ . '/src/bootstrap.php';
// Connection class imported
use PDO4You\PDO4You;
// Connection instance started and available
PDO4You::getInstance();
// Applying styling on the page
PDO4You::css();
// Displaying details on the target server's database connected
PDO4You::showServerInfo();
?>
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>PDO4You</title>
        <style>
            body { background:#FAFAFA; font:normal 12px/1.7em Bitstream Vera Sans Mono,Courier New,Monospace; margin:0; padding:0; }
            a    { color:#08C; text-decoration:none; } b { color:#CCC; }
            #pdo4you h2 { color:#000; background:#FFF; font-size:20px; display:block; margin:0; padding:10px; border-bottom:solid 1px #999; }
            .pagination { margin-top:25px;}
            .pagination a { color:#08C; background:#FFF; border:1px solid #BBB; font-size:12px; font-weight:bold; display:inline-block; min-width:13px; min-height:15px; margin:0 2px; padding:1px 6px; -webkit-border-radius:2px; -moz-border-radius:2px; border-radius:2px; text-align:center; cursor:pointer; }
            .pagination a.selected, .pagination a.selected:hover, .pagination a:hover { color:#FFF; background:#09E; border:1px solid #08C; }
            .pagination a.nolink, .pagination a.nolink:hover { color:#AAA; background:#FFF; border:1px solid #CCC; cursor:default; }
        </style>
    </head>
    <body id="pdo4you">
        <?php 
if ((string) ($page = filter_input(INPUT_GET, 'p'))) {
All Usage Examples Of PDO4You\PDO4You::getInstance