Proyecto reestructurado, nuevas clases añadidas
This commit is contained in:
parent
c73e018c0a
commit
a90c09c755
|
@ -3,7 +3,7 @@
|
|||
set_include_path
|
||||
(
|
||||
get_include_path ()
|
||||
.PATH_SEPARATOR.__DIR__.'/lib'
|
||||
.PATH_SEPARATOR.__DIR__
|
||||
);
|
||||
|
||||
?>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
php-vn-lib (1.208-deb8) stable; urgency=low
|
||||
php-vn-lib (1.210-deb8) stable; urgency=low
|
||||
|
||||
* Initial Release.
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
lib/* usr/share/php
|
||||
vn/* usr/share/php/vn
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
|
||||
require_once ('vn/lib/type.php');
|
||||
require_once ('vn/lib/locale.php');
|
||||
require_once ('vn/lib/log.php');
|
||||
|
||||
?>
|
|
@ -24,6 +24,7 @@ class Conn
|
|||
{
|
||||
$conn = $this->conn = mysqli_init ();
|
||||
$conn->options (MYSQLI_READ_DEFAULT_FILE, __DIR__.'/my.cnf');
|
||||
$conn->options (MYSQLI_OPT_LOCAL_INFILE, TRUE);
|
||||
@$conn->real_connect ($host, $user, $pass, $name, $port);
|
||||
|
||||
if (mysqli_connect_errno ())
|
||||
|
@ -48,81 +49,16 @@ class Conn
|
|||
|
||||
$this->isOpen = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the connection is open.
|
||||
*
|
||||
* @return boolean %TRUE if connection is open, %FALSE otherwise
|
||||
**/
|
||||
function isOpen ()
|
||||
{
|
||||
return $this->isOpen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query and gets it's first row.
|
||||
*
|
||||
* @param string $query The SQL query
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed[] An associative array with the first row, %NULL if error
|
||||
**/
|
||||
function getRow ($query, $params = NULL)
|
||||
{
|
||||
$result = $this->query ($query, $params);
|
||||
|
||||
if ($result)
|
||||
{
|
||||
$row = $result->fetch_assoc ();
|
||||
$result->free ();
|
||||
return $row;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query and gets the first value of the first row.
|
||||
* Changes the default schema for the current connection.
|
||||
*
|
||||
* @param string $query The SQL query
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
* @param string $schema The schema name
|
||||
* @return boolean %TRUE if success, %FALSE otherwise
|
||||
**/
|
||||
function getValue ($query, $params = NULL)
|
||||
function selectDb ($dbName)
|
||||
{
|
||||
$value = NULL;
|
||||
$result = $this->query ($query, $params);
|
||||
|
||||
if ($result)
|
||||
{
|
||||
$row = $result->fetch_row ();
|
||||
|
||||
if ($row && count ($row) > 0)
|
||||
$value = $row[0];
|
||||
|
||||
$result->free ();
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query stored in the specified file and gets the result.
|
||||
*
|
||||
* @param string $query The SQL query
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
**/
|
||||
function queryFromFile ($file, $params = NULL)
|
||||
{
|
||||
$query = file_get_contents ($file);
|
||||
|
||||
if ($query)
|
||||
return $this->query ($query, $params);
|
||||
|
||||
return NULL;
|
||||
return $this->conn->select_db ($dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,6 +81,151 @@ class Conn
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the connection is open.
|
||||
*
|
||||
* @return boolean %TRUE if connection is open, %FALSE otherwise
|
||||
**/
|
||||
function isOpen ()
|
||||
{
|
||||
return $this->isOpen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first row value of the first column from a result.
|
||||
*
|
||||
* @param resource $result The database result
|
||||
*
|
||||
* @return mixed[] An associative array with the first row, %NULL if error
|
||||
**/
|
||||
function getRowFromResult ($result)
|
||||
{
|
||||
if ($result)
|
||||
{
|
||||
$row = $result->fetch_assoc ();
|
||||
$result->free ();
|
||||
return $row;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first row value of the first column from a result.
|
||||
*
|
||||
* @param resource $result The database result
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
**/
|
||||
function getValueFromResult ($result)
|
||||
{
|
||||
$value = NULL;
|
||||
|
||||
if ($result)
|
||||
{
|
||||
$row = $result->fetch_row ();
|
||||
|
||||
if ($row && count ($row) > 0)
|
||||
$value = $row[0];
|
||||
|
||||
$result->free ();
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query and gets it's first row.
|
||||
*
|
||||
* @param string $query The SQL query
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed[] An associative array with the first row, %NULL if error
|
||||
**/
|
||||
function getRow ($query, $params = NULL)
|
||||
{
|
||||
$result = $this->query ($query, $params);
|
||||
return $this->getRowFromResult ($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query and gets the first value of the first row.
|
||||
*
|
||||
* @param string $query The SQL query
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
**/
|
||||
function getValue ($query, $params = NULL)
|
||||
{
|
||||
$result = $this->query ($query, $params);
|
||||
return $this->getValueFromResult ($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a query from a file and renders it.
|
||||
*
|
||||
* @param string $file The file path
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The query string
|
||||
**/
|
||||
function loadFromFile ($file, $params = NULL)
|
||||
{
|
||||
$query = file_get_contents ($file .'.sql');
|
||||
|
||||
if ($query === FALSE)
|
||||
throw new Exception (NULL, 'Can not load query from file');
|
||||
|
||||
return $this->render ($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the query stored in the specified file and gets the result.
|
||||
*
|
||||
* @param string $file The file path
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
**/
|
||||
function queryFromFile ($file, $params = NULL)
|
||||
{
|
||||
$query = $this->loadFromFile ($file, $params);
|
||||
|
||||
if ($query)
|
||||
return $this->query ($query);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the file query and gets it's first row.
|
||||
*
|
||||
* @param string $file The file path
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed[] An associative array with the first row, %NULL if error
|
||||
**/
|
||||
function getRowFromFile ($file, $params = NULL)
|
||||
{
|
||||
$result = $this->queryFromFile ($query, $params);
|
||||
return $this->getRowFromResult ($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the file query and gets the first value of the first row.
|
||||
*
|
||||
* @param string $file The file path
|
||||
* @param mixed[] $params The query parameters
|
||||
*
|
||||
* @return mixed The value or %NULL if error
|
||||
**/
|
||||
function getValueFromFile ($file, $params = NULL)
|
||||
{
|
||||
$result = $this->queryFromFile ($query, $params);
|
||||
return $this->getValueFromResult ($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute multiple queries separated by semicolons.
|
||||
|
@ -193,7 +274,9 @@ class Conn
|
|||
|
||||
function nextResult ()
|
||||
{
|
||||
return $this->conn->next_result ();
|
||||
$hasNext = $this->conn->next_result ();
|
||||
$this->checkError ();
|
||||
return $hasNext;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -222,7 +305,7 @@ class Conn
|
|||
* @param string $query The SQL string
|
||||
* @param mixed[] $paramsMap The query parameters
|
||||
*
|
||||
* @return mixed The rendered SQL string
|
||||
* @return string The rendered SQL string
|
||||
**/
|
||||
function render (&$query, &$paramsMap = NULL)
|
||||
{
|
||||
|
@ -252,6 +335,13 @@ class Conn
|
|||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an SQL respresentation from a PHP value.
|
||||
*
|
||||
* @param mixed $value The value
|
||||
*
|
||||
* @return string The SQL value
|
||||
**/
|
||||
function renderValue ($value)
|
||||
{
|
||||
if ($value !== NULL)
|
|
@ -0,0 +1,181 @@
|
|||
<?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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once ('vn/lib/app.php');
|
||||
|
||||
/**
|
||||
* Implements command line applications.
|
||||
**/
|
||||
class CliApp extends App
|
||||
{
|
||||
function run ()
|
||||
{
|
||||
$this->init ();
|
||||
|
||||
if ($lang = substr (getenv ('LANG'), 0, 2))
|
||||
Locale::set ($lang);
|
||||
|
||||
try {
|
||||
$options = getopt ('m:');
|
||||
|
||||
if (!empty ($options['m']))
|
||||
$this->usage ();
|
||||
|
||||
switch ($options['m'])
|
||||
{
|
||||
default:
|
||||
$this->usage ();
|
||||
}
|
||||
|
||||
echo s('Action completed.')."\n";
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
echo $e->getMessage ()."\n";
|
||||
exit (2);
|
||||
}
|
||||
}
|
||||
|
||||
function login ()
|
||||
{
|
||||
$this->conn = $this->getSysConn ();
|
||||
}
|
||||
|
||||
function logout ()
|
||||
{
|
||||
$this->conn = NULL;
|
||||
}
|
||||
|
||||
function usage ()
|
||||
{
|
||||
echo "app.php -m method\n";
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
require_once ('vn/lib/type.php');
|
||||
require_once ('vn/lib/locale.php');
|
||||
require_once ('vn/lib/log.php');
|
||||
require_once ('vn/lib/app.php');
|
||||
require_once ('vn/lib/cli-app.php');
|
||||
require_once ('vn/lib/method.php');
|
||||
require_once ('vn/lib/util.php');
|
||||
|
||||
?>
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once ('vn/lib/app.php');
|
||||
|
||||
/**
|
||||
* Base class for rest methods.
|
||||
**/
|
||||
abstract class Method
|
||||
{
|
||||
protected $app;
|
||||
protected $conn = NULL;
|
||||
|
||||
/**
|
||||
* Initializes the method.
|
||||
*
|
||||
* @param app Lib\App The application
|
||||
**/
|
||||
function __construct ($app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the method. Shoud be defined by child classes.
|
||||
*
|
||||
* @return mixed The result of the method
|
||||
**/
|
||||
abstract function run ();
|
||||
|
||||
/**
|
||||
* Authenticates the user agaisnt database and returns its associated
|
||||
* database connection.
|
||||
*
|
||||
* return Db\Conn The database connection
|
||||
**/
|
||||
function login ()
|
||||
{
|
||||
return $this->app->login ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Logouts the current user.
|
||||
**/
|
||||
function logout ()
|
||||
{
|
||||
$this->app->logout ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the system database connection.
|
||||
*
|
||||
* return Db\Conn The database connection
|
||||
**/
|
||||
function getSysConn ()
|
||||
{
|
||||
return $this->app->getSysConn ();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a set of keys are defined and not empty inside the passed
|
||||
* associative array.
|
||||
*
|
||||
* @param {string} $map The map to check
|
||||
* @param {array} $params The list of keys to check
|
||||
* @return {boolean} %TRUE if all key are defined, %FALSE otherwise
|
||||
**/
|
||||
function checkParams ($map, $params)
|
||||
{
|
||||
if (!isset ($map))
|
||||
return FALSE;
|
||||
|
||||
foreach ($params as $param)
|
||||
if (empty ($map[$param]))
|
||||
{
|
||||
return FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Retrieves an element from an associative array or %NULL if it is not
|
||||
* defined.
|
||||
**/
|
||||
function nullIf ($map, $key)
|
||||
{
|
||||
return isset ($map[$key]) ? $map[$key] : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if passed $path is inside directory $dir.
|
||||
**/
|
||||
function checkFilePath ($file, $path)
|
||||
{
|
||||
$checkPath = stream_resolve_include_path ($path).'/';
|
||||
$filePath = stream_resolve_include_path ($checkPath.$file);
|
||||
|
||||
$len = strlen ($checkPath);
|
||||
return substr ($filePath, 0, strlen ($checkPath)) === $checkPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if passed token is hyphenized.
|
||||
**/
|
||||
function isHyphen ($token)
|
||||
{
|
||||
return preg_match ('/^[\w\-]+$/', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an hyphen string to camelcase.
|
||||
**/
|
||||
function hyphenToCamelCase ($string, $upperFirst = FALSE)
|
||||
{
|
||||
$replaceFunc = function ($matches)
|
||||
{
|
||||
return strtoupper ($matches[0][1]);
|
||||
};
|
||||
|
||||
$result = preg_replace_callback ('/-[a-z]/', $replaceFunc, $string);
|
||||
|
||||
if ($upperFirst)
|
||||
$result = strtoupper ($result{0}) . substr ($result, 1);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue