/** * Write an entry to the ORM log. * * Pass a method or class name as the second argument. * * @param string $sMessage * @param string $sClass * @throws \Exception */ public function log($sMessage, $sClass) { if ($this->oConfig instanceof Configuration) { $aConfig = $this->oConfig->get(); if (isset($aConfig[Configuration::ORM_KEY]['logfile']) && \is_file($aConfig[Configuration::ORM_KEY]['logfile']) && \is_writable($aConfig[Configuration::ORM_KEY]['logfile'])) { $sLog = \is_string($sClass) ? $sClass . ' ' . $sMessage : $sMessage; //Write to the log file if it exists. Append entries and apply a lock to the file during write operation. \file_put_contents($aConfig[Configuration::ORM_KEY]['logfile'], $sLog, FILE_APPEND | LOCK_EX); } else { throw new \Exception('Log file cannot be found or is not writable'); } } }
/** * Set the error mode for PDO. * * If no error mode is set in configurations then PDO will be set to error mode exception. * * @param \PDO $oPDO */ private function setErrorMode(\PDO $oPDO) { $aConfig = $this->oConfig->get(); if (isset($aConfig[Configuration::PDO_KEY]) && isset($aConfig[Configuration::PDO_KEY]['errormode'])) { switch ($aConfig[Configuration::PDO_KEY]['errormode']) { case self::ERRMODE_SILENT: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); break; case self::ERRMODE_WARNING: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); break; case self::ERRMODE_EXCEPTION: $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); break; } } else { //Default setting if error mode is not set in configurations. $oPDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } }
/** * 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 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; }