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

Fixes #35: Removendo contantes de configuração. #37

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
25 changes: 13 additions & 12 deletions src/DAO/GenericDAO.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace PseudoORM\DAO;

use PseudoORM\Services\IDataBaseCreator;
use PseudoORM\Services\IDatabaseCreator;
use PseudoORM\Entity\EntidadeBase;
use PseudoORM\Exception;
use \PDO;
Expand Down Expand Up @@ -224,20 +224,21 @@ private function bindArrayValue(\PDOStatement $query, $array, $typeArray = false
/**
* {@inheritDoc}
*/
public function generate(IDataBaseCreator $creator, $create = false)
public function geraScriptDeCriacaoDoBancoDeDados(IDatabaseCreator $creator)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this method be removed? it doesn't do too much now...

{
$script = $creator->scriptCreation($this->type, true);

if ($create == false) {
return $script;
} else {
// TODO extract to method
try {
$dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ));
$dbh->exec($script);// or die(print_r($dbh->errorInfo(), true));
} catch (PDOException $e) {
echo ("DB ERROR: ". $e->getMessage());
}
return $script;
}

public function criaBancoDeDados(IDatabaseCreator $creator)
{
$script = $this->geraScriptDeCriacaoDoBancoDeDados($creator);
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the try, let it fail instead.

$dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Que tal passar essa instancia do PDO via parametro?

$dbh->exec($script);
} catch (PDOException $e) {
echo ("DB ERROR: ". $e->getMessage());
}
}
}
10 changes: 7 additions & 3 deletions src/DAO/IGenericDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ public function insert(EntidadeBase $entidade);
public function update(EntidadeBase $entidade);

/**
* Gera script de criação do banco de dados e permite a criação automatica.
* @param bolean $create True to create database automatically | False To print script in screen
* Retorna script de criação do banco de dados.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return?

*/
public function generate(IDataBaseCreator $creator, $create = false);
public function geraScriptDeCriacaoDoBancoDeDados(IDataBaseCreator $creator);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English or Portuguese? let's avoid mix languages in here.


/**
* Cria banco de dados
*/
public function criaBancoDeDados(IDataBaseCreator $creator);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDatabaseCreator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return void?

}
10 changes: 10 additions & 0 deletions src/Entity/EntidadeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ final public static function createFromForm($params)
}
}
}

public function getClassShortName()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc?

{
return (new \ReflectionClass($this))->getShortName();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about proxy this ReflectionClass?

}

public function getClass()
{
return (new \ReflectionClass($this))->getName();
}


public function setUID($uid)
Expand Down
20 changes: 7 additions & 13 deletions src/Factory/AppFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ public static function getFactory()

public static function getRepository(EntidadeBase $objeto)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it static?

{
try {
$class = get_class($objeto);
$respositoryPath = DAOS . $class . 'DAO.php';
if (!file_exists($respositoryPath)) {
return new GenericDAO($class);
}
require_once $respositoryPath;
$repository = $class . 'DAO';
return new $repository;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
$repository = '\\PseudoORM\\DAO\\' . $objeto->getClassShortName() . 'DAO';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be in a config/map instead?

if(class_exists($repository))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSR-2

return new $repository($objeto->getClass());

return new GenericDAO($objeto->getClass());
}
}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL EOF

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PseudoORM\Services;

interface IDataBaseCreator
interface IDatabaseCreator
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace PseudoORM\Services;

use PseudoORM\Services\IDataBaseCreator;
use PseudoORM\Services\IDatabaseCreator;

use Addendum\ReflectionAnnotatedClass;

class PostgreSQLDataBaseCreator implements IDataBaseCreator
class PostgreSQLDatabaseCreator implements IDatabaseCreator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, shouldn't it be PostgresSQLDriver?

{

protected $tableName;
Expand Down
12 changes: 6 additions & 6 deletions test/functional/check-environment.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ define("DB_PORT", 5432);
define("DB_NAME", 'pseudoorm');
define("SCHEMA", '');
define('ENCODING', "SET NAMES 'utf8';");

/**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This demarks a phpdoc not a comment... please use /* @todo blah */ instead

* @todo Remover constantes
*/
define("DB_DSN", "pgsql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME.";");
define("SHOW_SQL_ERROR", PDO::ERRMODE_EXCEPTION);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mark this constants to be removed in the future as well? @todo ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marquei como @todo


define('MODELS', '../app/models/');
define('DAOS', MODELS . 'DAO/impl/');
define('EXCEPTIONS', MODELS . 'exception/');

use PseudoORM\Entity\Usuario;
use PseudoORM\Factory\AppFactory;
use PseudoORM\Services\PostgreSQLDataBaseCreator;
use PseudoORM\Services\PostgreSQLDatabaseCreator;

require_once 'src/Annotations/Column.php';
require_once 'src/Annotations/Id.php';
Expand All @@ -36,7 +36,7 @@ require_once 'src/Annotations/Persistent.php';
$dao = AppFactory::getRepository(new Usuario());

// USe para gerar o script de criação do banco
$dao->generate(new PostgreSQLDataBaseCreator(), true);
$dao->criaBancoDeDados(new PostgreSQLDatabaseCreator());

// Realizar operações básicas
$usuario = $dao->create();
Expand Down