Skip to content

Instantly share code, notes, and snippets.

@diguinhorocks
Created June 2, 2012 19:33
Show Gist options
  • Save diguinhorocks/2859690 to your computer and use it in GitHub Desktop.
Save diguinhorocks/2859690 to your computer and use it in GitHub Desktop.
Library which generates some model stuff (attributes, magic getter, setter) for any application. Make sure the path to models is writable.
<?php
include('MysqlMapper.php');
include('ModelGen.php');
$gen = new ModelGen();
$gen->setHost('localhost');
$gen->setUser('user');
$gen->setPassword('pass');
$gen->setDatabase('test');
$gen->setPathToModel('path/to/models');
$gen->generate();
<?php
class ModelGen extends MysqlMapper{
protected $modelName;
protected $model = null;
protected $extends = 'extends MysqlActiveRecord';
protected $implements = '';
protected $mapper = null;
const PATH = '';
public function __construct(){
$this->mapper = parent::getInstance();
}
public function setModel($model){
$this->modelName = $model;
}
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(){
$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(PATH. 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;
}
}
<?php
class MysqlMapper{
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(){
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment