Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- }
Add Comment
Please, Sign In to add comment