From c6b06c2131a86dccda26fc0a6364fc3db66b06eb Mon Sep 17 00:00:00 2001 From: Jack Makiyama Date: Thu, 29 Dec 2016 00:34:49 -0300 Subject: [PATCH 1/8] =?UTF-8?q?Removendo=20contantes=20de=20configura?= =?UTF-8?q?=C3=A7=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Foi removido as contantes que tinha caminhos de pastas fisicos e no seu lugar foi adicionado na AppFactory as namespace hardcoded, gerando um debito tecnico, mas removendo essas contantes. --- src/Factory/AppFactory.php | 26 +++++++++++++------------- test/functional/check-environment.phpt | 4 ---- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/Factory/AppFactory.php b/src/Factory/AppFactory.php index 8d54390..7e5a77f 100644 --- a/src/Factory/AppFactory.php +++ b/src/Factory/AppFactory.php @@ -2,7 +2,6 @@ namespace PseudoORM\Factory; use PseudoORM\Entity\EntidadeBase; -use PseudoORM\DAO\GenericDAO; use \Exception; class AppFactory @@ -33,17 +32,18 @@ public static function getFactory() public static function getRepository(EntidadeBase $objeto) { - 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()); - } + $class = get_class($objeto); + $classShortName = (new \ReflectionClass($objeto))->getShortName(); + + $classDAO = '\\PseudoORM\\DAO\\' . $classShortName . 'DAO'; + + $entityName = class_exists($classDAO) + ? $classShortName + : 'Generic' + ; + + $repository = '\\PseudoORM\\DAO\\' . $entityName . 'DAO'; + + return new $repository($class); } } diff --git a/test/functional/check-environment.phpt b/test/functional/check-environment.phpt index 7de1a37..422f16f 100644 --- a/test/functional/check-environment.phpt +++ b/test/functional/check-environment.phpt @@ -14,10 +14,6 @@ define('ENCODING', "SET NAMES 'utf8';"); define("DB_DSN", "pgsql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME.";"); define("SHOW_SQL_ERROR", PDO::ERRMODE_EXCEPTION); -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; From f319875c5bc5b0353caba63e1cafcb27b112af23 Mon Sep 17 00:00:00 2001 From: evandro Date: Sun, 18 Jun 2017 12:52:11 -0300 Subject: [PATCH 2/8] Ajustando classe com sugestoes do @malukenho --- src/Factory/AppFactory.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Factory/AppFactory.php b/src/Factory/AppFactory.php index 7e5a77f..1eedd22 100644 --- a/src/Factory/AppFactory.php +++ b/src/Factory/AppFactory.php @@ -2,6 +2,7 @@ namespace PseudoORM\Factory; use PseudoORM\Entity\EntidadeBase; +use PseudoORM\DAO\GenericDAO; use \Exception; class AppFactory @@ -32,18 +33,13 @@ public static function getFactory() public static function getRepository(EntidadeBase $objeto) { - $class = get_class($objeto); $classShortName = (new \ReflectionClass($objeto))->getShortName(); - $classDAO = '\\PseudoORM\\DAO\\' . $classShortName . 'DAO'; + $repository = '\\PseudoORM\\DAO\\' . $classShortName . 'DAO'; - $entityName = class_exists($classDAO) - ? $classShortName - : 'Generic' - ; + if(class_exists($repository)) + return new $repository((new \ReflectionClass($objeto))->getName()); - $repository = '\\PseudoORM\\DAO\\' . $entityName . 'DAO'; - - return new $repository($class); + return new GenericDAO((new \ReflectionClass($objeto))->getName()); } -} +} \ No newline at end of file From 859d7a5cd01ffdd43b8d74a356dca60bc4eb4aa6 Mon Sep 17 00:00:00 2001 From: evandro Date: Sun, 18 Jun 2017 13:12:57 -0300 Subject: [PATCH 3/8] Refatorando para separar responsabilidades --- src/Entity/EntidadeBase.php | 10 ++++++++++ src/Factory/AppFactory.php | 8 +++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Entity/EntidadeBase.php b/src/Entity/EntidadeBase.php index be58ecd..6d3a6d3 100644 --- a/src/Entity/EntidadeBase.php +++ b/src/Entity/EntidadeBase.php @@ -17,6 +17,16 @@ final public static function createFromForm($params) } } } + + public function getClassShortName() + { + return (new \ReflectionClass($this))->getShortName(); + } + + public function getClass() + { + return (new \ReflectionClass($objeto))->getName(); + } public function setUID($uid) diff --git a/src/Factory/AppFactory.php b/src/Factory/AppFactory.php index 1eedd22..462be0e 100644 --- a/src/Factory/AppFactory.php +++ b/src/Factory/AppFactory.php @@ -33,13 +33,11 @@ public static function getFactory() public static function getRepository(EntidadeBase $objeto) { - $classShortName = (new \ReflectionClass($objeto))->getShortName(); - - $repository = '\\PseudoORM\\DAO\\' . $classShortName . 'DAO'; - + $repository = '\\PseudoORM\\DAO\\' . $objeto->getClassShortName() . 'DAO'; if(class_exists($repository)) - return new $repository((new \ReflectionClass($objeto))->getName()); + return new $repository($objeto->getClass()); return new GenericDAO((new \ReflectionClass($objeto))->getName()); } + } \ No newline at end of file From 17c9a30f0a19926f551f3ecbef79acb43b2f68b3 Mon Sep 17 00:00:00 2001 From: evandro Date: Sun, 18 Jun 2017 13:15:40 -0300 Subject: [PATCH 4/8] =?UTF-8?q?Refatorando=20para=20separar=20responsabili?= =?UTF-8?q?dades,=20corrigindo=20chamada=20de=20m=C3=A9todos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Entity/EntidadeBase.php | 2 +- src/Factory/AppFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entity/EntidadeBase.php b/src/Entity/EntidadeBase.php index 6d3a6d3..fb66b49 100644 --- a/src/Entity/EntidadeBase.php +++ b/src/Entity/EntidadeBase.php @@ -25,7 +25,7 @@ public function getClassShortName() public function getClass() { - return (new \ReflectionClass($objeto))->getName(); + return (new \ReflectionClass($this))->getName(); } diff --git a/src/Factory/AppFactory.php b/src/Factory/AppFactory.php index 462be0e..7765449 100644 --- a/src/Factory/AppFactory.php +++ b/src/Factory/AppFactory.php @@ -37,7 +37,7 @@ public static function getRepository(EntidadeBase $objeto) if(class_exists($repository)) return new $repository($objeto->getClass()); - return new GenericDAO((new \ReflectionClass($objeto))->getName()); + return new GenericDAO($objeto->getClass()); } } \ No newline at end of file From 483a99cbf9c549b1de88b8aa7f63e9ee9c39d361 Mon Sep 17 00:00:00 2001 From: evandro Date: Sun, 18 Jun 2017 13:40:22 -0300 Subject: [PATCH 5/8] =?UTF-8?q?Refatorando=20m=C3=A9todo=20de=20gera=C3=A7?= =?UTF-8?q?=C3=A3o=20do=20banco=20de=20dados=20para=202=20m=C3=A9todos=20d?= =?UTF-8?q?iferentes,=20removendo=20par=C3=A2metro=20booleano?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DAO/GenericDAO.php | 25 ++++++++++--------- src/DAO/IGenericDAO.php | 10 +++++--- ...taBaseCreator.php => IDatabaseCreator.php} | 2 +- ...ator.php => PostgreSQLDatabaseCreator.php} | 4 +-- 4 files changed, 23 insertions(+), 18 deletions(-) rename src/Services/{IDataBaseCreator.php => IDatabaseCreator.php} (91%) rename src/Services/{PostgreSQLDataBaseCreator.php => PostgreSQLDatabaseCreator.php} (96%) diff --git a/src/DAO/GenericDAO.php b/src/DAO/GenericDAO.php index 21bf422..04fb118 100644 --- a/src/DAO/GenericDAO.php +++ b/src/DAO/GenericDAO.php @@ -1,7 +1,7 @@ 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 { + $dbh = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )); + $dbh->exec($script); + } catch (PDOException $e) { + echo ("DB ERROR: ". $e->getMessage()); } } } diff --git a/src/DAO/IGenericDAO.php b/src/DAO/IGenericDAO.php index 44d6cb7..f8cbce4 100644 --- a/src/DAO/IGenericDAO.php +++ b/src/DAO/IGenericDAO.php @@ -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. */ - public function generate(IDataBaseCreator $creator, $create = false); + public function geraScriptDeCriacaoDoBancoDeDados(IDataBaseCreator $creator); + + /** + * Cria banco de dados + */ + public function criaBancoDeDados(IDataBaseCreator $creator); } diff --git a/src/Services/IDataBaseCreator.php b/src/Services/IDatabaseCreator.php similarity index 91% rename from src/Services/IDataBaseCreator.php rename to src/Services/IDatabaseCreator.php index a231fcf..8a499ab 100644 --- a/src/Services/IDataBaseCreator.php +++ b/src/Services/IDatabaseCreator.php @@ -2,7 +2,7 @@ namespace PseudoORM\Services; -interface IDataBaseCreator +interface IDatabaseCreator { /** diff --git a/src/Services/PostgreSQLDataBaseCreator.php b/src/Services/PostgreSQLDatabaseCreator.php similarity index 96% rename from src/Services/PostgreSQLDataBaseCreator.php rename to src/Services/PostgreSQLDatabaseCreator.php index 028b667..a8b8628 100644 --- a/src/Services/PostgreSQLDataBaseCreator.php +++ b/src/Services/PostgreSQLDatabaseCreator.php @@ -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 { protected $tableName; From 5ab5ae57f313f3edad55a3f1c4b904e34d2b0a89 Mon Sep 17 00:00:00 2001 From: evandro Date: Sun, 18 Jun 2017 13:43:01 -0300 Subject: [PATCH 6/8] Marcando como @todo para remover isso mais pra frente --- test/functional/check-environment.phpt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/functional/check-environment.phpt b/test/functional/check-environment.phpt index 422f16f..031c1b1 100644 --- a/test/functional/check-environment.phpt +++ b/test/functional/check-environment.phpt @@ -11,6 +11,10 @@ define("DB_PORT", 5432); define("DB_NAME", 'pseudoorm'); define("SCHEMA", ''); define('ENCODING', "SET NAMES 'utf8';"); + +/** + * @todo Remover constantes + */ define("DB_DSN", "pgsql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME.";"); define("SHOW_SQL_ERROR", PDO::ERRMODE_EXCEPTION); From 0c83bb47aa4fa5d5385cb5a9284a6c84ea326a47 Mon Sep 17 00:00:00 2001 From: Evandro Mohr Date: Sun, 18 Jun 2017 13:32:51 -0300 Subject: [PATCH 7/8] Update check-environment.phpt --- test/functional/check-environment.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/check-environment.phpt b/test/functional/check-environment.phpt index 031c1b1..9919a86 100644 --- a/test/functional/check-environment.phpt +++ b/test/functional/check-environment.phpt @@ -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(); From 949e131e1dd7b5093cd5294184845c3eeb64e800 Mon Sep 17 00:00:00 2001 From: Evandro Mohr Date: Sun, 18 Jun 2017 13:42:06 -0300 Subject: [PATCH 8/8] Update check-environment.phpt --- test/functional/check-environment.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/check-environment.phpt b/test/functional/check-environment.phpt index 9919a86..d7ba389 100644 --- a/test/functional/check-environment.phpt +++ b/test/functional/check-environment.phpt @@ -20,7 +20,7 @@ define("SHOW_SQL_ERROR", PDO::ERRMODE_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'; @@ -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->criaBancoDeDados(new PostgreSQLDataBaseCreator()); +$dao->criaBancoDeDados(new PostgreSQLDatabaseCreator()); // Realizar operações básicas $usuario = $dao->create();