2016-05-09 07:40:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Lib;
|
|
|
|
|
2016-09-27 06:20:02 +00:00
|
|
|
require_once __DIR__.'/util.php';
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2016-09-27 06:20:02 +00:00
|
|
|
use Vn\Db\Connection;
|
2016-05-09 07:40:46 +00:00
|
|
|
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');
|
2016-08-22 10:42:31 +00:00
|
|
|
if (!defined ('_DATA_DIR'))
|
|
|
|
define ('_DATA_DIR', '/var/lib');
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for applications.
|
|
|
|
**/
|
|
|
|
class App
|
|
|
|
{
|
|
|
|
protected $name;
|
2016-08-22 10:42:31 +00:00
|
|
|
protected $methodDir;
|
2016-05-09 07:40:46 +00:00
|
|
|
private $conf = NULL;
|
|
|
|
private $sysConn = NULL;
|
|
|
|
|
|
|
|
/**
|
2016-08-22 10:42:31 +00:00
|
|
|
* Creates a new application object.
|
2016-05-09 07:40:46 +00:00
|
|
|
*
|
|
|
|
* @param name string The application name
|
|
|
|
**/
|
2016-08-22 10:42:31 +00:00
|
|
|
function __construct ($name, $methodDir = NULL)
|
2016-05-09 07:40:46 +00:00
|
|
|
{
|
|
|
|
$this->name = $name;
|
2016-08-22 10:42:31 +00:00
|
|
|
$this->methodDir = $methodDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the application.
|
|
|
|
*
|
|
|
|
* @return The application name
|
|
|
|
**/
|
|
|
|
function getName ()
|
|
|
|
{
|
|
|
|
return $this->name;
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes Application. Should be the first called function in any
|
|
|
|
* application that uses this class.
|
|
|
|
**/
|
|
|
|
function init ()
|
|
|
|
{
|
|
|
|
ini_set ('log_errors', TRUE);
|
2016-08-22 10:42:31 +00:00
|
|
|
ini_set ('error_log', _LOG_DIR .'/'. $this->name .'.log');
|
|
|
|
|
|
|
|
register_shutdown_function ([$this, 'deinit']);
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
$configFile = $this->getConfigFile ();
|
|
|
|
$this->conf = include ($configFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deinitializes the Application. When init method is called, this
|
2016-08-22 10:42:31 +00:00
|
|
|
* function is called automatically at the end of the script.
|
2016-05-09 07:40:46 +00:00
|
|
|
**/
|
|
|
|
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;
|
|
|
|
|
2016-09-27 06:20:02 +00:00
|
|
|
$conn = new Connection ();
|
2016-05-09 07:40:46 +00:00
|
|
|
$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.
|
|
|
|
**/
|
2016-08-26 12:44:08 +00:00
|
|
|
function loadMethod ($methodUri = NULL, $checkClass = NULL, $baseDir = NULL)
|
2016-05-09 07:40:46 +00:00
|
|
|
{
|
2016-08-22 10:42:31 +00:00
|
|
|
// XXX: Partially implemented
|
|
|
|
if (!$methodUri)
|
|
|
|
$methodUri = basename ($_SERVER['SCRIPT_FILENAME'], '.php');
|
|
|
|
if (!$baseDir)
|
|
|
|
$baseDir = $this->methodDir;
|
2016-08-26 12:44:08 +00:00
|
|
|
if (!$checkClass)
|
|
|
|
$checkClass = __NAMESPACE__ .'\Method';
|
2016-08-22 10:42:31 +00:00
|
|
|
|
|
|
|
if (!preg_match ('/^[\/\w\-]+$/', $methodUri))
|
2016-05-09 07:40:46 +00:00
|
|
|
throw new \Exception ('Method contains invalid characters');
|
|
|
|
|
2016-08-22 10:42:31 +00:00
|
|
|
$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";
|
2016-05-09 07:40:46 +00:00
|
|
|
$className = hyphenToCamelCase ($methodName, TRUE);
|
|
|
|
include_once ($methodFile);
|
|
|
|
|
|
|
|
if (!class_exists ($className))
|
2016-08-22 10:42:31 +00:00
|
|
|
throw new \Exception ("Class '$className' not exists");
|
2016-08-26 12:44:08 +00:00
|
|
|
if (!is_subclass_of ($className, $checkClass))
|
|
|
|
throw new \Exception ("Class '$className' is not a '$checkClass' child");
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
Locale::addPath ("$baseDir/$methodName");
|
|
|
|
return new $className ($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|