/index.php

https://github.com/thomascrepain/classCreator · PHP · 217 lines · 178 code · 17 blank · 22 comment · 11 complexity · e1e1f6335d586bcf37b7ff8013885682 MD5 · raw file

  1. <?php
  2. require_once 'settings.php';
  3. require_once 'dal/MySQLDatabase.php';
  4. require_once 'libs/Smarty.class.php';
  5. // connect to database
  6. $database = new MySQLDatabase(DATABASE_TYPE, DATABASE_HOSTNAME,
  7. DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_DATABASE, DATABASE_PORT);
  8. // get tables
  9. echo 'Getting tables from database... ';
  10. $tables = $database->getTables();
  11. echo "DONE \n";
  12. // for every table...
  13. foreach ($tables as $table) {
  14. generateClassesForTable($database, $table);
  15. }
  16. function generateClassesForTable($database, $table) {
  17. echo "Generating classes for $table... ";
  18. // get it's columns
  19. $records = $database->getRecords("SHOW COLUMNS FROM $table");
  20. // prepare vars
  21. $columns = array();
  22. $numberOfPrimaryKeys = 0;
  23. foreach ($records as $record) {
  24. $column = array();
  25. // make fieldname camel cased
  26. $column['fieldName'] = toCamelCase($record['Field']);
  27. $column['originalFieldName'] = $record['Field'];
  28. $column['type'] = getTypePerLanguage($record['Type']);
  29. $column['isNull'] = $record['Null'] === 'YES' ? true : false;
  30. $column['isPrimaryKey'] = $record['Key'] === "PRI" ? true : false;
  31. $column['isDefaultValue'] = $record['Default'];
  32. $column['extra'] = $record['Extra'];
  33. if ($column['isPrimaryKey']) {
  34. $columns['primaryKey'] = $column;
  35. $numberOfPrimaryKeys++;
  36. }
  37. else
  38. $columns[] = $column;
  39. }
  40. // generate the classes for every item in the directory
  41. // but only if the table has 1 primary key (we want to exclude junction tables)
  42. if ($numberOfPrimaryKeys == 1)
  43. generateEveryFileInDir('', $table, $columns);
  44. echo "DONE \n";
  45. }
  46. function generateEveryFileInDir($directory, $table, $columns) {
  47. // only run this function if the given directory is in fact a directory
  48. if (is_dir(TEMPLATE_DIRECTORY . '/' . $directory)) {
  49. // get the directory content
  50. $directoryContent = scandir(TEMPLATE_DIRECTORY . '/' . $directory);
  51. $directory = (empty($directory) ? '' : $directory . '/');
  52. // for ever item in the directory...
  53. foreach ($directoryContent as $item) {
  54. // ignore hidden template files (e.g. . and ..)
  55. if (in_array($item, explode("|", TEMPLATE_HIDDEN))) {
  56. // do nothing
  57. } else {
  58. // test if it's a directory on it's own
  59. if (is_dir(TEMPLATE_DIRECTORY . '/' . $directory . $item)) {
  60. // generate directory if not exists
  61. if (!file_exists('generated_classes/' . $directory))
  62. mkdir('generated_classes/' . $directory);
  63. // generate the files in that directory
  64. generateEveryFileInDir($directory . $item, $table, $columns);
  65. } else {
  66. // create output directory if not exists
  67. if (!file_exists('generated_classes/' . $directory))
  68. mkdir('generated_classes/' . $directory);
  69. // generate the files in this directory
  70. generateFile(TEMPLATE_DIRECTORY . '/' . $directory, $item, 'generated_classes/' . $directory . str_replace('%Classname%', toCamelCase($table, true), $item), $table, $columns);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. function generateFile($templateDir, $templateFileName, $generatedFile, $table, $columns) {
  77. // prepare template compiler
  78. $templateCompiler = new Smarty();
  79. $templateCompiler->setTemplateDir($templateDir);
  80. $templateCompiler->setCompileDir('compiled_templates/');
  81. $templateCompiler->setConfigDir('configs/');
  82. $templateCompiler->setCacheDir('cache/');
  83. // assign vars
  84. $templateCompiler->assign('className', toCamelCase($table, true));
  85. $templateCompiler->assign('tableName', $table);
  86. $templateCompiler->assign('fields', $columns);
  87. $templateCompiler->assign('authorName', AUTHOR_NAME);
  88. $templateCompiler->assign('authorEmail', AUTHOR_EMAIL);
  89. // fetch file content'
  90. $fileContent = $templateCompiler->fetch($templateFileName);
  91. // save to new file
  92. $file = fopen($generatedFile, 'w') or die("Couldn't create file: " . toCamelCase($table));
  93. fwrite($file, $fileContent);
  94. fclose($file);
  95. }
  96. /**
  97. * returns the extension for a filename.
  98. *
  99. * @return string The extension.
  100. * @param string $filename The full path of the file.
  101. */
  102. function getExtension($filename) {
  103. // init var
  104. $filename = (string) $filename;
  105. // get extension
  106. $parts = (array) explode('.', $filename);
  107. // count the parts
  108. $count = count($parts);
  109. // return the last part
  110. if ($count != 0)
  111. return $parts[$count - 1];
  112. // no extension
  113. return '';
  114. }
  115. function getTypePerLanguage($SQLtype) {
  116. // init vars
  117. $types = array();
  118. // Boolean
  119. if (preg_match('/bool/i', $SQLtype) || preg_match('/boolean/i', $SQLtype)) {
  120. $types = array('php' => 'boolean', 'as' => 'Boolean');
  121. }
  122. // Date
  123. if (preg_match('/date/i', $SQLtype)) {
  124. $types = array('php' => 'date', 'as' => 'Date');
  125. }
  126. // timestamp
  127. if (preg_match('/timestamp/i', $SQLtype)) {
  128. $types = array('php' => 'int', 'as' => 'Number');
  129. }
  130. // float
  131. if (preg_match('/float/i', $SQLtype)) {
  132. $types = array('php' => 'float', 'as' => 'Number');
  133. }
  134. // decimal
  135. if (preg_match('/decimal/i', $SQLtype)) {
  136. $types = array('php' => 'float', 'as' => 'Number');
  137. }
  138. // Integer
  139. if (preg_match('/int\(\d+\)/i', $SQLtype)) {
  140. $types = array('php' => 'int', 'as' => 'Number');
  141. }
  142. // String
  143. if (preg_match('/varchar\(\d+\)/i', $SQLtype)) {
  144. $types = array('php' => 'string', 'as' => 'String');
  145. }
  146. // return types
  147. return $types;
  148. }
  149. /**
  150. * Converts a string to camelcasing.
  151. *
  152. * @return string The resulted string
  153. * @param string $inputValue The string that should be camelcased
  154. * @param bool[optional] $firstCharUpperCase Should the first character be uppercase?
  155. * @param string[optional] $separator The separator between the words
  156. */
  157. function toCamelCase($inputValue, $firstCharUpperCase = false, $separator = '_') {
  158. // init var
  159. $returnValue = '';
  160. // fetch words
  161. $words = explode((string) $separator, (string) $inputValue);
  162. foreach ($words as $i => $word) {
  163. // skip empty words
  164. if ($word != '') {
  165. // make word lowercase
  166. $word = strtolower($word);
  167. // When it's not the first word and we shouldn't use lowercase for the first word convert first letter to uppercase
  168. if ($i != 0)
  169. $word = ucfirst($word);
  170. else if ($firstCharUpperCase)
  171. $word = ucfirst($word);
  172. // append the word to the return value
  173. $returnValue .= $word;
  174. }
  175. }
  176. // return resulting string
  177. return $returnValue;
  178. }
  179. ?>