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;
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (!defined('_ENABLE_DEBUG'))
|
|
|
|
define('_ENABLE_DEBUG', FALSE);
|
|
|
|
if (!defined('_DEV_MODE'))
|
|
|
|
define('_DEV_MODE', FALSE);
|
|
|
|
if (!defined('_CONFIG_DIR'))
|
|
|
|
define('_CONFIG_DIR', '/etc');
|
|
|
|
if (!defined('_LOG_DIR'))
|
|
|
|
define('_LOG_DIR', '/var/log');
|
|
|
|
if (!defined('_DATA_DIR'))
|
|
|
|
define('_DATA_DIR', '/var/lib');
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for applications.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
class App {
|
2016-05-09 07:40:46 +00:00
|
|
|
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
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +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
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getName() {
|
2016-08-22 10:42:31 +00:00
|
|
|
return $this->name;
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 13:27:25 +00:00
|
|
|
/**
|
|
|
|
* Returns the configuration object.
|
|
|
|
*
|
|
|
|
* @return The config object
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getConf() {
|
2017-05-30 13:27:25 +00:00
|
|
|
return $this->conf;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:40:46 +00:00
|
|
|
/**
|
|
|
|
* Initializes Application. Should be the first called function in any
|
|
|
|
* application that uses this class.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function init() {
|
|
|
|
ini_set('log_errors', TRUE);
|
|
|
|
//ini_set('error_log', _LOG_DIR .'/'. $this->name .'.log');
|
2016-08-22 10:42:31 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
register_shutdown_function([$this, 'deinit']);
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
$configFile = $this->getConfigFile();
|
|
|
|
$this->conf = include($configFile);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function deinit() {
|
|
|
|
if ($this->sysConn) {
|
|
|
|
$this->sysConn->close();
|
2016-05-09 07:40:46 +00:00
|
|
|
$this->sysConn = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the configuration file name.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getConfigFile() {
|
2016-05-09 07:40:46 +00:00
|
|
|
$configDir = _CONFIG_DIR .'/'. $this->name;
|
|
|
|
$customFile = "$configDir/config.my.php";
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (file_exists('config.php'))
|
2018-05-08 15:09:44 +00:00
|
|
|
return 'config.php';
|
2018-05-23 09:08:11 +00:00
|
|
|
else if (file_exists($customFile))
|
2016-05-09 07:40:46 +00:00
|
|
|
return $customFile;
|
|
|
|
else
|
|
|
|
return "$configDir/config.php";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new connection object using the configuration parameters and
|
|
|
|
* the passed user and password.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function createConnection($user, $password, $persistent = FALSE) {
|
2016-05-09 07:40:46 +00:00
|
|
|
$dbConf = $this->conf['db'];
|
|
|
|
$host = $dbConf['host'];
|
|
|
|
|
|
|
|
if ($persistent)
|
|
|
|
$host = 'p:'.$host;
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
$conn = new Connection();
|
|
|
|
$conn->open(
|
2016-05-09 07:40:46 +00:00
|
|
|
$host
|
|
|
|
,$user
|
|
|
|
,$password
|
|
|
|
,$dbConf['schema']
|
|
|
|
,$dbConf['port']
|
|
|
|
);
|
2018-05-23 09:08:11 +00:00
|
|
|
$conn->query('SET @lang = #', [Locale::get()]);
|
2016-05-09 07:40:46 +00:00
|
|
|
return $conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the system database connection.
|
|
|
|
*
|
|
|
|
* @return Vn\Db\Conn The connection
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function getSysConn() {
|
|
|
|
if (!$this->sysConn) {
|
2016-05-09 07:40:46 +00:00
|
|
|
$dbConf = $this->conf['db'];
|
2018-05-23 09:08:11 +00:00
|
|
|
$this->sysConn = $this->createConnection(
|
|
|
|
$dbConf['user'], base64_decode($dbConf['pass']), TRUE);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sysConn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the application. Should be implemented by child classes.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function run() {}
|
2016-05-09 07:40:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a method.
|
2018-05-08 15:09:44 +00:00
|
|
|
*/
|
2018-05-23 09:08:11 +00:00
|
|
|
function loadMethod($methodUri = NULL, $checkClass = NULL, $baseDir = NULL) {
|
2016-08-22 10:42:31 +00:00
|
|
|
// XXX: Partially implemented
|
|
|
|
if (!$methodUri)
|
2018-05-23 09:08:11 +00:00
|
|
|
$methodUri = basename($_SERVER['SCRIPT_FILENAME'], '.php');
|
2016-08-22 10:42:31 +00:00
|
|
|
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
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (!preg_match('/^[\/\w\-]+$/', $methodUri))
|
|
|
|
throw new \Exception('Method contains invalid characters');
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
$split = explode('/', $methodUri);
|
|
|
|
$methodName = array_pop($split);
|
|
|
|
$methodPath = implode('/', $split);
|
2016-08-22 10:42:31 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (empty($methodName))
|
|
|
|
throw new \Exception('Invalid method name');
|
2016-08-22 10:42:31 +00:00
|
|
|
|
|
|
|
$methodFile = '';
|
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
if (!empty($baseDir))
|
2016-08-22 10:42:31 +00:00
|
|
|
$methodFile .= "$baseDir/";
|
2018-05-23 09:08:11 +00:00
|
|
|
if (!empty($methodPath))
|
2016-08-22 10:42:31 +00:00
|
|
|
$methodFile .= "$methodPath/";
|
|
|
|
|
|
|
|
$methodFile .= "$methodName.php";
|
2018-05-23 09:08:11 +00:00
|
|
|
$className = hyphenToCamelCase($methodName, TRUE);
|
|
|
|
include_once($methodFile);
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
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");
|
2016-05-09 07:40:46 +00:00
|
|
|
|
2018-05-23 09:08:11 +00:00
|
|
|
Locale::addPath("$baseDir/$methodName");
|
|
|
|
return new $className($this);
|
2016-05-09 07:40:46 +00:00
|
|
|
}
|
|
|
|
}
|