Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php5.6 1.x #1

Open
wants to merge 63 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
a4c4ccb
Update README.md
Sep 25, 2017
467700a
Added model
Sep 25, 2017
4dc750f
Added controller
Sep 25, 2017
1476ddf
Added ModelInterface
Sep 25, 2017
aa41e34
Update composer.json
ulises-jeremias Sep 28, 2017
86dc6d4
Update composer.json
ulises-jeremias Sep 28, 2017
9023d58
Update composer.json
ulises-jeremias Sep 28, 2017
0e5ab51
Update composer.json
ulises-jeremias Sep 28, 2017
df7e7c1
Update composer.json
ulises-jeremias Sep 28, 2017
ba96ca0
StdConnection class
ulises-jeremias Sep 30, 2017
e23c0f1
Update composer.json
ulises-jeremias Sep 30, 2017
641de7b
Update composer.json
ulises-jeremias Sep 30, 2017
e358f11
Update composer.json
ulises-jeremias Sep 30, 2017
aa52d5a
Preparing mysql connection class
ulises-jeremias Sep 30, 2017
6e0fb0f
Connection class added
ulises-jeremias Sep 30, 2017
2bc5fcc
Fix
ulises-jeremias Sep 30, 2017
694176a
Model added
ulises-jeremias Oct 1, 2017
b218dab
Model class methods added
ulises-jeremias Oct 1, 2017
741f0ce
Model fix
ulises-jeremias Oct 1, 2017
d1b2320
Added restriction to composer.lock
Oct 1, 2017
437f5ab
Merge branch 'PHP5.6-1.x' of github.com:mbhframework/mbh-mvc into PHP…
Oct 1, 2017
9e73ecf
Fix
ulises-jeremias Oct 1, 2017
ae57fec
Composer fix
ulises-jeremias Oct 1, 2017
243bb29
Add logical to Controller contruct
Oct 2, 2017
4a2286f
Model functions added
ulises-jeremias Oct 2, 2017
1b753a7
Fix indentation
Oct 2, 2017
265a348
Added inflate helper
Oct 2, 2017
a3dc7b4
Fix
ulises-jeremias Oct 2, 2017
81d3e00
Added automatic model value
Oct 2, 2017
077993b
Added create mathod
Oct 2, 2017
ceac030
Model
ulises-jeremias Oct 2, 2017
a804989
Merge branch 'PHP5.6-1.x' of https://github.com/MBHFramework/mbh-mvc …
ulises-jeremias Oct 2, 2017
b0ccd6c
Model changes
ulises-jeremias Oct 2, 2017
ba954aa
Models
ulises-jeremias Oct 2, 2017
038c794
Templates
ulises-jeremias Oct 2, 2017
5735c90
Model
ulises-jeremias Oct 2, 2017
25d2874
Dependent
ulises-jeremias Oct 2, 2017
2ec6e75
Controller change
ulises-jeremias Oct 2, 2017
ae60e23
Controller changes
ulises-jeremias Oct 2, 2017
264eac4
Controller update
ulises-jeremias Oct 3, 2017
7e47ef9
Fix
ulises-jeremias Oct 3, 2017
aa168e2
Sweet
ulises-jeremias Oct 3, 2017
bc0487a
Fix
ulises-jeremias Oct 3, 2017
1182a7a
Fix
ulises-jeremias Oct 3, 2017
659aa4e
Model fix
ulises-jeremias Oct 3, 2017
05ad577
Fix
ulises-jeremias Oct 6, 2017
81b048d
Fix
ulises-jeremias Oct 6, 2017
6d1e165
Refresh function added
ulises-jeremias Oct 7, 2017
db05361
Fix
ulises-jeremias Oct 7, 2017
41bcfaa
Model changes
ulises-jeremias Oct 7, 2017
9fd9a26
Some changes
ulises-jeremias Oct 8, 2017
af7d4fe
Fix
ulises-jeremias Oct 9, 2017
2a17555
Fix
ulises-jeremias Oct 9, 2017
a0280e5
Fix
ulises-jeremias Oct 9, 2017
84209d4
Fix
ulises-jeremias Oct 9, 2017
815fb34
Fix
ulises-jeremias Oct 9, 2017
a5dce02
Equals method added
ulises-jeremias Oct 10, 2017
5f8f5d7
Model fix
ulises-jeremias Oct 11, 2017
c6db859
Update Model.php
ulises-jeremias Oct 13, 2017
74c8ab4
Added __toString and updateWith methods
Oct 30, 2017
161de6d
Added methods updateWith and
Nov 9, 2017
d9a0c64
Readd methods deleted by error
Nov 11, 2017
458560a
Update Model.php
ulises-jeremias Nov 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
/composer.lock
28 changes: 28 additions & 0 deletions Mbh/Connection/Engines/Mysql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* MBHFramework
*
* @link https://github.com/MBHFramework/mbh-framework
* @copyright Copyright (c) 2017 COD-Project
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
*/

namespace Mbh\Connection\Engines;

use Mbh\Connection\StdConnection;

/**
* created by Ulises Jeremias Cornejo Fandos
*/
class Mysql extends StdConnection
{
public function __construct($database = [])
{
parent::__construct('mysql:host=' . $database['host'] . ';port=' . $database['port'] . ';dbname=' . $database['name'], $database['user'], $database['pass'], [
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]);
}
}
149 changes: 32 additions & 117 deletions Mbh/Connection/StdConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* MBHFramework
*
* @link https://github.com/MBHFramework/mbh-framework
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
* @copyright Copyright (c) 2017 COD-Project
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
*/

Expand All @@ -15,142 +15,69 @@
*/
class StdConnection extends \PDO
{
private static $id;

private static $instance;

/**
* Starts the connection instance, if it has already been declared before, does not duplicate it and saves memory.
*
* @param string $DATABASE, A database other than the one defined in DATABASE ['name'] is optionally passed to connect
* @param array $database
*
* @return connection instance
*/
public static function start(string $DATABASE = DATABASE['name'], string $MOTOR = DATABASE['motor'], bool $new_instance = false)
public static function start($database = [], $new_instance = false)
{
if (!self::$instance instanceof self or $new_instance) {
self::$instance = new self($DATABASE, $MOTOR);
if (!static::$instance instanceof static or $new_instance) {
static::$instance = new static($database);
}

return self::$instance;
return static::$instance;
}


/**
* Starts database connection
*
* @param string $DATABASE, A database other than the one defined in DATABASE ['name'] is optionally passed to connect
* @param array $database
*
* @return void
* @return connection instance
*/
public function __construct(string $DATABASE = DATABASE['name'], string $MOTOR = DATABASE['motor'])
public static function create($database = [])
{
try {
switch ($MOTOR) {
case 'sqlite':
parent::__construct('sqlite:'.$DATABASE);
break;
case 'cubrid':
parent::__construct('cubrid:host='.DATABASE['host'].';dbname='.$DATABASE.';port='.DATABASE['port'], DATABASE['user'], DATABASE['pass'], array(
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
break;
case 'firebird':
parent::__construct('firebird:dbname='.DATABASE['host'].':'.$DATABASE, DATABASE['user'], DATABASE['pass'], array(
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
break;
case 'odbc':
parent::__construct('odbc:'.$DATABASE, DATABASE['user'], DATABASE['pass']);
break;
case 'oracle':
parent::__construct(
'oci:dbname=(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = '.DATABASE['protocol'].')(HOST = '.$MOTOR.')(PORT = '.DATABASE['port'].'))
)
(CONNECT_DATA =
(SERVICE_NAME = '.$DATABASE.')
)
);charset=utf8',
['user'],
['pass'],
[
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
]);
break;
case 'postgresql':
parent::__construct('pgsql:host='.DATABASE['host'].';dbname='.$DATABASE.';charset=utf8', DATABASE['user'], DATABASE['pass'], [
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
]);
break;
case 'mysql':
parent::__construct('mysql:host='.DATABASE['host'].';dbname='.$DATABASE, DATABASE['user'], DATABASE['pass'], [
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
]);
break;
default:
if (IS_API) {
die(json_encode([
'success' => 0,
'message' => 'Unidentified connection engine.'
]));
} else {
die('Unidentified connection engine.');
}
break;
}
} catch (\PDOException $e) {
if (IS_API) {
die(json_encode([
'success' => 0,
'message' => 'Error attempting to connect to database.'
]));
$driver = isset($database['driver']) ? ucwords($database['driver']) : "";
$className = __NAMESPACE__ . "\\Engines\\" . $driver;
if (class_exists($className)) {
return new $className($database);
} else {
die('Error attempting to connect to database.');
throw new \RuntimeException("Unidentified connection engine $className.");
}
} finally {
unset($DATABASE, $MOTOR);
} catch (\PDOException $e) {
throw new \RuntimeException('Problem connecting to the database: ' . $e->getMessage());
}
}

public function lastInsertId()
{
return parent::lastInsertId() ?? $this->id;
}


/**
* Returns an associative array of all the results thrown by a query
*
* @param object \PDOStatement $query, return value of the query
*
* @return associative array
*/
public function fetchArray(\PDOStatement $query): array
public function fetchArray($query)
{
return $query->fetchAll(\PDO::FETCH_ASSOC);
}


/**
* Get the number of rows found after a SELECT
*
* @param object \PDOStatement $query, return value of the query
*
* @return number of rows found
*/
public function rows(\PDOStatement $query): int
public function rows($query)
{
return $query->rowCount();
}


/**
* Heals a value to be later entered into a query
*
Expand All @@ -163,14 +90,12 @@ public function escape($e)
if (!isset($e)) {
return '';
}

if (is_numeric($e) and PHP_INT_MIN <= $e and $e <= PHP_INT_MAX) {
if (explode('.', $e)[0] != $e) {
return (float) $e;
}
return (int) $e;
}

return (string) trim(str_replace(['\\',"\x00",'\n','\r',"'",'"',"\x1a"], ['\\\\','\\0','\\n','\\r',"\'",'\"','\\Z'], $e));
}

Expand All @@ -181,21 +106,14 @@ public function escape($e)
*
* @return object \PDOStatement
*/
public function query(string $q): \PDOStatement
public function query($q)
{
try {
if (DEBUG) {
$_SESSION['___QUERY_DEBUG___'][] = (string) $q;
}

$_SESSION['___QUERY_DEBUG___'][] = (string) $q;
return parent::query($q);
} catch (Exception $e) {
if (defined(GENERATOR)) {
$message = "\nError in query:\n $q \n\n" . $e->getMessage();
} else {
$message = 'Error in query: <b>' . $q . '<b/><br /><br />' . $e->getMessage();
}
die(IS_API ? json_encode(array('success' => 0, 'message' => $message)): $message);
} catch (\PDOException $e) {
throw new \Exception('Error in query: <b>' . $q . '<b/><br /><br />' . $e->getMessage());

}
}

Expand All @@ -208,7 +126,7 @@ public function query(string $q): \PDOStatement
*
* @return object \PDOStatement
*/
public function delete(string $table, string $where, string $limit = 'LIMIT 1'): \PDOStatement
public function delete($table, $where, $limit = 'LIMIT 1')
{
return $this->query("DELETE FROM $table WHERE $where $limit;");
}
Expand All @@ -222,12 +140,11 @@ public function delete(string $table, string $where, string $limit = 'LIMIT 1'):
*
* @return object \PDOStatement
*/
public function insert(string $table, array $e): \PDOStatement
public function insert($table, $e)
{
if (sizeof($e) == 0) {
trigger_error('array passed in $this->db->insert(...) is empty.', E_ERROR);
trigger_error('array passed in Connection::insert(...) is empty.', E_ERROR);
}

$query = "INSERT INTO $table (";
$values = '';
foreach ($e as $campo => $v) {
Expand All @@ -238,8 +155,6 @@ public function insert(string $table, array $e): \PDOStatement
$values[strlen($values) - 1] = ')';
$query .= ' VALUES (' . $values . ';';

self::$id = parent::lastInsertId($table);

return $this->query($query);
}

Expand All @@ -254,12 +169,11 @@ public function insert(string $table, array $e): \PDOStatement
*
* @return object \PDOStatement
*/
public function update(string $table, array $e, string $where, string $limit = ''): \PDOStatement
public function update($table, $e, $where, $limit = '')
{
if (sizeof($e) == 0) {
trigger_error('El arreglo pasado por $this->db->update(...) está vacío.', E_ERROR);
}

$query = "UPDATE $table SET ";
foreach ($e as $campo => $valor) {
$query .= $campo . '=\'' . $this->escape($valor) . '\',';
Expand All @@ -280,22 +194,23 @@ public function update(string $table, array $e, string $where, string $limit = '
*
* @return False if you do not find any results, array associative / numeric if you get at least one
*/
public function select(string $e, string $table, string $where = '1 = 1', string $limit = "")
public function select($e, $table, $where = '1 = 1', $limit = "")
{
$sql = $this->query("SELECT $e FROM $table WHERE $where $limit;");
$result = $sql->fetchAll();
$result = $sql->fetchAll(\PDO::FETCH_ASSOC);
$sql->closeCursor();

return $sql;
return $result;
}

/**
* Alert to avoid cloning
*
* @throws \RuntimeException
* @return void
*/
public function __clone()
{
trigger_error('Estás intentando clonar la Conexión', E_ERROR);
throw new \RuntimeException('You are trying to clone the Connection', E_ERROR);
}
}
37 changes: 32 additions & 5 deletions Mbh/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,46 @@
/**
* MBHFramework
*
* @link https://github.com/MBHFramework/mbh-framework
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
* @link https://github.com/MBHFramework/mbh-mvc
* @copyright Copyright (c) 2017 COD-Project (https://github.com/COD-Project)
* @license https://github.com/MBHFramework/mbh-mvc/blob/PHP5.6-1.x/LICENSE (MIT License)
*/

namespace Mbh;

use Mbh\Helpers\Inflect;
use Mbh\Interfaces\ControllerInterface;

/**
* created by Ulises Jeremias Cornejo Fandos
* created by Lucas Di Cunzolo
*/
class Controller
{
function __construct()
protected $model;

protected $template;

protected $app;

public static function create($controller, $args)
{
if(!class_exists($controller)) {
throw new \RuntimeException;
}

return new $controller(...$args);
}

function __construct($app = null)
{
$className = explode('\\', get_called_class());

$modelName = ucwords(Inflect::pluralize(
str_replace("Controller", "", $className[count($className)-1])
));

$this->model = !class_exists($modelName) ?: $modelName;

$this->app = $app;
}
}
Loading