Created
June 2, 2012 19:33
Revisions
-
diguinhorocks revised this gist
Jun 19, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ <?php class MysqlMapper{ protected static $instance = null; protected $pdo; -
diguinhorocks revised this gist
Jun 2, 2012 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ <?php include('MysqlMapper.php'); include('ModelGen.php'); $gen = new ModelGen(); $gen->setHost('localhost'); -
diguinhorocks revised this gist
Jun 2, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ public function setDatabase($database){ } public function setHost($host){ parent::HOST = $host; } public function setUser($user){ -
diguinhorocks revised this gist
Jun 2, 2012 . 3 changed files with 30 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,12 +6,14 @@ class ModelGen extends MysqlMapper{ protected $model = null; protected $extends = 'extends MysqlActiveRecord'; protected $implements = ''; protected $mapper = null; const PATH = ''; public function __construct(){ $this->mapper = parent::getInstance(); } @@ -24,10 +26,25 @@ public function setDatabase($database){ parent::DATABASE = $database; } public function setHost($host){ parent:HOST = $host; } public function setUser($user){ parent::USER = $user; } public function setPassword($password){ parent::PASS = $password; } public function setPath($path){ self::PATH = $path; } protected function createModelHeader(){ $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . ' '.$this->extends . ' '.$this->implements . "{ " . $this->printEndOfLine(2) ; } protected function createModelFooter(){ @@ -86,7 +103,7 @@ public function generate(){ } protected function writeInFile($title, $content){ $fp = fopen(PATH. strtolower($title) .".php", "w"); $write = fwrite($fp, $content); fclose($fp); This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,12 +5,13 @@ class MysqlTableMapper{ protected static $instance = null; protected $pdo; const HOST = 'localhost'; const DATABASE = ''; const USER = ''; const PASS = ''; public function __construct(){ $this->pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', HOST, DATABASE), USER, PASS); } public static function getInstance(){ This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,12 @@ <?php $gen = new ModelGen(); $gen->setHost('localhost'); $gen->setUser('user'); $gen->setPassword('pass'); $gen->setDatabase('test'); $gen->setPathToModel('path/to/models'); $gen->generate(); -
diguinhorocks revised this gist
Jun 2, 2012 . 3 changed files with 11 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,11 @@ public function __construct(){ public function setModel($model){ $this->modelName = $model; } public function setDatabase($database){ parent::DATABASE = $database; } protected function createModelHeader(){ This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,8 +5,12 @@ class MysqlTableMapper{ protected static $instance = null; protected $pdo; const DATABASE = ''; const USER = ''; const PASS = ''; public function __construct(){ $this->pdo = new PDO(sprintf('mysql:host=%s;dbname=%s', 'localhost', DATABASE), USER, PASS); } public static function getInstance(){ This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ <?php $gen = new ModelGen(); $gen->setDatabase('test'); $gen->generate(); -
diguinhorocks created this gist
Jun 2, 2012 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,141 @@ <?php class ModelGen extends MysqlMapper{ protected $modelName; protected $model = null; protected $extends = ' extends MysqlActiveRecord'; protected $implements = ''; protected $mapper = null; public function __construct(){ $this->mapper = parent::getInstance(); } public function setModel($model){ $this->modelName = $model; } protected function createModelHeader(){ $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . $this->extends . $this->implements . "{ " . $this->printEndOfLine(2) ; } protected function createModelFooter(){ $this->model .= $this->printEndOfLine(1). " }". $this->printEndOfLine(2) . "?>"; } protected function printEndOfLine($qtd){ $lb = ""; for($i = 0; $i < $qtd; $i++): $lb .= "\n"; endfor; return $lb; } protected function printTab($size){ $tab = ""; for($i = 0; $i < $size; $i++): $tab .= "\t"; endfor; return $tab; } public function generate(){ foreach($this->mapper->showTables() as $table): $this->setModel($table); $this->createModelHeader(); $this->getAttributes(); $this->printEndOfLine(2); $this->getConstructor(); $this->printEndOfLine(2); $this->getMagicSetter(); $this->printEndOfLine(2); $this->getMagicGetter(); $this->printEndOfLine(2); $this->createModelFooter(); $this->writeInFile($this->modelName, $this->model); $this->model = ""; endforeach; } protected function writeInFile($title, $content){ $fp = fopen(MODEL. strtolower($title) .".php", "w"); $write = fwrite($fp, $content); fclose($fp); } protected function getAttributes(){ $columns = $this->mapper->mapColumns($this->modelName); foreach($columns as $data): $this->model .= $this->printTab(1)." protected $".$data['Field'].";" . $this->printEndOfLine(1); endforeach; } protected function tableToClassName($name){ $isUnderLined = preg_split("/\_/", $name); $modelName = ""; if($isUnderLined): foreach($isUnderLined as $pieces): $modelName .= ucfirst(strtolower($pieces)); endforeach; else: $modelName = ucfirst(strtolower($name)); endif; return $modelName; } protected function getConstructor(){ $this->model .= <<<CONSTRUCTOR public function __construct() { parent::__construct(\$this); } CONSTRUCTOR; } protected function getMagicSetter(){ $this->model .= <<<SETTER public function __set(\$name, \$value) { \$this->\$name = \$value; } SETTER; } protected function getMagicGetter(){ $this->model .= <<<GETTER public function __get(\$name) { return \$this->\$name; } GETTER; } } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ <?php class MysqlTableMapper{ protected static $instance = null; protected $pdo; public function __construct(){ $this->pdo = Registry::getInstance()->get('connection'); } public static function getInstance(){ if(self::$instance == null): self::$instance = new MysqlTableMapper(); return self::$instance; else: return self::$instance; endif; } public function mapColumns($table){ $stm = $this->pdo->prepare('SHOW COLUMNS FROM '.$table.' FROM '.DATABASE); $stm->setFetchMode(PDO::FETCH_ASSOC); $stm->execute(); return $stm->fetchAll(); } public function showTables(){ $stm = $this->pdo->prepare('SHOW TABLES FROM '.DATABASE); $stm->setFetchMode(PDO::FETCH_ASSOC); $stm->execute(); $tables = array(); foreach($stm->fetchAll() as $table): $tables[] = $table['Tables_in_'.DATABASE]; endforeach; return $tables; } } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ <?php $gen = new ModelGen(); $gen->generate();