1
0

init 8.6 KB

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