Clase App ahora soporta URI para cargar metodos
This commit is contained in:
parent
a90c09c755
commit
fa2f629c73
|
@ -1,4 +1,4 @@
|
|||
php-vn-lib (1.210-deb8) stable; urgency=low
|
||||
php-vn-lib (1.301-deb8) stable; urgency=low
|
||||
|
||||
* Initial Release.
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
vn/* usr/share/php/vn
|
||||
vn usr/share/php
|
||||
|
|
|
@ -209,7 +209,7 @@ class Conn
|
|||
**/
|
||||
function getRowFromFile ($file, $params = NULL)
|
||||
{
|
||||
$result = $this->queryFromFile ($query, $params);
|
||||
$result = $this->queryFromFile ($file, $params);
|
||||
return $this->getRowFromResult ($result);
|
||||
}
|
||||
|
||||
|
@ -223,7 +223,7 @@ class Conn
|
|||
**/
|
||||
function getValueFromFile ($file, $params = NULL)
|
||||
{
|
||||
$result = $this->queryFromFile ($query, $params);
|
||||
$result = $this->queryFromFile ($file, $params);
|
||||
return $this->getValueFromResult ($result);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
<?php
|
||||
|
||||
require_once ('vn/lib/lib.php');
|
||||
require_once ('vn/db/conn.php');
|
||||
|
||||
?>
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once ('vn/lib/method.php');
|
||||
require_once ('vn/lib/locale.php');
|
||||
require_once ('vn/db/db.php');
|
||||
require_once (__DIR__.'/method.php');
|
||||
require_once (__DIR__.'/exception.php');
|
||||
require_once (__DIR__.'/user-exception.php');
|
||||
require_once (__DIR__.'/locale.php');
|
||||
require_once ('vn/db/conn.php');
|
||||
|
||||
use Vn\Db\Conn;
|
||||
use Vn\Lib\Locale;
|
||||
|
@ -15,35 +17,38 @@ 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 {}
|
||||
if (!defined ('_DATA_DIR'))
|
||||
define ('_DATA_DIR', '/var/lib');
|
||||
|
||||
/**
|
||||
* Base class for applications.
|
||||
**/
|
||||
class App
|
||||
{
|
||||
protected $conn = NULL;
|
||||
protected $name;
|
||||
protected $methodDir;
|
||||
private $conf = NULL;
|
||||
private $sysConn = NULL;
|
||||
|
||||
/**
|
||||
* Creates e new application object.
|
||||
* Creates a new application object.
|
||||
*
|
||||
* @param name string The application name
|
||||
**/
|
||||
function __construct ($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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,17 +58,17 @@ class App
|
|||
function init ()
|
||||
{
|
||||
ini_set ('log_errors', TRUE);
|
||||
// ini_set ('error_log', _LOG_DIR .'/'. $this->name .'.log');
|
||||
ini_set ('error_log', _LOG_DIR .'/'. $this->name .'.log');
|
||||
|
||||
register_shutdown_function ([$this, 'deinit']);
|
||||
|
||||
$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 is called automatically at the end of the script.
|
||||
**/
|
||||
function deinit ()
|
||||
{
|
||||
|
@ -73,15 +78,6 @@ class App
|
|||
$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.
|
||||
|
@ -142,40 +138,47 @@ class App
|
|||
* 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)
|
||||
function loadMethod ($methodUri = NULL, $baseDir = NULL)
|
||||
{
|
||||
if (empty ($methodName))
|
||||
throw new \Exception ('Method not defined');
|
||||
if (!isHyphen ($methodName))
|
||||
// XXX: Partially implemented
|
||||
if (!$methodUri)
|
||||
$methodUri = basename ($_SERVER['SCRIPT_FILENAME'], '.php');
|
||||
if (!$baseDir)
|
||||
$baseDir = $this->methodDir;
|
||||
|
||||
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);
|
||||
$methodFile = "$baseDir/$methodName.php";
|
||||
include_once ($methodFile);
|
||||
|
||||
if (!class_exists ($className))
|
||||
throw new \Exception ('Method class not exists');
|
||||
throw new \Exception ("Class '$className' not exists");
|
||||
if (!is_subclass_of ($className, __NAMESPACE__ .'\Method'))
|
||||
throw new \Exception ('Class is not a Method class child');
|
||||
throw new \Exception ("Class '$className' is not a 'Method' child");
|
||||
|
||||
Locale::addPath ("$baseDir/$methodName");
|
||||
return new $className ($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once ('vn/lib/app.php');
|
||||
require_once (__DIR__.'/app.php');
|
||||
|
||||
/**
|
||||
* Implements command line applications.
|
||||
|
@ -19,16 +19,11 @@ class CliApp extends App
|
|||
try {
|
||||
$options = getopt ('m:');
|
||||
|
||||
if (!empty ($options['m']))
|
||||
if (empty ($options['m']))
|
||||
$this->usage ();
|
||||
|
||||
switch ($options['m'])
|
||||
{
|
||||
default:
|
||||
$this->usage ();
|
||||
}
|
||||
|
||||
echo s('Action completed.')."\n";
|
||||
$method = $this->loadMethod ($options['m']);
|
||||
$method->run ();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
|
@ -37,19 +32,16 @@ class CliApp extends App
|
|||
}
|
||||
}
|
||||
|
||||
function login ()
|
||||
function json ($json)
|
||||
{
|
||||
$this->conn = $this->getSysConn ();
|
||||
}
|
||||
|
||||
function logout ()
|
||||
{
|
||||
$this->conn = NULL;
|
||||
print_r ($json);
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
function usage ()
|
||||
{
|
||||
echo "app.php -m method\n";
|
||||
global $argv;
|
||||
echo "{$argv[0]} -m method\n";
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Vn\Lib;
|
||||
|
||||
/**
|
||||
* Class used to store information about errors, warnings and info messages.
|
||||
*
|
||||
* @property string $message The message string
|
||||
* @property string $code The code of message
|
||||
**/
|
||||
class Exception extends \Exception
|
||||
{
|
||||
protected $code;
|
||||
|
||||
function __construct ($message = '', $code = NULL, $previous = NULL)
|
||||
{
|
||||
parent::__construct ($message, 0, $previous);
|
||||
$this->code = $code;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +0,0 @@
|
|||
<?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');
|
||||
|
||||
?>
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once ('vn/lib/app.php');
|
||||
require_once (__DIR__.'/app.php');
|
||||
|
||||
/**
|
||||
* Base class for rest methods.
|
||||
|
@ -57,7 +57,7 @@ abstract class Method
|
|||
{
|
||||
return $this->app->getSysConn ();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a set of keys are defined and not empty inside the passed
|
||||
* associative array.
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Vn\Lib;
|
||||
|
||||
require_once (__DIR__.'/exception.php');
|
||||
|
||||
/**
|
||||
* Class used to store information about errors, warnings and info messages,
|
||||
* that end users understood and are allowed to see.
|
||||
*
|
||||
* @property string $domain The domain name
|
||||
* @property string $code The code of message
|
||||
* @property string $message The message string
|
||||
**/
|
||||
class UserException extends Exception {}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue