/** * Tries to establish a database connection * * @return mixed False is the connection is OK, otherwise the error message */ protected function _checkDatabaseConnection() { // Gets the incoming data $database = $this->_getModuleVar('database'); // Gets the class name of the database engine (we won't use the getEngine() method, as it will tries to connect the engine with an incomplete configuration) $engineClass = Woops_Database_Layer::getInstance()->getEngineClass(); // Gets the engine instance $engine = Woops_Core_Class_Manager::getInstance()->getSingleton($engineClass); // We don't want any error here, we are just testing the database settings try { // Database settings $driver = $database['driver']; $host = $database['host']; $port = isset($database['port']) && $database['port'] ? $database['port'] : false; $user = isset($database['user']) && $database['user'] ? $database['user'] : false; $password = isset($database['password']) && $database['password'] ? $database['password'] : false; $name = isset($database['database']) && $database['database'] ? $database['database'] : false; $prefix = isset($database['tablePrefix']) && $database['tablePrefix'] ? $database['tablePrefix'] : false; // Loads and connect the engine - If this fail, we'll should get an exception $engine->load($driver, $host, $port, $name, $prefix); $engine->connect($user, $password); } catch (Exception $e) { // Returns the error message return $e->getMessage(); } // Database connection is OK return false; }
/** * Registers a class as a database engine * * Note that the database engine class must implement the * Woops_Database_Engine_Interface interface. * * @param string The name of the database engine * @param string The class of the database engine * @return void * @throws Woops_Database_Layer_Exception If an engine with the same name is already registered * @throws Woops_Database_Layer_Exception If the engine class does not exists * @throws Woops_Database_Layer_Exception If the engine class does not implements the Woops_Database_Engine_Interface interface * @see Woops_Core_Singleton_Interface::load * @see Woops_Core_Singleton_Interface::connect */ public function registerDatabaseEngine($name, $class) { // Checks for an engine with the same name if (isset($this->_engines[$name])) { // Engine already registered throw new Woops_Database_Layer_Exception('The engine \'' . $name . '\' is already registered', Woops_Database_Layer_Exception::EXCEPTION_ENGINE_EXISTS); } // Checks for the engine class if (!class_exists($class)) { // The engine class does not exist throw new Woops_Database_Layer_Exception('Cannot register unexisting class \'' . $class . '\' as a database engine', Woops_Database_Layer_Exception::EXCEPTION_NO_ENGINE); } // Gets the interfaces $interfaces = class_implements($class); // Checks if the engine class implements the database engine interface if (!isset($interfaces['Woops_Database_Engine_Interface'])) { // Invalid class throw new Woops_Database_Layer_Exception('Cannot register class \'' . $class . '\' as a database engine, since it does not implements the \'Woops_Database_Engine_Interface\' interface', Woops_Database_Layer_Exception::EXCEPTION_INVALID_ENGINE_CLASS); } // Gets and stores the instance of the database engine class $this->_engines[$name] = Woops_Core_Class_Manager::getInstance()->getSingleton($class); $this->_engineNames[$name] = true; // Gets the available drivers from the engine $this->_drivers[$name] = $this->_engines[$name]->getAvailableDrivers(); // Dispatch the event to the listeners $this->dispatchEventObject(new Woops_Database_Layer_Event(Woops_Database_Layer_Event::EVENT_ENGINE_REGISTER, $this->_engines[$name])); }
/** * Registers a log writer class * * @param string The name of the log writer class * @param int The log types supported by the log writer class (some LOG_TYPE_XXX constants) * @return void * throws Woops_Log_Writer_Exception If the log writer class if already registered * throws Woops_Log_Writer_Exception If the log writer class does not exists * throws Woops_Log_Writer_Exception If the log writer class does not implements the Woops_Log_Writer_Interface interface */ public function registerLogWriter($class, $types = 0xff) { // Type correction $types = $types & self::LOG_TYPE_ALL; // Checks if the log writer is already registered if (isset($this->_loggers[$class])) { // Class is already registered throw new Woops_Log_Writer_Exception('The log writer \'' . $class . '\' is already registered', Woops_Log_Writer_Exception::EXCEPTION_WRITER_EXISTS); } // Checks if the class exists if (!class_exists($class)) { // The class does not exists throw new Woops_Log_Writer_Exception('Cannot register unexisting class \'' . $class . '\' as a log writer', Woops_Log_Writer_Exception::EXCEPTION_NO_WRITER); } // Gets the interfaces of the log writer class $interfaces = class_implements($class); // Checks if the log writer class implements the log writer interface if (!isset($interfaces['Woops_Log_Writer_Interface'])) { // Error - The log writer class must extends the log writer interface throw new Woops_Log_Writer_Exception('Cannot register class \'' . $class . '\' as a log writer, since it does not implements the \'Woops_Log_Writer_Interface\' interface', Woops_Log_Writer_Exception::EXCEPTION_INVALID_WRITER_CLASS); } // Dispatch the event to the listeners $this->dispatchEvent(Woops_Log_Writer_Event::EVENT_LOG_WRITER_REGISTER); // Registers the log writer class $this->_loggers[$class] = array(Woops_Core_Class_Manager::getInstance()->getSingleton($class), $types); }