Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

init 5.8KB

8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Yii Application Initialization Tool
  5. *
  6. * In order to run in non-interactive mode:
  7. *
  8. * init --env=Development --overwrite=n
  9. *
  10. * @author Alexander Makarov <sam@rmcreative.ru>
  11. *
  12. * @link http://www.yiiframework.com/
  13. * @copyright Copyright (c) 2008 Yii Software LLC
  14. * @license http://www.yiiframework.com/license/
  15. */
  16. if (!extension_loaded('openssl')) {
  17. die('The OpenSSL PHP extension is required by Yii2.');
  18. }
  19. $params = getParams();
  20. $root = str_replace('\\', '/', __DIR__);
  21. $envs = require("$root/environments/index.php");
  22. $envNames = array_keys($envs);
  23. echo "Yii Application Initialization Tool v1.0\n\n";
  24. $envName = null;
  25. if (empty($params['env']) || $params['env'] === '1') {
  26. echo "Which environment do you want the application to be initialized in?\n\n";
  27. foreach ($envNames as $i => $name) {
  28. echo " [$i] $name\n";
  29. }
  30. echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
  31. $answer = trim(fgets(STDIN));
  32. if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
  33. echo "\n Quit initialization.\n";
  34. exit(0);
  35. }
  36. if (isset($envNames[$answer])) {
  37. $envName = $envNames[$answer];
  38. }
  39. } else {
  40. $envName = $params['env'];
  41. }
  42. if (!in_array($envName, $envNames)) {
  43. $envsList = implode(', ', $envNames);
  44. echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
  45. exit(2);
  46. }
  47. $env = $envs[$envName];
  48. if (empty($params['env'])) {
  49. echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
  50. $answer = trim(fgets(STDIN));
  51. if (strncasecmp($answer, 'y', 1)) {
  52. echo "\n Quit initialization.\n";
  53. exit(0);
  54. }
  55. }
  56. echo "\n Start initialization ...\n\n";
  57. $files = getFileList("$root/environments/{$env['path']}");
  58. $all = false;
  59. foreach ($files as $file) {
  60. if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all, $params)) {
  61. break;
  62. }
  63. }
  64. $callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable'];
  65. foreach ($callbacks as $callback) {
  66. if (!empty($env[$callback])) {
  67. $callback($root, $env[$callback]);
  68. }
  69. }
  70. echo "\n ... initialization completed.\n\n";
  71. function getFileList($root, $basePath = '')
  72. {
  73. $files = [];
  74. $handle = opendir($root);
  75. while (($path = readdir($handle)) !== false) {
  76. if ($path === '.svn' || $path === '.' || $path === '..') {
  77. continue;
  78. }
  79. $fullPath = "$root/$path";
  80. $relativePath = $basePath === '' ? $path : "$basePath/$path";
  81. if (is_dir($fullPath)) {
  82. $files = array_merge($files, getFileList($fullPath, $relativePath));
  83. } else {
  84. $files[] = $relativePath;
  85. }
  86. }
  87. closedir($handle);
  88. return $files;
  89. }
  90. function copyFile($root, $source, $target, &$all, $params)
  91. {
  92. if (!is_file($root . '/' . $source)) {
  93. echo " skip $target ($source not exist)\n";
  94. return true;
  95. }
  96. if (is_file($root . '/' . $target)) {
  97. if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
  98. echo " unchanged $target\n";
  99. return true;
  100. }
  101. if ($all) {
  102. echo " overwrite $target\n";
  103. } else {
  104. echo " exist $target\n";
  105. echo " ...overwrite? [Yes|No|All|Quit] ";
  106. $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
  107. if (!strncasecmp($answer, 'q', 1)) {
  108. return false;
  109. } else {
  110. if (!strncasecmp($answer, 'y', 1)) {
  111. echo " overwrite $target\n";
  112. } else {
  113. if (!strncasecmp($answer, 'a', 1)) {
  114. echo " overwrite $target\n";
  115. $all = true;
  116. } else {
  117. echo " skip $target\n";
  118. return true;
  119. }
  120. }
  121. }
  122. }
  123. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  124. return true;
  125. }
  126. echo " generate $target\n";
  127. @mkdir(dirname($root . '/' . $target), 0777, true);
  128. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  129. return true;
  130. }
  131. function getParams()
  132. {
  133. $rawParams = [];
  134. if (isset($_SERVER['argv'])) {
  135. $rawParams = $_SERVER['argv'];
  136. array_shift($rawParams);
  137. }
  138. $params = [];
  139. foreach ($rawParams as $param) {
  140. if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
  141. $name = $matches[1];
  142. $params[$name] = isset($matches[3]) ? $matches[3] : true;
  143. } else {
  144. $params[] = $param;
  145. }
  146. }
  147. return $params;
  148. }
  149. function setWritable($root, $paths)
  150. {
  151. foreach ($paths as $writable) {
  152. echo " chmod 0777 $writable\n";
  153. @chmod("$root/$writable", 0777);
  154. }
  155. }
  156. function setExecutable($root, $paths)
  157. {
  158. foreach ($paths as $executable) {
  159. echo " chmod 0755 $executable\n";
  160. @chmod("$root/$executable", 0755);
  161. }
  162. }
  163. function setCookieValidationKey($root, $paths)
  164. {
  165. foreach ($paths as $file) {
  166. echo " generate cookie validation key in $file\n";
  167. $file = $root . '/' . $file;
  168. $length = 32;
  169. $bytes = openssl_random_pseudo_bytes($length);
  170. $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
  171. $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
  172. file_put_contents($file, $content);
  173. }
  174. }
  175. function createSymlink($links)
  176. {
  177. foreach ($links as $link => $target) {
  178. echo " symlink $target as $link\n";
  179. if (!is_link($link)) {
  180. symlink($target, $link);
  181. }
  182. }
  183. }