PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/setup.php

https://bitbucket.org/ekowabaka/wyf
PHP | 175 lines | 134 code | 16 blank | 25 comment | 25 complexity | e7551a3cff3b3311357262fa7236e626 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env php
  2. <?php
  3. echo <<< WELCOME
  4. Welcome to the WYF Framework
  5. ============================
  6. This setup guide would help you get up and running with the WYF framework
  7. as fast as possible.
  8. WELCOME;
  9. $name = get_response("What is the name of your application", null, null, true);
  10. $host = get_response("Where is your application's database hosted", 'localhost', null, true);
  11. $port = get_response("What is the port of this database", '5432', null, true);
  12. $username = get_response("What is the database username", null, null, true);
  13. $password = get_response("What is the password for the database");
  14. $database = get_response("What is the name of your application's database (please ensure that the database exists)", null, null, true);
  15. $home = get_response("Where is your application residing", getcwd(), null, null, true) . "/";
  16. $prefix = get_response("What is the prefix of your application (Enter 'no prefix' if you do not want a prefix)", basename($home));
  17. echo "\nSetting up the application ...\n";
  18. mkdir2($home . 'app');
  19. mkdir2($home . 'app/cache');
  20. mkdir2($home . 'app/cache/code');
  21. mkdir2($home . 'app/cache/menus');
  22. mkdir2($home . 'app/cache/template_compile');
  23. mkdir2($home . 'app/logs');
  24. mkdir2($home . 'app/modules');
  25. mkdir2($home . 'app/modules/system');
  26. mkdir2($home . 'app/temp');
  27. mkdir2($home . 'app/themes');
  28. mkdir2($home . 'app/uploads');
  29. echo shell_exec("cp -r lib/setup/factory/* app");
  30. echo shell_exec("cp -r lib/setup/htaccess .htaccess");
  31. $system = <<< SYSTEM
  32. <?php
  33. \$redirect_path = "lib/modules/system";
  34. \$package_name = "System";
  35. \$package_path = "system";
  36. \$package_schema = "system";
  37. SYSTEM;
  38. file_put_contents($home . 'app/modules/system/package_redirect.php', $system);
  39. $index = <<< "INDEX"
  40. <?php
  41. /**
  42. * Generated by WYF setup script.
  43. *
  44. */
  45. require "lib/entry.php";
  46. INDEX;
  47. file_put_contents($home . 'index.php', $index);
  48. $config = <<< "CONFIG"
  49. <?php
  50. \$selected = "main";
  51. \$config = array(
  52. 'home' => "$home",
  53. 'prefix' => "/$prefix",
  54. 'name' => "$name",
  55. 'db' => array(
  56. "main" => array(
  57. 'driver' => 'postgresql',
  58. 'user' => '$username',
  59. 'host' => '$host',
  60. 'password' => '$password',
  61. 'name' => '$database',
  62. 'port' => '$port'
  63. )
  64. ),
  65. 'cache' => array(
  66. 'method' => 'file',
  67. 'models' => true
  68. ),
  69. 'audit_trails' => false,
  70. 'theme' => 'default'
  71. );
  72. CONFIG;
  73. file_put_contents($home . 'app/config.php', $config);
  74. file_put_contents($home . 'app/includes.php', "<?php\n");
  75. file_put_contents($home . 'app/bootstrap.php', "<?php\n");
  76. require "lib/wyf_bootstrap.php";
  77. echo "\nSetting up the database ...\n";
  78. Db::query(file_get_contents("lib/setup/schema.sql"));
  79. $username = get_response("Enter a name for the superuser account", 'super', null, true);
  80. $email = get_response('Provide your email address', null, null, true);
  81. Db::query("INSERT INTO system.roles(role_id, role_name) VALUES(1, 'Super User')");
  82. Db::query(
  83. sprintf(
  84. "INSERT INTO system.users
  85. (user_name, password, role_id, first_name, last_name, user_status, email)
  86. VALUES
  87. ('%s', '%s', 1, 'Super', 'User', 2, '%s')",
  88. Db::escape($username),
  89. Db::escape($username),
  90. Db::escape($email)
  91. )
  92. );
  93. echo "\nDone! Happy programming ;)\n\n";
  94. /**
  95. * A function for getting answers to questions from users interractively.
  96. * @param $question The question you want to ask
  97. * @param $answers An array of possible answers that this function should validate
  98. * @param $default The default answer this function should assume for the user.
  99. * @param $notNull Is the answer required
  100. */
  101. function get_response($question, $default=null, $answers=null, $notNull = false)
  102. {
  103. echo $question;
  104. if(is_array($answers))
  105. {
  106. if(count($answers) > 0) echo " (" . implode("/", $answers) . ")";
  107. }
  108. echo " [$default]: ";
  109. $response = str_replace(array("\n", "\r"),array("",""),fgets(STDIN));
  110. if($response == "" && $notNull === true && $default == '')
  111. {
  112. echo "A value is required.\n";
  113. return get_response($question, $answers, $default, $notNull);
  114. }
  115. else if($response == "" && $notNull === true && $default != '')
  116. {
  117. return $default;
  118. }
  119. else if($response == "")
  120. {
  121. return $default;
  122. }
  123. else
  124. {
  125. if(count($answers) == 0)
  126. {
  127. return $response;
  128. }
  129. foreach($answers as $answer)
  130. {
  131. if(strtolower($answer) == strtolower($response))
  132. {
  133. return strtolower($answer);
  134. }
  135. }
  136. echo "Please provide a valid answer.\n";
  137. return get_response($question, $answers, $default, $notNull);
  138. }
  139. }
  140. function mkdir2($path)
  141. {
  142. echo("Creating directory $path\n");
  143. if(!\is_writable(dirname($path)))
  144. {
  145. fputs(STDERR, "You do not have permissions to create the $path directory\n");
  146. $path = false;
  147. }
  148. else if(\is_dir($path))
  149. {
  150. echo ("Directory $path already exists. I will skip creating it ...\n");
  151. }
  152. else
  153. {
  154. mkdir($path);
  155. }
  156. return $path;
  157. }