Yii 2.0
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

218 líneas
6.6KB

  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 <[email protected]>
  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. echo " chmod 0777 $writable\n";
  159. @chmod("$root/$writable", 0777);
  160. } else {
  161. echo "\n Error. Directory $writable does not exist. \n";
  162. }
  163. }
  164. }
  165. function setExecutable($root, $paths)
  166. {
  167. foreach ($paths as $executable) {
  168. echo " chmod 0755 $executable\n";
  169. @chmod("$root/$executable", 0755);
  170. }
  171. }
  172. function setCookieValidationKey($root, $paths)
  173. {
  174. foreach ($paths as $file) {
  175. echo " generate cookie validation key in $file\n";
  176. $file = $root . '/' . $file;
  177. $length = 32;
  178. $bytes = openssl_random_pseudo_bytes($length);
  179. $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
  180. $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
  181. file_put_contents($file, $content);
  182. }
  183. }
  184. function createSymlink($root, $links) {
  185. foreach ($links as $link => $target) {
  186. echo " symlink " . $root . "/" . $target . " " . $root . "/" . $link . "\n";
  187. //first removing folders to avoid errors if the folder already exists
  188. @rmdir($root . "/" . $link);
  189. //next removing existing symlink in order to update the target
  190. if (is_link($root . "/" . $link)) {
  191. @unlink($root . "/" . $link);
  192. }
  193. @symlink($root . "/" . $target, $root . "/" . $link);
  194. }
  195. }