init 8.6 KB

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