Dibi\Connection::__construct PHP Method

__construct() public method

Connection options: (see driver-specific options too) - lazy (bool) => if TRUE, connection will be established only when required - result (array) => result set options - formatDateTime => date-time format (if empty, DateTime objects will be returned) - profiler (array or bool) - run (bool) => enable profiler? - file => file to log - substitutes (array) => map of driver specific substitutes (under development)
public __construct ( $config, $name = NULL )
    public function __construct($config, $name = NULL)
    {
        if (is_string($config)) {
            parse_str($config, $config);
        } elseif ($config instanceof Traversable) {
            $tmp = [];
            foreach ($config as $key => $val) {
                $tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val;
            }
            $config = $tmp;
        } elseif (!is_array($config)) {
            throw new \InvalidArgumentException('Configuration must be array, string or object.');
        }
        Helpers::alias($config, 'username', 'user');
        Helpers::alias($config, 'password', 'pass');
        Helpers::alias($config, 'host', 'hostname');
        Helpers::alias($config, 'result|formatDate', 'resultDate');
        Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
        if (!isset($config['driver'])) {
            $config['driver'] = \dibi::$defaultDriver;
        }
        if ($config['driver'] instanceof Driver) {
            $this->driver = $config['driver'];
            $config['driver'] = get_class($this->driver);
        } elseif (is_subclass_of($config['driver'], 'Dibi\\Driver')) {
            $this->driver = new $config['driver']();
        } else {
            $class = preg_replace(['#\\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($config['driver'])));
            $class = "Dibi\\Drivers\\{$class}Driver";
            if (!class_exists($class)) {
                throw new Exception("Unable to create instance of dibi driver '{$class}'.");
            }
            $this->driver = new $class();
        }
        $config['name'] = $name;
        $this->config = $config;
        // profiler
        $profilerCfg =& $config['profiler'];
        if (is_scalar($profilerCfg)) {
            $profilerCfg = ['run' => (bool) $profilerCfg];
        }
        if (!empty($profilerCfg['run'])) {
            $filter = isset($profilerCfg['filter']) ? $profilerCfg['filter'] : Event::QUERY;
            if (isset($profilerCfg['file'])) {
                $this->onEvent[] = [new Loggers\FileLogger($profilerCfg['file'], $filter), 'logEvent'];
            }
            if (Loggers\FirePhpLogger::isAvailable()) {
                $this->onEvent[] = [new Loggers\FirePhpLogger($filter), 'logEvent'];
            }
            if (!interface_exists('Tracy\\IBarPanel') && interface_exists('Nette\\Diagnostics\\IBarPanel') && class_exists('Dibi\\Bridges\\Nette\\Panel')) {
                $panel = new Bridges\Nette\Panel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter);
                $panel->register($this);
            }
        }
        $this->substitutes = new HashMap(function ($expr) {
            return ":{$expr}:";
        });
        if (!empty($config['substitutes'])) {
            foreach ($config['substitutes'] as $key => $value) {
                $this->substitutes->{$key} = $value;
            }
        }
        if (empty($config['lazy'])) {
            $this->connect();
        }
    }