Skip to content

Commit

Permalink
Versão DEV.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaduAmaral committed Jun 9, 2016
1 parent 661ab3a commit 6c0c600
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 73 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Base – 0.0.0-DEV.4
>>>>>>> origin/master
# Base – 0.0.0-DEV.4.1

Base é uma "base" para construção de soluções em MVC com PHP.
Construi o _framework_ para uso próprio, porém resolvi disponibilizar para a comunidade em geral.
Expand All @@ -23,6 +22,7 @@ Muita coisa pra fazer. Ainda não foi especificado um roteiro, mas aqui estão a
2. Criar estrutura de `Exceptions` e realizar _log_ dos erros e excessões para _debug_;
3. Criar métodos e recursos para facilitar o _debug_ da aplicação;
4. Criar recurso de instalação facilitando a configuração dos arquivos necessários;
5. Implementar sistema de rotas.


## Licença
Expand Down
15 changes: 8 additions & 7 deletions app/appsconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

return objectify(Array(
'example' => Array(
'url' => 'http://localhost/Base/public_html/',
'database' => Array(
'url' => 'http://localhost/Base/public_html/',
'database' => Array(
'driver' => 'mysql',
'port' => 3306,
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'schema' => 'test;charset=utf8'
'port' => 3306,
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'schema' => 'test',
'charset' => 'utf8'
)
)
));
31 changes: 21 additions & 10 deletions base/core/application.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,30 @@ public static function RUN($app = null) {
// Retorno caso configuração $outputreturn do controller seja true
$output = '';

try {

if (class_exists($class)) {
$app = New $class($request);
} else {
throw new Exception("Requisição inválida");
}

} catch (Exception $e) {
$app = New ErrorController($request);
$app->setOutput($app->index());
$app->output();
return FALSE;
}


if (!empty($request->post['mvc:model'])){
$model = '\Model\\' . array_remove($request->post, 'mvc:model') . 'Model';
try {
$param = New $model($request->post);
} catch (Exception $e) {
$app = New \Controller\ErrorController($request);
$app->setOutput($app->index());
$app->output();
return FALSE;
}
} else if (empty($request->lost) && !is_numeric($request->lost))
$param = NULL;
Expand All @@ -40,19 +56,14 @@ public static function RUN($app = null) {


try {
if (class_exists($class)) {
$app = New $class($request);
$output = $app->execute($param);
} else {
throw new Exception("Requisição inválida");
}

$output = $app->execute($param);
} catch (Exception $e) {
$app = New ErrorController($request);
$output = $app->index();
$app->setOutput($app->index());
$app->output();
return FALSE;
}


if ($app->outputreturn)
$app->setOutput($output);

Expand Down
46 changes: 7 additions & 39 deletions base/core/connection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,13 @@ function __construct($settings) {
$dns = $settings->driver .
':host=' . $settings->host .
';port=' . $settings->port .
';dbname=' . $settings->schema;
parent::__construct($dns, $settings->user, $settings->pass);
}

public function save(Model &$model){

$reference = $model->getReference();
$pk = $model->getPK();

$data = get_object_vars($model);

if (empty($pk))
throw new \Exception('Model PK is empty!', 54);

$pkv = $data[$pk];

unset($data[$pk]);

if (empty($pkv)) {
if (array_key_exists('created', $data))
$data['created'] = Date('Y-m-d H:i:s');
';dbname=' . $settings->schema .
';charset='.(!empty($settings->charset) ? $settings->charset : 'utf8');

$res = $this->insert($reference, $data)->execute();

if ($res)
$model->$pk = $this->lastInsertId();

return $res;
} else {

if (array_key_exists('modified', $data))
$data['modified'] = Date('Y-m-d H:i:s');

foreach ($data as $col => $value)
if (empty($value) && $value !== NULL && $value !== 0)
unset($data[$col]);

return $this->update($reference, $data, [$pk => $pkv])->execute();
}
parent::__construct($dns, $settings->user, $settings->pass);

if (defined('DEBUG') && DEBUG === TRUE)
$this->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
}
}

}
39 changes: 25 additions & 14 deletions base/core/model.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class Model {


function __construct($data = []) {
if (!empty($data)) $this->_setData($data);
if (!empty($data)) {
$this->_preLoad($data);
$this->_setData($data);
}
}

public function refresh() {
Expand All @@ -27,6 +30,26 @@ public function refresh() {

}

public function _preLoad($data) {
$pk = self::_getPK();
if (!is_array($pk)) $pk = [$pk];
$pks = [];

$haspk = TRUE;

foreach ($pk as $key)
if (!empty($data[$key]))
$pks[$key] = $data[$key];
else
$haspk = FALSE;

if ($haspk && !empty($pks)) {
$this->_setData($pks);
$this->refresh();
}

}

private function _setData($data) {
if (!empty($data)){

Expand Down Expand Up @@ -129,7 +152,6 @@ public static function getAll() {
* @return Array(Model)
*/
public static function getWhere($where, $shift = TRUE, $setobj = TRUE) {

$stmt = self::$connection->select( self::_getReference(), $where );
$res = $stmt->execute();

Expand Down Expand Up @@ -260,21 +282,15 @@ public function save(){
unset($data[$key]);
}

define('DEBUG', TRUE);

if ($insert) {


if (array_key_exists('created', $data))
$data['created'] = Date('Y-m-d H:i:s');

if (array_key_exists('modified', $data))
unset($data['modified']);


if (defined('DEBUG') && DEBUG === TRUE)
self::$connection->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING );

$res = self::$connection->insert($reference, $data)->execute();

if ($res && $pk !== NULL && !is_array($pk)) {
Expand All @@ -293,11 +309,6 @@ public function save(){
if (array_key_exists('created', $data))
unset($data['created']);

foreach ($data as $col => $value)
if (empty($value) && $value !== NULL && $value !== 0)
unset($data[$col]);

if (defined('DEBUG') && DEBUG === TRUE) self::$connection->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING );
$res = self::$connection->update($reference, $data, $_pkv)->execute();

$this->afterSave($res);
Expand All @@ -307,4 +318,4 @@ public function save(){

}

}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"minimum-stability": "dev",
"require": {
"php": ">=5.5.33",
"kaduamaral/connectionpdo": "~1.0",
"kaduamaral/connectionpdo": "~1.1",
"phpmailer/phpmailer": "~5.2",
"mobiledetect/mobiledetectlib": "~2.8"
}
Expand Down

0 comments on commit 6c0c600

Please sign in to comment.