php-vn-lib/lib/app.php

184 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Vn\Lib;
require_once __DIR__.'/util.php';
use Vn\Db\Connection;
use Vn\Lib\Locale;
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');
/**
* Base class for applications.
**/
class App
{
protected $name;
protected $methodDir;
private $conf = NULL;
private $sysConn = NULL;
/**
* Creates a new application object.
*
* @param name string The application name
**/
function __construct ($name, $methodDir = NULL)
{
$this->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.
**/
2016-08-26 12:44:08 +00:00
function loadMethod ($methodUri = NULL, $checkClass = NULL, $baseDir = NULL)
{
// 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';
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");
2016-08-26 12:44:08 +00:00
if (!is_subclass_of ($className, $checkClass))
throw new \Exception ("Class '$className' is not a '$checkClass' child");
Locale::addPath ("$baseDir/$methodName");
return new $className ($this);
}
}