init 10.0 KB

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