init 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. if (!extension_loaded('openssl')) {
  11. die('The OpenSSL PHP extension is required by Yii2.');
  12. }
  13. $params = getParams();
  14. $root = str_replace('\\', '/', __DIR__);
  15. $envs = require "$root/environments/index.php";
  16. $envNames = array_keys($envs);
  17. echo "Yii Application Initialization Tool v1.0\n\n";
  18. $envName = null;
  19. if (empty($params['env']) || $params['env'] === '1') {
  20. echo "Which environment do you want the application to be initialized in?\n\n";
  21. foreach ($envNames as $i => $name) {
  22. echo " [$i] $name\n";
  23. }
  24. echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
  25. $answer = trim(fgets(STDIN));
  26. if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
  27. echo "\n Quit initialization.\n";
  28. exit(0);
  29. }
  30. if (isset($envNames[$answer])) {
  31. $envName = $envNames[$answer];
  32. }
  33. } else {
  34. $envName = $params['env'];
  35. }
  36. if (!in_array($envName, $envNames)) {
  37. $envsList = implode(', ', $envNames);
  38. echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
  39. exit(2);
  40. }
  41. $env = $envs[$envName];
  42. if (empty($params['env'])) {
  43. echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
  44. $answer = trim(fgets(STDIN));
  45. if (strncasecmp($answer, 'y', 1)) {
  46. echo "\n Quit initialization.\n";
  47. exit(0);
  48. }
  49. }
  50. echo "\n Start initialization ...\n\n";
  51. $files = getFileList("$root/environments/{$env['path']}");
  52. if (isset($env['skipFiles'])) {
  53. $skipFiles = $env['skipFiles'];
  54. array_walk($skipFiles, function(&$value) use($env, $root) { $value = "$root/$value"; });
  55. $files = array_diff($files, array_intersect_key($env['skipFiles'], array_filter($skipFiles, 'file_exists')));
  56. }
  57. $all = false;
  58. foreach ($files as $file) {
  59. if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all, $params)) {
  60. break;
  61. }
  62. }
  63. $callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable', 'createSymlink'];
  64. foreach ($callbacks as $callback) {
  65. if (!empty($env[$callback])) {
  66. $callback($root, $env[$callback]);
  67. }
  68. }
  69. echo "\n ... initialization completed.\n\n";
  70. function getFileList($root, $basePath = '')
  71. {
  72. $files = [];
  73. $handle = opendir($root);
  74. while (($path = readdir($handle)) !== false) {
  75. if ($path === '.git' || $path === '.svn' || $path === '.' || $path === '..') {
  76. continue;
  77. }
  78. $fullPath = "$root/$path";
  79. $relativePath = $basePath === '' ? $path : "$basePath/$path";
  80. if (is_dir($fullPath)) {
  81. $files = array_merge($files, getFileList($fullPath, $relativePath));
  82. } else {
  83. $files[] = $relativePath;
  84. }
  85. }
  86. closedir($handle);
  87. return $files;
  88. }
  89. function copyFile($root, $source, $target, &$all, $params)
  90. {
  91. if (!is_file($root . '/' . $source)) {
  92. echo " skip $target ($source not exist)\n";
  93. return true;
  94. }
  95. if (is_file($root . '/' . $target)) {
  96. if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
  97. echo " unchanged $target\n";
  98. return true;
  99. }
  100. if ($all) {
  101. echo " overwrite $target\n";
  102. } else {
  103. echo " exist $target\n";
  104. echo " ...overwrite? [Yes|No|All|Quit] ";
  105. $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
  106. if (!strncasecmp($answer, 'q', 1)) {
  107. return false;
  108. } else {
  109. if (!strncasecmp($answer, 'y', 1)) {
  110. echo " overwrite $target\n";
  111. } else {
  112. if (!strncasecmp($answer, 'a', 1)) {
  113. echo " overwrite $target\n";
  114. $all = true;
  115. } else {
  116. echo " skip $target\n";
  117. return true;
  118. }
  119. }
  120. }
  121. }
  122. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  123. return true;
  124. }
  125. echo " generate $target\n";
  126. @mkdir(dirname($root . '/' . $target), 0777, true);
  127. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  128. return true;
  129. }
  130. function getParams()
  131. {
  132. $rawParams = [];
  133. if (isset($_SERVER['argv'])) {
  134. $rawParams = $_SERVER['argv'];
  135. array_shift($rawParams);
  136. }
  137. $params = [];
  138. foreach ($rawParams as $param) {
  139. if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
  140. $name = $matches[1];
  141. $params[$name] = isset($matches[3]) ? $matches[3] : true;
  142. } else {
  143. $params[] = $param;
  144. }
  145. }
  146. return $params;
  147. }
  148. function setWritable($root, $paths)
  149. {
  150. foreach ($paths as $writable) {
  151. if (is_dir("$root/$writable")) {
  152. if (@chmod("$root/$writable", 0777)) {
  153. echo " chmod 0777 $writable\n";
  154. } else {
  155. printError("Operation chmod not permitted for directory $writable.");
  156. }
  157. } else {
  158. printError("Directory $writable does not exist.");
  159. }
  160. }
  161. }
  162. function setExecutable($root, $paths)
  163. {
  164. foreach ($paths as $executable) {
  165. if (file_exists("$root/$executable")) {
  166. if (@chmod("$root/$executable", 0755)) {
  167. echo " chmod 0755 $executable\n";
  168. } else {
  169. printError("Operation chmod not permitted for $executable.");
  170. }
  171. } else {
  172. printError("$executable does not exist.");
  173. }
  174. }
  175. }
  176. function setCookieValidationKey($root, $paths)
  177. {
  178. foreach ($paths as $file) {
  179. echo " generate cookie validation key in $file\n";
  180. $file = $root . '/' . $file;
  181. $length = 32;
  182. $bytes = openssl_random_pseudo_bytes($length);
  183. $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
  184. $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
  185. file_put_contents($file, $content);
  186. }
  187. }
  188. function createSymlink($root, $links)
  189. {
  190. foreach ($links as $link => $target) {
  191. //first removing folders to avoid errors if the folder already exists
  192. @rmdir($root . "/" . $link);
  193. //next removing existing symlink in order to update the target
  194. if (is_link($root . "/" . $link)) {
  195. @unlink($root . "/" . $link);
  196. }
  197. if (@symlink($root . "/" . $target, $root . "/" . $link)) {
  198. echo " symlink $root/$target $root/$link\n";
  199. } else {
  200. printError("Cannot create symlink $root/$target $root/$link.");
  201. }
  202. }
  203. }
  204. /**
  205. * Prints error message.
  206. * @param string $message message
  207. */
  208. function printError($message)
  209. {
  210. echo "\n " . formatMessage("Error. $message", ['fg-red']) . " \n";
  211. }
  212. /**
  213. * Returns true if the stream supports colorization. ANSI colors are disabled if not supported by the stream.
  214. *
  215. * - windows without ansicon
  216. * - not tty consoles
  217. *
  218. * @return boolean true if the stream supports ANSI colors, otherwise false.
  219. */
  220. function ansiColorsSupported()
  221. {
  222. return DIRECTORY_SEPARATOR === '\\'
  223. ? getenv('ANSICON') !== false || getenv('ConEmuANSI') === 'ON'
  224. : function_exists('posix_isatty') && @posix_isatty(STDOUT);
  225. }
  226. /**
  227. * Get ANSI code of style.
  228. * @param string $name style name
  229. * @return integer ANSI code of style.
  230. */
  231. function getStyleCode($name)
  232. {
  233. $styles = [
  234. 'bold' => 1,
  235. 'fg-black' => 30,
  236. 'fg-red' => 31,
  237. 'fg-green' => 32,
  238. 'fg-yellow' => 33,
  239. 'fg-blue' => 34,
  240. 'fg-magenta' => 35,
  241. 'fg-cyan' => 36,
  242. 'fg-white' => 37,
  243. 'bg-black' => 40,
  244. 'bg-red' => 41,
  245. 'bg-green' => 42,
  246. 'bg-yellow' => 43,
  247. 'bg-blue' => 44,
  248. 'bg-magenta' => 45,
  249. 'bg-cyan' => 46,
  250. 'bg-white' => 47,
  251. ];
  252. return $styles[$name];
  253. }
  254. /**
  255. * Formats message using styles if STDOUT supports it.
  256. * @param string $message message
  257. * @param string[] $styles styles
  258. * @return string formatted message.
  259. */
  260. function formatMessage($message, $styles)
  261. {
  262. if (empty($styles) || !ansiColorsSupported()) {
  263. return $message;
  264. }
  265. return sprintf("\x1b[%sm", implode(';', array_map('getStyleCode', $styles))) . $message . "\x1b[0m";
  266. }