/** * Configures and then returns a data access object returned from a Driver. * * @return \PDO * @throws DbalException */ public function getDataObject() { $aConfig = $this->oConfig->get(); if (isset($aConfig[Configuration::CONNECTION_KEY]) && isset($aConfig[Configuration::CONNECTION_KEY]['driver'])) { $oDriver = $this->oDriverFactory->getInstance($aConfig[Configuration::CONNECTION_KEY]['driver']); $oPDO = $oDriver->getDataAccessObject($this->oConfig); //Configure the error reporting level for PDO. $this->setErrorMode($oPDO); return $oPDO; } else { throw DbalException::invalidConfiguration('Cannot find driver in configurations.'); } }
/** * This operation reads a configuration file using the file path method argument. * * Configurations are stored in a class attribue as an array. * * @param string $sPath * @throws \MiniOrm\DbalException */ public function read($sPath) { if (!\is_file($sPath)) { throw DbalException::configurationNotFound($sPath); } if (!\is_readable($sPath)) { throw DbalException::configurationNotReadable($sPath); } $this->aConfigs = (include $sPath); if (!\is_array($this->aConfigs)) { throw DbalException::configurationInvalid($sPath); } }
/** * Returns an instance of the PHP data object using the mysql PDO extension. * * @param Configuration $oConfig * @throws \MiniOrm\DbalException */ public function getDataAccessObject(Configuration $oConfig) { $aConfig = $oConfig->get(); $sConnect = $this->getConnection($oConfig); if (isset($aConfig['connection']['username']) && isset($aConfig['connection']['password'])) { try { return new \PDO($sConnect, $aConfig['connection']['username'], $aConfig['connection']['password']); } catch (\PDOException $oExp) { throw DbalException::pdoException($oExp->getMessage(), $oExp); } } else { throw DbalException::credentialsNotFound(); } }
/** * Returns a concrete instance of a Driver class. * * Each driver class encapsulates creating a connection to a specific database. * * @param string $sDriver * @return \MiniOrm\Driver * @throws \MiniOrm\DbalException * @throws \RuntimeException */ public function getInstance($sDriver) { $sClass = '\\' . __NAMESPACE__ . '\\' . self::PACKAGE_NAME . '\\' . $sDriver; if (\class_exists($sClass)) { $oDriver = new $sClass(); if ($oDriver instanceof Driver) { return $oDriver; } else { //Code error, throwing runtime exceptin. throw new \RuntimeException('Requested driver class does not implement the Driver interface. Class is ' . $sDriver); } } else { //Incorrect class name provided by client code. throw DbalException::driverNotFound($sDriver); } }
/** * Returns an instance of the PHP data object using the sqlite PDO extension. * * @param Configuration $oConfig * @return \PDO * @throws \MiniOrm\DbalException */ public function getDataAccessObject(Configuration $oConfig) { $sConnect = $this->getConnection($oConfig); try { return new \PDO($sConnect); } catch (\PDOException $oExp) { throw DbalException::pdoException($oExp->getMessage(), $oExp); } }