Skip to content

Commit

Permalink
Version 0.0.47
Browse files Browse the repository at this point in the history
  • Loading branch information
francescobianco committed Oct 18, 2017
1 parent 018df15 commit fbd5391
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "javanile/moldable",
"type": "library",
"version": "0.0.46",
"version": "0.0.47",
"description": "Uknown PHP Database Abstraction Layer",
"keywords": [
"moldable",
Expand Down
32 changes: 32 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class Context
*/
private static $_useLaravel = true;

/**
* @var bool
*/
private static $_container = null;

/**
* Check if run inside laravel.
*/
Expand All @@ -35,4 +40,31 @@ public static function useLaravel($flag)
{
static::$_useLaravel = (bool) $flag;
}

/**
* Apply use laravel flag.
*
* @param mixed $flag
*/
public static function registerContainer($container)
{
static::$_container = $container;
}

/**
*
*/
public static function checkContainer()
{
return static::$_container != null;
}

/**
*
*/
public static function getContainerDatabase()
{
return static::$_container->db;
}
}

9 changes: 7 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ public function __construct($args)
$this->_parser = new Parser\MysqlParser();
$this->_writer = new Writer\MysqlWriter();

if (isset($args['debug'])) {
if (isset($args['debug']) && $args['debug']) {
$this->setDebug($args['debug']);
}

// Logger
$logFile = isset($args['log']) ? $args['log'] : getcwd().'/moldable.log';
$logFlag = isset($args['debug']) ? Logger::INFO : Logger::ERROR;
$logFlag = isset($args['debug']) && $args['debug'] ? Logger::INFO : Logger::ERROR;
$this->_logger = new Logger('name');
$this->_logger->pushHandler(new StreamHandler($logFile, $logFlag));

Expand All @@ -156,6 +156,11 @@ public static function getDefault()

return static::$_default;
}

// check if registered container
if (Context::checkContainer()) {
return Context::getContainerDatabase();
}
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,21 @@ public static function throwException(
$exception,
$trace = null,
$offset = 0
) {
)
{
$info = is_object($exception) ? $exception->getMessage() : $exception;
//$code = is_object($exception) ? $exception->getCode() : 0;

$message = $slug.$info;
$message = $slug . $info;
if (isset($trace[$offset]['function'])) {
$message .= ' in method '."'->".$trace[$offset]['function']."()'"
.' called at '.$trace[$offset]['file']
.' on line '.$trace[$offset]['line'];
} else {
$message .= ' in method ' . "'->" . $trace[$offset]['function'] . "()'"
. ' called at ' . $trace[$offset]['file']
. ' on line ' . $trace[$offset]['line'];
} elseif ($trace) {
$message .= ' declared at '.$trace[$offset]['file'];
//.' on line '.$trace[$offset]['line'];
} else {
$message .= '.';
}

throw new Exception($message);
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ClassApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait ClassApi
* @var type
*/
protected static $__global = [
'schema-excluded-fields' => [
'exclude-fields' => [
'__global',
'__attrib',
'__config',
Expand Down
9 changes: 4 additions & 5 deletions src/Model/DebugApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait DebugApi
public static function error($type, $exception)
{
$reflector = new \ReflectionClass(static::getClass());
$offset = 0;

switch ($type) {
case 'class':
Expand All @@ -26,13 +27,11 @@ public static function error($type, $exception)
'file' => $reflector->getFileName(),
'line' => $reflector->getStartLine(),
]];
$offset = 0;
break;

case 'database':
$slug = 'Moldable database error, ';
$backtrace = debug_backtrace();
$offset = 0;
case 'connection':
$slug = 'Moldable connection error, ';
$backtrace = null;
break;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Model/FieldApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ public function getMainFieldValue()
: null;
}

/**
* Get PrimaryKey name or MainField name.
*/
protected static function getPrimaryKeyOrMainField()
{
//
Expand All @@ -182,6 +185,14 @@ protected static function getPrimaryKeyOrMainField()
return $key ? $key : static::getMainField();
}

/**
* Get list of Class-Model fields use to store or read data.
*/
public static function getModelFields()
{
return static::getSchemaFields();
}

/**
* Retrieve primary key field name.
*
Expand Down Expand Up @@ -275,6 +286,7 @@ protected static function getAllFieldsValues()
}

/**
* Get all fields of called class.
*
*/
protected static function getAllFields()
Expand Down
16 changes: 9 additions & 7 deletions src/Model/SchemaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
trait SchemaApi
{
/**
* Apply schema model related.
*
* @return type
*/
public static function applySchema()
Expand All @@ -30,10 +32,6 @@ public static function applySchema()

$database = static::getDatabase();

if (!$database) {
static::error('database not found', debug_backtrace(), 2);
}

$schema = static::getSchema();

if (!$schema) {
Expand Down Expand Up @@ -150,6 +148,9 @@ protected static function getSchemaFieldsValues()
return static::getClassAttribute($attribute);
}

/**
* Describe model.
*/
public static function desc()
{
static::applySchema();
Expand All @@ -173,7 +174,10 @@ public static function getDatabase()
$database = Database::getDefault();

if (!$database) {
static::error('database', 'database connection not found');
static::error(
'connection',
'database not found, instantiate one or register a dependency injection container'
);
}

static::setClassAttribute($attribute, $database);
Expand All @@ -191,10 +195,8 @@ public static function getDatabase()
*/
public static function setDatabase($database)
{
//
$attribute = 'database';

//
static::setClassAttribute($attribute, $database);
}
}

0 comments on commit fbd5391

Please sign in to comment.