/** * 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])); }
/** * Gets the unique class instance * * This method is used to get the unique instance of the class * (singleton). If no instance is available, it will create it. * * @return Woops_Core_ClassManager The unique instance of the class * @see __construct */ public static function getInstance() { // Checks if the unique instance already exists if (!is_object(self::$_instance)) { // Creates the unique instance self::$_instance = new self(); // Gets the instance of the WOOPS environment self::$_instance->_env = Woops_Core_Env_Getter::getInstance(); // Gets the instance of the WOOPS module manager self::$_instance->_modManager = Woops_Core_Module_Manager::getInstance(); // Checks if we must use AOP classes self::$_instance->_enableAop = Woops_Core_Config_Getter::getInstance()->getVar('aop', 'enable'); // If AOP is enabled, the class cache must also be enabled if (self::$_instance->_enableAop) { // Enables the class cache self::$_instance->_classCache = true; } else { // Checks if we must use a class cache self::$_instance->_classCache = Woops_Core_Config_Getter::getInstance()->getVar('classCache', 'enable'); } // Checks if we must use cached classes if (self::$_instance->_classCache) { // Gets the class cache directory self::$_instance->_cacheDirectory = self::$_instance->_env->getPath('cache/classes/'); // Checks if the cache directory exist, and is writeable if (!self::$_instance->_cacheDirectory || !is_dir(self::$_instance->_cacheDirectory) || !is_writeable(self::$_instance->_cacheDirectory)) { // Disables the AOP and the class cache // Maybe this should generate a fatal error, but in that // case, and if we are installing WOOPS, this could // generate a bad first impression... self::$_instance->_classCache = false; self::$_instance->_enableAop = false; define('WOOPS_CLASS_CACHE_MODE_OFF', true); } } // Adds the WOOPS version to the HTTP headers header('X-WOOPS-VERSION: ' . Woops_Core_Informations::WOOPS_VERSION . '-' . Woops_Core_Informations::WOOPS_VERSION_SUFFIX); } // Returns the unique instance return self::$_instance; }
/** * 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 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); }