init 8.1 KB

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