name = $name; $this->methodDir = $methodDir; } /** * Returns the name of the application. * * @return The application name **/ function getName () { return $this->name; } /** * Initializes Application. Should be the first called function in any * application that uses this class. **/ function init () { ini_set ('log_errors', TRUE); //ini_set ('error_log', _LOG_DIR .'/'. $this->name .'.log'); register_shutdown_function ([$this, 'deinit']); $configFile = $this->getConfigFile (); $this->conf = include ($configFile); } /** * Deinitializes the Application. When init method is called, this * function is called automatically at the end of the script. **/ function deinit () { if ($this->sysConn) { $this->sysConn->close (); $this->sysConn = NULL; } } /** * Gets the configuration file name. **/ function getConfigFile () { $configDir = _CONFIG_DIR .'/'. $this->name; $customFile = "$configDir/config.my.php"; if (file_exists ($customFile)) return $customFile; else return "$configDir/config.php"; } /** * Creates a new connection object using the configuration parameters and * the passed user and password. **/ function createConnection ($user, $password, $persistent = FALSE) { $dbConf = $this->conf['db']; $host = $dbConf['host']; if ($persistent) $host = 'p:'.$host; $conn = new Connection (); $conn->open ( $host ,$user ,$password ,$dbConf['schema'] ,$dbConf['port'] ); $conn->query ('SET @lang = #', [Locale::get ()]); return $conn; } /** * Opens the system database connection. * * @return Vn\Db\Conn The connection **/ function getSysConn () { if (!$this->sysConn) { $dbConf = $this->conf['db']; $this->sysConn = $this->createConnection ( $dbConf['user'], base64_decode ($dbConf['pass']), TRUE); } return $this->sysConn; } /** * Starts the application. Should be implemented by child classes. **/ function run () {} /** * Runs a method. **/ function loadMethod ($methodUri = NULL, $checkClass = NULL, $baseDir = NULL) { // XXX: Partially implemented if (!$methodUri) $methodUri = basename ($_SERVER['SCRIPT_FILENAME'], '.php'); if (!$baseDir) $baseDir = $this->methodDir; if (!$checkClass) $checkClass = __NAMESPACE__ .'\Method'; if (!preg_match ('/^[\/\w\-]+$/', $methodUri)) throw new \Exception ('Method contains invalid characters'); $split = explode ('/', $methodUri); $methodName = array_pop ($split); $methodPath = implode ('/', $split); if (empty ($methodName)) throw new \Exception ('Invalid method name'); $methodFile = ''; if (!empty ($baseDir)) $methodFile .= "$baseDir/"; if (!empty ($methodPath)) $methodFile .= "$methodPath/"; $methodFile .= "$methodName.php"; $className = hyphenToCamelCase ($methodName, TRUE); include_once ($methodFile); if (!class_exists ($className)) throw new \Exception ("Class '$className' not exists"); if (!is_subclass_of ($className, $checkClass)) throw new \Exception ("Class '$className' is not a '$checkClass' child"); Locale::addPath ("$baseDir/$methodName"); return new $className ($this); } }