php-vn-lib/vn/lib/app.php

182 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Vn\Lib;
require_once ('vn/lib/method.php');
require_once ('vn/lib/locale.php');
require_once ('vn/db/db.php');
use Vn\Db\Conn;
use Vn\Lib\Locale;
if (!defined ('_DEBUG_MODE'))
define ('_DEBUG_MODE', FALSE);
if (!defined ('_CONFIG_DIR'))
define ('_CONFIG_DIR', '/etc');
if (!defined ('_LOG_DIR'))
define ('_LOG_DIR', '/var/log');
/**
* Exception thrown when user credentials could not be fetched.
**/
class SessionExpiredException extends \Exception {}
/**
* Exception thrown when user credentials are invalid.
**/
class BadLoginException extends \Exception {}
/**
* Base class for applications.
**/
class App
{
protected $conn = NULL;
protected $name;
private $conf = NULL;
private $sysConn = NULL;
/**
* Creates e new application object.
*
* @param name string The application name
**/
function __construct ($name)
{
$this->name = $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');
$configFile = $this->getConfigFile ();
$this->conf = include ($configFile);
register_shutdown_function ([$this, 'deinit']);
}
/**
* 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;
}
}
/**
* Obtains the application version number. It is based on de last
* modification date of the main script.
**/
function getVersion ()
{
return (int) filectime (__FILE__);
}
/**
* 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 Conn ();
$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 () {}
/**
* Authenticates the user. Should be reimplemented by child classes.
**/
function login () {}
/**
* Deauthenticates the user. Should be reimplemented by child classes.
**/
function logout () {}
/**
* Runs a method.
**/
function runMethod ($baseDir, $methodName)
{
if (empty ($methodName))
throw new \Exception ('Method not defined');
if (!isHyphen ($methodName))
throw new \Exception ('Method contains invalid characters');
$className = hyphenToCamelCase ($methodName, TRUE);
$methodFile = "$baseDir/$methodName.php";
include_once ($methodFile);
if (!class_exists ($className))
throw new \Exception ('Method class not exists');
if (!is_subclass_of ($className, __NAMESPACE__ .'\Method'))
throw new \Exception ('Class is not a Method class child');
Locale::addPath ("$baseDir/$methodName");
return new $className ($this);
}
}
?>