scaffolder.class.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Shibboleth;
  3. /**
  4. * Scaffolder. Genereate code templates from the database layout.
  5. * See /template/ for the code being generated
  6. *
  7. * @license see /license.txt
  8. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  9. */
  10. class Scaffolder
  11. {
  12. /**
  13. *
  14. * @staticvar boolean $result
  15. * @return Scaffolder
  16. */
  17. public static function instance()
  18. {
  19. static $result = false;
  20. if (empty($result))
  21. {
  22. $result = new self();
  23. }
  24. return $result;
  25. }
  26. public function scaffold($table_name, $class_name = '', $prefix = '_')
  27. {
  28. $db_name = Database :: get_main_database();
  29. $sql = "SELECT * FROM `$db_name`.`$table_name` LIMIT 1";
  30. $fields = array();
  31. $unique_fields = array();
  32. $rs = Database::query($sql, null, __FILE__);
  33. while ($field = mysql_fetch_field($rs))
  34. {
  35. $fields[] = $field;
  36. if ($field->primary_key)
  37. {
  38. /**
  39. * Could move that to an array to support multiple keys
  40. */
  41. $id_name = $field->name;
  42. }
  43. if ($field->unique_key | $field->primary_key)
  44. {
  45. $keys[] = $field->name;
  46. }
  47. }
  48. $name = $table_name;
  49. $class_name = ucfirst($table_name);
  50. ob_start();
  51. include dirname(__FILE__) . '/template/model.php';
  52. $result = ob_get_clean();
  53. return $result;
  54. }
  55. }