Directorios reesctructurados, integración total con PHP autoload

This commit is contained in:
Juan Ferrer Toribio 2016-09-27 08:20:02 +02:00
parent 496eb58dcb
commit d5912072f2
16 changed files with 134 additions and 97 deletions

View File

@ -1,9 +1,9 @@
<?php <?php
set_include_path set_include_path (__DIR__.PATH_SEPARATOR.get_include_path ());
(
get_include_path () $vnAutoloadMap = [];
.PATH_SEPARATOR.__DIR__ $vnAutoloadMap['vn/lib'] = __DIR__.'/lib';
); $vnAutoloadMap['vn/db'] = __DIR__.'/db';
?> ?>

View File

@ -2,10 +2,9 @@
namespace Vn\Db; namespace Vn\Db;
require_once ('vn/lib/type.php'); use Vn\Lib\Type;
require_once ('vn/db/exception.php');
class Conn class Connection
{ {
private $conn = NULL; private $conn = NULL;
private $isOpen = FALSE; private $isOpen = FALSE;
@ -74,7 +73,7 @@ class Conn
/** /**
* Returns the internal database connection handler. This function shoud be * Returns the internal database connection handler. This function shoud be
* used to set options for specific databases that could not be set using * used to set options for specific databases that could not be set using
* the Db\Conn class methods. * the Connection class methods.
* *
* @return Objetct The connection handler * @return Objetct The connection handler
**/ **/
@ -314,7 +313,7 @@ class Conn
/** /**
* Check if there has been an error in the last query or multiquery, * Check if there has been an error in the last query or multiquery,
* throwing a @Db\Exception exception if any. * throwing a @Exception exception if any.
**/ **/
function checkError () function checkError ()
{ {
@ -378,17 +377,17 @@ class Conn
function renderValue ($value) function renderValue ($value)
{ {
if ($value !== NULL) if ($value !== NULL)
switch (get_type ($value)) switch (Type::get ($value))
{ {
case TYPE_BOOLEAN: case Type::BOOLEAN:
return ($value) ? 'TRUE' : 'FALSE'; return ($value) ? 'TRUE' : 'FALSE';
case TYPE_STRING: case Type::STRING:
return '\'' . $this->conn->escape_string ($value) . '\''; return '\'' . $this->conn->escape_string ($value) . '\'';
case TYPE_DATE: case Type::DATE:
return strftime ('\'%Y-%m-%d\'', $value->getTimestamp ()); return strftime ('\'%Y-%m-%d\'', $value->getTimestamp ());
case TYPE_TIME: case Type::TIME:
return strftime ('\'%T\'', $value->getTimestamp ()); return strftime ('\'%T\'', $value->getTimestamp ());
case TYPE_DATE_TIME: case Type::DATE_TIME:
return strftime ('\'%Y-%m-%d %T\'', $value->getTimestamp ()); return strftime ('\'%Y-%m-%d %T\'', $value->getTimestamp ());
default: default:
return '\'' . $this->conn->escape_string ($value) . '\''; return '\'' . $this->conn->escape_string ($value) . '\'';

4
debian/install vendored
View File

@ -1 +1,3 @@
vn usr/share/php lib usr/share/php/vn
db usr/share/php/vn
vn-autoload.php usr/share/php

View File

@ -2,14 +2,9 @@
namespace Vn\Lib; namespace Vn\Lib;
require_once (__DIR__.'/method.php'); require_once __DIR__.'/util.php';
require_once (__DIR__.'/exception.php');
require_once (__DIR__.'/user-exception.php');
require_once (__DIR__.'/locale.php');
require_once (__DIR__.'/util.php');
require_once ('vn/db/conn.php');
use Vn\Db\Conn; use Vn\Db\Connection;
use Vn\Lib\Locale; use Vn\Lib\Locale;
if (!defined ('_DEBUG_MODE')) if (!defined ('_DEBUG_MODE'))
@ -106,7 +101,7 @@ class App
if ($persistent) if ($persistent)
$host = 'p:'.$host; $host = 'p:'.$host;
$conn = new Conn (); $conn = new Connection ();
$conn->open ( $conn->open (
$host $host
,$user ,$user

View File

@ -2,8 +2,6 @@
namespace Vn\Lib; namespace Vn\Lib;
require_once (__DIR__.'/app.php');
/** /**
* Implements command line applications. * Implements command line applications.
**/ **/

View File

@ -2,8 +2,6 @@
namespace Vn\Lib; namespace Vn\Lib;
require_once (__DIR__.'/app.php');
/** /**
* Base class for rest methods. * Base class for rest methods.
**/ **/
@ -25,14 +23,15 @@ abstract class Method
/** /**
* Executes the method. Shoud be defined by child classes. * Executes the method. Shoud be defined by child classes.
* *
* @param {Db\Conn} $db The main database connection
* @return mixed The result of the method * @return mixed The result of the method
**/ **/
abstract function run (); abstract function run ($db);
/** /**
* Returns the system database connection. * Returns the system database connection.
* *
* return Db\Conn The database connection * return {Db\Conn} The database connection
**/ **/
function getSysConn () function getSysConn ()
{ {

69
lib/type.php Executable file
View File

@ -0,0 +1,69 @@
<?php
namespace Vn\Lib;
class Time extends \DateTime {}
class Date extends \DateTime {}
class Type
{
const NUL = 1;
const BOOLEAN = 2;
const INTEGER = 3;
const DOUBLE = 4;
const STRING = 5;
const OBJECT = 6;
const TIME = 7;
const DATE = 8;
const DATE_TIME = 9;
static function get ($value)
{
if (is_bool ($value))
return self::BOOLEAN;
elseif (is_int ($value))
return self::INTEGER;
elseif (is_float ($value))
return self::DOUBLE;
elseif (is_string ($value))
return self::STRING;
elseif (is_object ($value))
{
if ($value instanceof Time)
return self::TIME;
elseif ($value instanceof Date)
return self::DATE;
elseif ($value instanceof \DateTime)
return self::DATE_TIME;
else
return self::OBJECT;
}
return self::NUL;
}
static function set (& $value, $type)
{
switch ($type)
{
case self::INTEGER:
settype ($value, 'integer');
break;
case self::DOUBLE:
settype ($value, 'float');
break;
case self::STRING:
settype ($value, 'string');
break;
case self::BOOLEAN:
settype ($value, 'boolean');
break;
case self::NUL:
$value = NULL;
break;
}
}
}
?>

View File

@ -2,8 +2,6 @@
namespace Vn\Lib; namespace Vn\Lib;
require_once (__DIR__.'/exception.php');
/** /**
* Class used to store information about errors, warnings and info messages, * Class used to store information about errors, warnings and info messages,
* that end users understood and are allowed to see. * that end users understood and are allowed to see.

41
vn-autoload.php Normal file
View File

@ -0,0 +1,41 @@
<?php
$vnAutoloadReplace = function ($matches)
{
$match = strtolower ($matches[0]);
if (strlen ($match) == 1)
return $match;
if ($match{0} == '\\')
return DIRECTORY_SEPARATOR. $match{1};
return $match{0} .'-'. $match{1};
};
spl_autoload_register (function ($className)
{
global $vnAutoloadReplace, $vnAutoloadMap;
$classPath = preg_replace_callback ('/.?[A-Z]/', $vnAutoloadReplace, $className);
$classPath .= '.php';
if (isset ($vnAutoloadMap))
foreach ($vnAutoloadMap as $prefix => $location)
{
$prefixLen = strlen ($prefix);
if (strncmp ($classPath, $prefix, $prefixLen) == 0)
{
$classPath = "$location/". substr ($classPath, $prefixLen);
break;
}
}
if ($classPath{0} != DIRECTORY_SEPARATOR)
$classPath = stream_resolve_include_path ($classPath);
if (file_exists ($classPath))
require_once $classPath;
});
?>

View File

@ -1,64 +0,0 @@
<?php
const TYPE_NULL = 1;
const TYPE_BOOLEAN = 2;
const TYPE_INTEGER = 3;
const TYPE_DOUBLE = 4;
const TYPE_STRING = 5;
const TYPE_OBJECT = 6;
const TYPE_TIME = 7;
const TYPE_DATE = 8;
const TYPE_DATE_TIME = 9;
class Time extends DateTime {}
class Date extends DateTime {}
function get_type ($value)
{
if (is_bool ($value))
return TYPE_BOOLEAN;
elseif (is_int ($value))
return TYPE_INTEGER;
elseif (is_float ($value))
return TYPE_DOUBLE;
elseif (is_string ($value))
return TYPE_STRING;
elseif (is_object ($value))
{
if ($value instanceof Time)
return TYPE_TIME;
elseif ($value instanceof Date)
return TYPE_DATE;
elseif ($value instanceof DateTime)
return TYPE_DATE_TIME;
else
return TYPE_OBJECT;
}
return TYPE_NULL;
}
function set_type (& $value, $type)
{
switch ($type)
{
case TYPE_INTEGER:
settype ($value, 'integer');
break;
case TYPE_DOUBLE:
settype ($value, 'float');
break;
case TYPE_STRING:
settype ($value, 'string');
break;
case TYPE_BOOLEAN:
settype ($value, 'boolean');
break;
case TYPE_NULL:
$value = NULL;
break;
}
}
?>