/** * @param \Doctrine\Bundle\DoctrineBundle\Registry $doctrine */ public function __construct($doctrine) { $dbConfig = $doctrine->getConnection()->getParams(); if (isset($dbConfig['dsn'])) { $data = EhrlichAndreas_Util_Dsn::parseDsn($dbConfig['dsn']); foreach ($data as $key => $value) { $dbConfig[$key] = $value; } $data = EhrlichAndreas_Util_Dsn::parseUri($dbConfig['dsn']); if (isset($data[0])) { $dbConfig['adapter'] = ucfirst($data[0]); } if (isset($dbConfig['adapter']) && $dbConfig['driver']) { $dbConfig['adapter'] = $dbConfig['driver'] . '_' . $dbConfig['adapter']; } } if (!isset($dbConfig['adapter']) && isset($dbConfig['driver'])) { $dbConfig['adapter'] = $dbConfig['driver']; unset($dbConfig['driver']); } if (!isset($dbConfig['driver_options']) && isset($dbConfig['driverOptions'])) { $dbConfig['driver_options'] = $dbConfig['driverOptions']; unset($dbConfig['driverOptions']); } if (!isset($dbConfig['username']) && isset($dbConfig['user'])) { $dbConfig['username'] = $dbConfig['user']; unset($dbConfig['user']); } $this->adapter = EhrlichAndreas_Db_Db::factory($dbConfig); $this->adapter->setConnection($doctrine->getConnection()->getWrappedConnection()); }
/** * Creates a PDO instance representing a connection to a database * * @link http://www.php.net/manual/en/pdo.construct.php * @param * dsn * @param * username * @param * passwd * @param * options[optional] */ public function __construct($dsn, $username = '', $passwd = '', $options = array()) { if (!is_array($options)) { $options = array(); } $data = EhrlichAndreas_Util_Dsn::parseUri($dsn); $this->driver_name = $data[0]; $driver_dsn = EhrlichAndreas_Util_Dsn::parseDsn($dsn); if ($this->driver_name == 'uri') { $driver_dsn = $this->_get_uri_dsn(key($driver_dsn)); } $this->_init_driver($driver_dsn, $username, $passwd, $options); }
/** * @param \Zend\Db\Adapter\Adapter $adapter */ public function __construct($adapter) { $dbConfig = $adapter->getDriver()->getConnection()->getConnectionParameters(); if (isset($dbConfig['dsn'])) { $data = EhrlichAndreas_Util_Dsn::parseDsn($dbConfig['dsn']); foreach ($data as $key => $value) { $dbConfig[$key] = $value; } $data = EhrlichAndreas_Util_Dsn::parseUri($dbConfig['dsn']); if (isset($data[0])) { $dbConfig['adapter'] = ucfirst($data[0]); } if (isset($dbConfig['adapter']) && $dbConfig['driver']) { $dbConfig['adapter'] = $dbConfig['driver'] . '_' . $dbConfig['adapter']; } } $this->adapter = EhrlichAndreas_Db_Db::factory($dbConfig); $this->adapter->setConnection($adapter->getDriver()->getConnection()->getResource()); }
/** * Factory for EhrlichAndreas_Db_Adapter_Abstract classes. * * First argument may be a string containing the base of the adapter class * name, e.g. 'Mysqli' corresponds to class EhrlichAndreas_Db_Adapter_Mysqli. This * name is currently case-insensitive, but is not ideal to rely on this * behavior. * If your class is named 'EhrlichAndreas_Db_Adapter_Pdo_Mysql', where * 'EhrlichAndreas_Db_Adapter' is the * namespace * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly * as it * is defined in the class. This will ensure proper use of the factory API. * * First argument may alternatively be array. * The adapter class base name is read from the 'adapter' property. * The adapter config parameters are read from the 'params' property. * * Second argument is optional and may be an associative array of key-value * pairs. This is used as the argument to the adapter constructor. * * If the first argument is of type Iterator, it is assumed to contain * all parameters, and the second argument is ignored. * * @param mixed $adapter * String name of base adapter class, or array. * @param mixed $config * OPTIONAL; an array or array with adapter * parameters. * @return EhrlichAndreas_Db_Adapter_Abstract * @throws EhrlichAndreas_Db_Exception */ public static function factory($adapter, $config = array()) { if (is_object($config) && method_exists($config, 'toArray')) { $config = $config->toArray(); } if (is_object($adapter) && method_exists($adapter, 'toArray')) { $adapter = $adapter->toArray(); } /* * Convert array argument to plain string adapter name and separate * config object. */ if (is_array($adapter)) { if (isset($adapter['params'])) { $config = $adapter['params']; } if (empty($config)) { $config = $adapter; } if (isset($adapter['adapter'])) { $adapter = (string) $adapter['adapter']; } else { $adapter = null; } } /* * Verify that adapter parameters are in an array. */ if (!is_array($config)) { /** * * @see EhrlichAndreas_Db_Exception */ throw new EhrlichAndreas_Db_Exception('Adapter parameters must be in an array or a Iterator object'); } if (isset($config['dsn'])) { $dsn = EhrlichAndreas_Util_Dsn::parseDsn($config['dsn']); $uri = EhrlichAndreas_Util_Dsn::parseUri($config['dsn']); $driver_name = $uri[0]; $config = $config + $dsn; /* * Verify that an adapter name has been specified. */ if ((!is_string($adapter) || empty($adapter)) && isset($config['driver']) && stripos($config['driver'], '_') === false) { $adapter = $config['driver'] . '_' . $driver_name; } } /* * Verify that an adapter name has been specified. */ if (!is_string($adapter) || empty($adapter)) { /** * * @see EhrlichAndreas_Db_Exception */ throw new EhrlichAndreas_Db_Exception('Adapter name must be specified in a string'); } /* * Form full adapter class name */ $adapterNamespace = 'EhrlichAndreas_Db_Adapter'; if (isset($config['adapterNamespace'])) { if ($config['adapterNamespace'] != '') { $adapterNamespace = $config['adapterNamespace']; } unset($config['adapterNamespace']); } $adapterName = $adapterNamespace . '_'; $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter)))); /* * Load the adapter class. This throws an exception if the specified * class cannot be loaded. */ if (!class_exists($adapterName)) { throw new EhrlichAndreas_Db_Exception("Adapter class '{$adapterName}' does not exist"); } $notInstanceOf = true; $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'EhrlichAndreas_Db_Adapter_Abstract'); $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'Zend_Db_Adapter_Abstract'); $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'MiniPhp_Db_Adapter_Abstract'); /* * Verify that the object created is a descendent of the abstract * adapter type. */ if ($notInstanceOf) { /** * * @see EhrlichAndreas_Db_Exception */ throw new EhrlichAndreas_Db_Exception("Adapter class '{$adapterName}' does not extend EhrlichAndreas_Db_Adapter_Abstract"); } /* * Create an instance of the adapter class. Pass the config to the * adapter class constructor. */ $adapter = str_replace(' ', '_', ucwords(str_replace('_', ' ', $adapter))); $config['adapter'] = $adapter; $dbAdapter = new $adapterName($config); return $dbAdapter; }
protected function _get_uri_dsn($driver_dsn) { $uri_data = EhrlichAndreas_Util_Dsn::parseUri($driver_dsn); switch ($uri_data[0]) { case 'file': $dsn = file_get_contents($uri_data[1]); if (false === $dsn) { throw new EhrlichAndreas_Pdo_Exception('invalid data source name'); } return EhrlichAndreas_Util_Dsn::parseDsn($dsn); break; default: throw new EhrlichAndreas_Pdo_Exception('invalid data source name'); break; } }
/** * * @param mixed $options * @return array */ protected function _getCmsConfigFromAdapter($options = array()) { if (!is_array($options)) { if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'Zend\\Db\\Adapter\\Adapter')) { $options = new EhrlichAndreas_Db_ZF2Bridge_Adapter($options); } if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'Doctrine\\Bundle\\DoctrineBundle\\Registry')) { $options = new EhrlichAndreas_Db_DoctrineBridge_Adapter($options); } if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'EhrlichAndreas_Db_Adapter_Abstract') || EhrlichAndreas_Util_Object::isInstanceOf($options, 'Zend_Db_Adapter_Abstract')) { $adapter = $options; $dbConfig = $options->getConfig(); if (isset($dbConfig['dsn'])) { $data = EhrlichAndreas_Util_Dsn::parseDsn($dbConfig['dsn']); foreach ($data as $key => $value) { $dbConfig[$key] = $value; } $data = EhrlichAndreas_Util_Dsn::parseUri($dbConfig['dsn']); if (isset($data[0])) { $dbConfig['adapter'] = ucfirst($data[0]); } if (isset($dbConfig['adapter']) && $dbConfig['driver']) { $dbConfig['adapter'] = $dbConfig['driver'] . '_' . $dbConfig['adapter']; } } $options = array('db' => $adapter, 'dbconfig' => $dbConfig, 'params' => $dbConfig); if (isset($dbConfig['adapter'])) { $options['adapter'] = $dbConfig['adapter']; } if (isset($dbConfig['install'])) { $options['install'] = $dbConfig['install']; } } } return $options; }