PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/php/util/data_class_generator/data_class_generator.class.php

https://bitbucket.org/chamilo/chamilo-migration-dev/
PHP | 104 lines | 64 code | 17 blank | 23 comment | 2 complexity | e6a70e3d9688e243a6820e882a7dd2e6 MD5 | raw file
  1. <?php
  2. namespace migration;
  3. /**
  4. * Dataclass generator used to generate dataclasses with given properties
  5. * $Id: data_class_generator.class.php 221 2009-11-13 14:36:41Z vanpouckesven $
  6. * @package migration.settings
  7. * @author Sven Vanpoucke
  8. */
  9. class DataClassGenerator
  10. {
  11. private $template;
  12. private $database;
  13. private $classname;
  14. private $properties;
  15. private $package;
  16. private $description;
  17. private $author;
  18. /**
  19. * Constructor
  20. * @param string $database the database
  21. * @param string $classname the classname
  22. * @param array of strings $properties the properties
  23. * @param string $package the package
  24. * @param string $description the description
  25. * @param string $author, the author
  26. */
  27. function __construct($database, $classname, $properties, $package, $description, $author)
  28. {
  29. $this->template = new MyTemplate();
  30. $this->classname = $classname;
  31. $this->properties = $properties;
  32. $this->package = $package;
  33. $this->description = $description;
  34. $this->author = $author;
  35. $this->database = $database;
  36. $this->generate_data_class();
  37. echo ('dataclass ' . $this->classname . ' generated<br />');
  38. }
  39. /**
  40. * Generate a dataclass with the given info
  41. */
  42. function generate_data_class()
  43. {
  44. $this->classname = self :: to_camel_case($this->classname);
  45. $filename = dirname(__FILE__) . '/classes/' . $this->database . '/' . strtolower($this->classname) . '.class.php';
  46. $destination_dir = dirname($filename);
  47. if (! is_dir($destination_dir))
  48. mkdir($destination_dir, 0777, true);
  49. $file = fopen($filename, 'w+');
  50. if ($file)
  51. {
  52. $this->template->set_filenames(array('dataclass' => 'dataclass.template'));
  53. $property_names = array();
  54. $this->template->assign_vars(array('PACKAGE' => $this->package, 'DESCRIPTION' => $this->description,
  55. 'AUTHOR' => $this->author, 'CLASSNAME' => $this->classname));
  56. foreach ($this->properties as $property)
  57. {
  58. $property_const = 'PROPERTY_' . strtoupper($property);
  59. $property_names[] = 'self :: ' . $property_const;
  60. $this->template->assign_block_vars("CONSTS", array('PROPERTY_CONST' => $property_const,
  61. 'PROPERTY_NAME' => $property));
  62. $this->template->assign_block_vars("PROPERTY", array('PROPERTY_CONST' => $property_const,
  63. 'PROPERTY_NAME' => $property));
  64. }
  65. $this->template->assign_vars(array('DEFAULT_PROPERTY_NAMES' => implode(', ', $property_names)));
  66. $string = "<?php \n" . $this->template->pparse_return('dataclass') . "\n?>";
  67. fwrite($file, $string);
  68. fclose($file);
  69. }
  70. }
  71. /**
  72. * Removes underscores and replace it by camelcase
  73. * @param string $text old text
  74. * @param string $newtext converted text
  75. */
  76. static function to_camel_case($text)
  77. {
  78. $textsplit = split('_', $text);
  79. $newtext = '';
  80. foreach ($textsplit as $temp)
  81. {
  82. $newtext = $newtext . strtoupper(substr($temp, 0, 1)) . substr($temp, 1);
  83. }
  84. return $newtext;
  85. }
  86. }
  87. ?>