Guest User

Untitled

a guest
Oct 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php
  2.  
  3. class ModelGen extends MysqlMapper{
  4.  
  5. protected $modelName;
  6.  
  7. protected $model = null;
  8.  
  9. protected $extends = 'extends MysqlActiveRecord';
  10.  
  11. protected $implements = '';
  12.  
  13. protected $mapper = null;
  14.  
  15. const PATH = '';
  16.  
  17. public function __construct(){
  18. $this->mapper = parent::getInstance();
  19. }
  20.  
  21. public function setModel($model){
  22. $this->modelName = $model;
  23. }
  24.  
  25. public function setDatabase($database){
  26. parent::DATABASE = $database;
  27. }
  28.  
  29. public function setHost($host){
  30. parent::HOST = $host;
  31. }
  32.  
  33. public function setUser($user){
  34. parent::USER = $user;
  35. }
  36.  
  37. public function setPassword($password){
  38. parent::PASS = $password;
  39. }
  40.  
  41. public function setPath($path){
  42. self::PATH = $path;
  43. }
  44.  
  45. protected function createModelHeader(){
  46.  
  47. $this->model .= "<?php ". $this->printEndOfLine(2) ." class ".($this->tableToClassName($this->modelName)) . ' '.$this->extends . ' '.$this->implements . "{ " . $this->printEndOfLine(2) ;
  48.  
  49. }
  50. protected function createModelFooter(){
  51. $this->model .= $this->printEndOfLine(1). " }". $this->printEndOfLine(2) . "?>";
  52. }
  53.  
  54. protected function printEndOfLine($qtd){
  55. $lb = "";
  56. for($i = 0; $i < $qtd; $i++):
  57. $lb .= "\n";
  58. endfor;
  59.  
  60. return $lb;
  61. }
  62.  
  63. protected function printTab($size){
  64. $tab = "";
  65. for($i = 0; $i < $size; $i++):
  66. $tab .= "\t";
  67. endfor;
  68.  
  69. return $tab;
  70. }
  71.  
  72.  
  73. public function generate(){
  74. foreach($this->mapper->showTables() as $table):
  75.  
  76. $this->setModel($table);
  77.  
  78. $this->createModelHeader();
  79.  
  80. $this->getAttributes();
  81.  
  82. $this->printEndOfLine(2);
  83.  
  84. $this->getConstructor();
  85.  
  86. $this->printEndOfLine(2);
  87.  
  88. $this->getMagicSetter();
  89.  
  90. $this->printEndOfLine(2);
  91.  
  92. $this->getMagicGetter();
  93.  
  94. $this->printEndOfLine(2);
  95.  
  96. $this->createModelFooter();
  97.  
  98. $this->writeInFile($this->modelName, $this->model);
  99.  
  100. $this->model = "";
  101. endforeach;
  102.  
  103. }
  104.  
  105. protected function writeInFile($title, $content){
  106. $fp = fopen(PATH. strtolower($title) .".php", "w");
  107. $write = fwrite($fp, $content);
  108.  
  109. fclose($fp);
  110. }
  111.  
  112. protected function getAttributes(){
  113.  
  114. $columns = $this->mapper->mapColumns($this->modelName);
  115. foreach($columns as $data):
  116. $this->model .= $this->printTab(1)." protected $".$data['Field'].";" . $this->printEndOfLine(1);
  117. endforeach;
  118. }
  119.  
  120. protected function tableToClassName($name){
  121.  
  122. $isUnderLined = preg_split("/\_/", $name);
  123. $modelName = "";
  124.  
  125. if($isUnderLined):
  126. foreach($isUnderLined as $pieces):
  127.  
  128. $modelName .= ucfirst(strtolower($pieces));
  129. endforeach;
  130. else:
  131. $modelName = ucfirst(strtolower($name));
  132. endif;
  133.  
  134. return $modelName;
  135. }
  136.  
  137. protected function getConstructor(){
  138. $this->model .= <<<CONSTRUCTOR
  139.  
  140. public function __construct() {
  141. parent::__construct(\$this);
  142. }
  143. CONSTRUCTOR;
  144. }
  145.  
  146. protected function getMagicSetter(){
  147. $this->model .= <<<SETTER
  148.  
  149. public function __set(\$name, \$value) {
  150. \$this->\$name = \$value;
  151. }
  152. SETTER;
  153. }
  154.  
  155. protected function getMagicGetter(){
  156. $this->model .= <<<GETTER
  157.  
  158. public function __get(\$name) {
  159. return \$this->\$name;
  160. }
  161. GETTER;
  162. }
  163. }
Add Comment
Please, Sign In to add comment