/**
  * 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)
  * @param  mixed   connection parameters
  * @param  string  connection name
  * @throws DibiException
  */
 public function __construct($config, $name = NULL)
 {
     class_exists('dibi');
     // ensure class dibi is loaded
     // DSN string
     if (is_string($config)) {
         parse_str($config, $config);
     } elseif ($config instanceof Traversable) {
         $tmp = array();
         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.');
     }
     self::alias($config, 'username', 'user');
     self::alias($config, 'password', 'pass');
     self::alias($config, 'host', 'hostname');
     self::alias($config, 'result|formatDate', 'resultDate');
     self::alias($config, 'result|formatDateTime', 'resultDateTime');
     if (!isset($config['driver'])) {
         $config['driver'] = dibi::$defaultDriver;
     }
     $driver = preg_replace('#[^a-z0-9_]#', '_', strtolower($config['driver']));
     $class = "Dibi" . $driver . "Driver";
     if (!class_exists($class, FALSE)) {
         include_once dirname(__FILE__) . "/../drivers/{$driver}.php";
         if (!class_exists($class, FALSE)) {
             throw new DibiException("Unable to create instance of dibi driver '{$class}'.");
         }
     }
     $config['name'] = $name;
     $this->config = $config;
     $this->driver = new $class();
     $this->translator = new DibiTranslator($this);
     // profiler
     $profilerCfg =& $config['profiler'];
     if (is_scalar($profilerCfg)) {
         $profilerCfg = array('run' => (bool) $profilerCfg);
     }
     if (!empty($profilerCfg['run'])) {
         $filter = isset($profilerCfg['filter']) ? $profilerCfg['filter'] : DibiEvent::QUERY;
         if (isset($profilerCfg['file'])) {
             $this->onEvent[] = array(new DibiFileLogger($profilerCfg['file'], $filter), 'logEvent');
         }
         if (DibiFirePhpLogger::isAvailable()) {
             $this->onEvent[] = array(new DibiFirePhpLogger($filter), 'logEvent');
         }
         if (class_exists('DibiNettePanel', FALSE)) {
             $panel = new DibiNettePanel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter);
             $panel->register($this);
         }
     }
     $this->substitutes = new DibiHashMap(create_function('$expr', 'return ":$expr:";'));
     if (!empty($config['substitutes'])) {
         foreach ($config['substitutes'] as $key => $value) {
             $this->substitutes->{$key} = $value;
         }
     }
     if (empty($config['lazy'])) {
         $this->connect();
     }
 }
Пример #2
0
 /**
  * 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)
  * @param  mixed   connection parameters
  * @param  string  connection name
  * @throws DibiException
  */
 public function __construct($config, $name = NULL)
 {
     if (is_string($config)) {
         parse_str($config, $config);
     } elseif ($config instanceof Traversable) {
         $tmp = array();
         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.');
     }
     self::alias($config, 'username', 'user');
     self::alias($config, 'password', 'pass');
     self::alias($config, 'host', 'hostname');
     self::alias($config, 'result|formatDate', 'resultDate');
     self::alias($config, 'result|formatDateTime', 'resultDateTime');
     if (!isset($config['driver'])) {
         $config['driver'] = dibi::$defaultDriver;
     }
     if ($config['driver'] instanceof IDibiDriver) {
         $this->driver = $config['driver'];
         $config['driver'] = get_class($this->driver);
     } elseif (PHP_VERSION_ID >= 50307 && is_subclass_of($config['driver'], 'IDibiDriver')) {
         $this->driver = new $config['driver']();
     } else {
         $class = preg_replace(array('#\\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver'])));
         $class = "Dibi{$class}Driver";
         if (!class_exists($class)) {
             include_once dirname(__FILE__) . "/../drivers/{$class}.php";
             if (!class_exists($class, FALSE)) {
                 throw new DibiException("Unable to create instance of dibi driver '{$class}'.");
             }
         }
         $this->driver = new $class();
     }
     $config['name'] = $name;
     $this->config = $config;
     $this->translator = new DibiTranslator($this);
     // profiler
     $profilerCfg =& $config['profiler'];
     if (is_scalar($profilerCfg)) {
         $profilerCfg = array('run' => (bool) $profilerCfg);
     }
     if (!empty($profilerCfg['run'])) {
         $filter = isset($profilerCfg['filter']) ? $profilerCfg['filter'] : DibiEvent::QUERY;
         if (isset($profilerCfg['file'])) {
             $this->onEvent[] = array(new DibiFileLogger($profilerCfg['file'], $filter), 'logEvent');
         }
         if (DibiFirePhpLogger::isAvailable()) {
             $this->onEvent[] = array(new DibiFirePhpLogger($filter), 'logEvent');
         }
         if (!interface_exists('Tracy\\IBarPanel') && interface_exists('Nette\\Diagnostics\\IBarPanel') && class_exists('DibiNettePanel')) {
             $panel = new DibiNettePanel(isset($profilerCfg['explain']) ? $profilerCfg['explain'] : TRUE, $filter);
             $panel->register($this);
         }
     }
     $this->substitutes = new DibiHashMap(create_function('$expr', 'return ":$expr:";'));
     if (!empty($config['substitutes'])) {
         foreach ($config['substitutes'] as $key => $value) {
             $this->substitutes->{$key} = $value;
         }
     }
     if (empty($config['lazy'])) {
         $this->connect();
     }
 }