init 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $noLoadExtension = [];
  17. extension_loaded('openssl') || $noLoadExtension[] = 'openssl';
  18. extension_loaded('bcmath') || $noLoadExtension[] = 'bcmath';
  19. extension_loaded('curl') || $noLoadExtension[] = 'curl';
  20. extension_loaded('gd') || $noLoadExtension[] = 'gd';
  21. extension_loaded('mbstring') || $noLoadExtension[] = 'mbstring';
  22. extension_loaded('mysqli') || $noLoadExtension[] = 'mysqli';
  23. extension_loaded('pcre') || $noLoadExtension[] = 'pcre';
  24. extension_loaded('PDO') || $noLoadExtension[] = 'PDO';
  25. extension_loaded('pdo_mysql') || $noLoadExtension[] = 'pdo_mysql';
  26. extension_loaded('pdo_sqlite') || $noLoadExtension[] = 'pdo_sqlite';
  27. extension_loaded('Reflection') || $noLoadExtension[] = 'Reflection';
  28. extension_loaded('xmlwriter') || $noLoadExtension[] = 'xmlwriter';
  29. extension_loaded('zip') || $noLoadExtension[] = 'zip';
  30. // extension_loaded('imap') || $noLoadExtension[] = 'imap';
  31. // extension_loaded('soap') || $noLoadExtension[] = 'soap';
  32. //extension_loaded('iconv') || $noLoadExtension[] = 'iconv';
  33. //extension_loaded('hash') || $noLoadExtension[] = 'hash';
  34. //extension_loaded('mcrypt') || $noLoadExtension[] = 'mcrypt';
  35. //extension_loaded('OAuth') || $noLoadExtension[] = 'OAuth';
  36. if (!empty($noLoadExtension)) {
  37. $noLoadExtensionStr = implode(',', $noLoadExtension);
  38. die("PHP extension [ $noLoadExtensionStr ] is required by Fecshop. you must install this php extension \n");
  39. }
  40. $params = getParams();
  41. $root = str_replace('\\', '/', __DIR__);
  42. $envs = require("$root/environments/index.php");
  43. $envNames = array_keys($envs);
  44. echo "Yii Application Initialization Tool v1.0\n\n";
  45. $envName = null;
  46. if (empty($params['env']) || $params['env'] === '1') {
  47. echo "Which environment do you want the application to be initialized in?\n\n";
  48. foreach ($envNames as $i => $name) {
  49. echo " [$i] $name\n";
  50. }
  51. echo "\n Your choice 0" ;
  52. $answer = "0"; //trim(fgets(STDIN));
  53. if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
  54. echo "\n Quit initialization.\n";
  55. exit(0);
  56. }
  57. if (isset($envNames[$answer])) {
  58. $envName = $envNames[$answer];
  59. }
  60. } else {
  61. $envName = $params['env'];
  62. }
  63. if (!in_array($envName, $envNames)) {
  64. $envsList = implode(', ', $envNames);
  65. echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
  66. exit(2);
  67. }
  68. $env = $envs[$envName];
  69. if (empty($params['env'])) {
  70. echo "\n Initialize the application under '{$envNames[$answer]}' environment? yes!"; // [yes|no]
  71. $answer = 'yes'; //trim(fgets(STDIN));
  72. if (strncasecmp($answer, 'y', 1)) {
  73. echo "\n Quit initialization.\n";
  74. exit(0);
  75. }
  76. }
  77. echo "\n Start initialization ...\n\n";
  78. $files = getFileList("$root/environments/{$env['path']}");
  79. if (isset($env['skipFiles'])) {
  80. $skipFiles = $env['skipFiles'];
  81. array_walk($skipFiles, function(&$value) use($env, $root) { $value = "$root/$value"; });
  82. $files = array_diff($files, array_intersect_key($env['skipFiles'], array_filter($skipFiles, 'file_exists')));
  83. }
  84. $all = false;
  85. foreach ($files as $file) {
  86. if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all, $params)) {
  87. break;
  88. }
  89. }
  90. $callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable', 'createSymlink'];
  91. foreach ($callbacks as $callback) {
  92. if (!empty($env[$callback])) {
  93. $callback($root, $env[$callback]);
  94. }
  95. }
  96. echo "\n ... initialization completed.\n\n";
  97. function getFileList($root, $basePath = '')
  98. {
  99. $files = [];
  100. $handle = opendir($root);
  101. while (($path = readdir($handle)) !== false) {
  102. if ($path === '.git' || $path === '.svn' || $path === '.' || $path === '..') {
  103. continue;
  104. }
  105. $fullPath = "$root/$path";
  106. $relativePath = $basePath === '' ? $path : "$basePath/$path";
  107. if (is_dir($fullPath)) {
  108. $files = array_merge($files, getFileList($fullPath, $relativePath));
  109. } else {
  110. $files[] = $relativePath;
  111. }
  112. }
  113. closedir($handle);
  114. return $files;
  115. }
  116. function copyFile($root, $source, $target, &$all, $params)
  117. {
  118. if (!is_file($root . '/' . $source)) {
  119. echo " skip $target ($source not exist)\n";
  120. return true;
  121. }
  122. if (is_file($root . '/' . $target)) {
  123. if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
  124. echo " unchanged $target\n";
  125. return true;
  126. }
  127. if ($all) {
  128. echo " overwrite $target\n";
  129. } else {
  130. echo " exist $target\n";
  131. echo " ...overwrite? [Yes|No|All|Quit] ";
  132. $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
  133. if (!strncasecmp($answer, 'q', 1)) {
  134. return false;
  135. } else {
  136. if (!strncasecmp($answer, 'y', 1)) {
  137. echo " overwrite $target\n";
  138. } else {
  139. if (!strncasecmp($answer, 'a', 1)) {
  140. echo " overwrite $target\n";
  141. $all = true;
  142. } else {
  143. echo " skip $target\n";
  144. return true;
  145. }
  146. }
  147. }
  148. }
  149. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  150. return true;
  151. }
  152. echo " generate $target\n";
  153. @mkdir(dirname($root . '/' . $target), 0777, true);
  154. file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
  155. return true;
  156. }
  157. function getParams()
  158. {
  159. $rawParams = [];
  160. if (isset($_SERVER['argv'])) {
  161. $rawParams = $_SERVER['argv'];
  162. array_shift($rawParams);
  163. }
  164. $params = [];
  165. foreach ($rawParams as $param) {
  166. if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
  167. $name = $matches[1];
  168. $params[$name] = isset($matches[3]) ? $matches[3] : true;
  169. } else {
  170. $params[] = $param;
  171. }
  172. }
  173. return $params;
  174. }
  175. function setWritable($root, $paths)
  176. {
  177. foreach ($paths as $writable) {
  178. if (is_dir("$root/$writable")) {
  179. echo " chmod 0777 $writable\n";
  180. @chmod("$root/$writable", 0777);
  181. } else {
  182. if (substr($writable,(strlen($writable)-4)) == '.xml') {
  183. echo " chmod 0777 $writable\n";
  184. @chmod("$root/$writable", 0777);
  185. } else if ($writable == 'common/config/main-local.php'){
  186. echo " chmod 0777 $writable\n";
  187. @chmod("$root/$writable", 0777);
  188. } else {
  189. echo "\n Error. Directory $writable does not exist. \n";
  190. }
  191. }
  192. }
  193. }
  194. function setExecutable($root, $paths)
  195. {
  196. foreach ($paths as $executable) {
  197. echo " chmod 0755 $executable\n";
  198. @chmod("$root/$executable", 0755);
  199. }
  200. }
  201. function setCookieValidationKey($root, $paths)
  202. {
  203. foreach ($paths as $file) {
  204. echo " generate cookie validation key in $file\n";
  205. $file = $root . '/' . $file;
  206. $length = 32;
  207. $bytes = openssl_random_pseudo_bytes($length);
  208. $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
  209. $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
  210. file_put_contents($file, $content);
  211. }
  212. }
  213. function createSymlink($root, $links) {
  214. foreach ($links as $link => $target) {
  215. echo " symlink " . $root . "/" . $target . " " . $root . "/" . $link . "\n";
  216. //first removing folders to avoid errors if the folder already exists
  217. @rmdir($root . "/" . $link);
  218. //next removing existing symlink in order to update the target
  219. if (is_link($root . "/" . $link)) {
  220. @unlink($root . "/" . $link);
  221. }
  222. @symlink($root . "/" . $target, $root . "/" . $link);
  223. }
  224. }