PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ruckusing/generate.php

https://github.com/radicaldesigns/jaguar
PHP | 106 lines | 89 code | 9 blank | 8 comment | 6 complexity | e31e9eda3ca81c1655ce98ba40bf26fc MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. /*
  3. Generator for migrations.
  4. Usage: php generate.php <migration name>
  5. Call with no arguments to see usage info.
  6. */
  7. define('RUCKUSING_BASE', realpath(dirname(__FILE__)));
  8. define('RUCKUSING_WORKING_BASE', getcwd());
  9. $config_filename = RUCKUSING_WORKING_BASE . '/ruckusing.conf.php';
  10. if (file_exists($config_filename)) {
  11. $config = include $config_filename;
  12. } else {
  13. $config = include RUCKUSING_BASE . '/config/database.inc.php';
  14. }
  15. require RUCKUSING_BASE . '/config/config.inc.php';
  16. require RUCKUSING_BASE . '/lib/classes/util/class.Ruckusing_Logger.php';
  17. require RUCKUSING_BASE . '/lib/classes/util/class.Ruckusing_NamingUtil.php';
  18. require RUCKUSING_BASE . '/lib/classes/util/class.Ruckusing_MigratorUtil.php';
  19. require RUCKUSING_BASE . '/lib/classes/class.Ruckusing_FrameworkRunner.php';
  20. $args = parse_args($argv);
  21. $framework = new Ruckusing_FrameworkRunner($config, null);
  22. //input sanity check
  23. if(!is_array($args) || (is_array($args) && !array_key_exists('name', $args)) ) {
  24. print_help(true);
  25. }
  26. $migration_name = $args['name'];
  27. //clear any filesystem stats cache
  28. clearstatcache();
  29. //generate a complete migration file
  30. $next_version = Ruckusing_MigratorUtil::generate_timestamp();
  31. $class = Ruckusing_NamingUtil::camelcase($migration_name);
  32. $file_name = $next_version . '_' . $class . '.php';
  33. $migrations_dir = $framework->migrations_directory();
  34. $template_str = get_template($class);
  35. if(!is_dir($migrations_dir)) {
  36. printf("\n\tMigrations directory (%s doesn't exist, attempting to create.", $migrations_dir);
  37. if(mkdir($migrations_dir) === FALSE) {
  38. printf("\n\tUnable to create migrations directory at %s, check permissions?", $migrations_dir);
  39. } else {
  40. printf("\n\tCreated OK");
  41. }
  42. }
  43. //check to make sure our destination directory is writable
  44. if(!is_writable($migrations_dir)) {
  45. die_with_error("ERROR: migration directory '" . $migrations_dir . "' is not writable by the current user. Check permissions and try again.");
  46. }
  47. //write it out!
  48. $full_path = $migrations_dir . '/' . $file_name;
  49. $file_result = file_put_contents($full_path, $template_str);
  50. if($file_result === FALSE) {
  51. die_with_error("Error writing to migrations directory/file. Do you have sufficient privileges?");
  52. } else {
  53. echo "\n\tCreated migration: {$file_name}\n\n";
  54. }
  55. /*
  56. Parse command line arguments.
  57. */
  58. function parse_args($argv) {
  59. $num_args = count($argv);
  60. if($num_args < 2) {
  61. print_help(true);
  62. }
  63. $migration_name = $argv[1];
  64. return array('name' => $migration_name);
  65. }
  66. /*
  67. Print a usage scenario for this script.
  68. Optionally take a boolean on whether to immediately die or not.
  69. */
  70. function print_help($exit = false) {
  71. echo "\nusage: php generate.php <migration name>\n\n";
  72. echo "\tWhere <migration name> is a descriptive name of the migration, joined with underscores.\n";
  73. echo "\tExamples: add_index_to_users | create_users_table | remove_pending_users\n\n";
  74. if($exit) { exit; }
  75. }
  76. function die_with_error($str) {
  77. die("\n{$str}\n");
  78. }
  79. function get_template($klass) {
  80. $template = <<<TPL
  81. <?php\n
  82. class $klass extends Ruckusing_BaseMigration {\n\n\tpublic function up() {\n\n\t}//up()
  83. \n\tpublic function down() {\n\n\t}//down()
  84. }
  85. ?>
  86. TPL;
  87. return $template;
  88. }
  89. ?>