/** * 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 the DSN string that is to be passed to the constructor when building the PDO object. * * Method returns a PDO DSN string for MySql connections. * * @param \Configuration $oConfig * @return string * @throws \MiniOrm\DbalException */ public function getConnection(Configuration $oConfig) { $aConfig = $oConfig->get(); $sConnect = ''; //Check that the driver is present and is the correct one. if (isset($aConfig['connection']) && isset($aConfig['connection']['driver']) && self::DRIVER_NAME == $aConfig['connection']['driver']) { //PDO will throw an exception if mysql server hostname is empty. $sConnect .= self::PREFIX; $sConnect .= 'host='; $sConnect .= isset($aConfig['connection']['hostname']) ? $aConfig['connection']['hostname'] : ''; $sConnect .= ';dbname='; $sConnect .= isset($aConfig['connection']['database']) ? $aConfig['connection']['database'] : ''; } else { throw DbalException::configurationInvalid('Cannot find the connection configurations or driver is invalid fir the MySql driver.'); } return $sConnect; }
/** * Returns the connection string that is to be passed when building the PDO object. * * Method returns a PDO connection string for SQLite3. * * @param \Configuration $oConfig * @return string * @throws \MiniOrm\DbalException */ public function getConnection(Configuration $oConfig) { $aConfig = $oConfig->get(); $sConnect = ''; //Check that the driver is present and is the correct one. if (isset($aConfig['connection']) && isset($aConfig['connection']['driver']) && self::DRIVER_NAME == $aConfig['connection']['driver']) { //Sqlite DSN has a variety of prefixes. //If one is not set in confuigurations then assign the default held in the PREFIX constant. $sPrefix = null; if (isset($aConfig['connection']['prefix']) && !empty($aConfig['connection']['prefix'])) { $sPrefix = $aConfig['connection']['prefix']; } else { $sPrefix = self::PREFIX; } $sConnect .= $sPrefix; $sConnect .= isset($aConfig['connection']['database']) ? $aConfig['connection']['database'] : ''; } else { throw DbalException::configurationInvalid('Cannot find the connection configurations or driver is invalid for the Sqlite driver.'); } return $sConnect; }