|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #!/usr/bin/env php
- <?php
- /**
- * Yii Application Initialization Tool
- *
- * In order to run in non-interactive mode:
- *
- * init --env=Development --overwrite=n
- *
- * @author Alexander Makarov <sam@rmcreative.ru>
- *
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
-
- if (!extension_loaded('openssl')) {
- die('The OpenSSL PHP extension is required by Yii2.');
- }
-
- $params = getParams();
- $root = str_replace('\\', '/', __DIR__);
- $envs = require("$root/environments/index.php");
- $envNames = array_keys($envs);
-
- echo "Yii Application Initialization Tool v1.0\n\n";
-
- $envName = null;
- if (empty($params['env']) || $params['env'] === '1') {
- echo "Which environment do you want the application to be initialized in?\n\n";
- foreach ($envNames as $i => $name) {
- echo " [$i] $name\n";
- }
- echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
- $answer = trim(fgets(STDIN));
-
- if (!ctype_digit($answer) || !in_array($answer, range(0, count($envs) - 1))) {
- echo "\n Quit initialization.\n";
- exit(0);
- }
-
- if (isset($envNames[$answer])) {
- $envName = $envNames[$answer];
- }
- } else {
- $envName = $params['env'];
- }
-
- if (!in_array($envName, $envNames)) {
- $envsList = implode(', ', $envNames);
- echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
- exit(2);
- }
-
- $env = $envs[$envName];
-
- if (empty($params['env'])) {
- echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
- $answer = trim(fgets(STDIN));
- if (strncasecmp($answer, 'y', 1)) {
- echo "\n Quit initialization.\n";
- exit(0);
- }
- }
-
- echo "\n Start initialization ...\n\n";
- $files = getFileList("$root/environments/{$env['path']}");
- $all = false;
- foreach ($files as $file) {
- if (!copyFile($root, "environments/{$env['path']}/$file", $file, $all, $params)) {
- break;
- }
- }
-
- $callbacks = ['setCookieValidationKey', 'setWritable', 'setExecutable'];
- foreach ($callbacks as $callback) {
- if (!empty($env[$callback])) {
- $callback($root, $env[$callback]);
- }
- }
-
- echo "\n ... initialization completed.\n\n";
-
- function getFileList($root, $basePath = '')
- {
- $files = [];
- $handle = opendir($root);
- while (($path = readdir($handle)) !== false) {
- if ($path === '.svn' || $path === '.' || $path === '..') {
- continue;
- }
- $fullPath = "$root/$path";
- $relativePath = $basePath === '' ? $path : "$basePath/$path";
- if (is_dir($fullPath)) {
- $files = array_merge($files, getFileList($fullPath, $relativePath));
- } else {
- $files[] = $relativePath;
- }
- }
- closedir($handle);
- return $files;
- }
-
- function copyFile($root, $source, $target, &$all, $params)
- {
- if (!is_file($root . '/' . $source)) {
- echo " skip $target ($source not exist)\n";
- return true;
- }
- if (is_file($root . '/' . $target)) {
- if (file_get_contents($root . '/' . $source) === file_get_contents($root . '/' . $target)) {
- echo " unchanged $target\n";
- return true;
- }
- if ($all) {
- echo " overwrite $target\n";
- } else {
- echo " exist $target\n";
- echo " ...overwrite? [Yes|No|All|Quit] ";
-
-
- $answer = !empty($params['overwrite']) ? $params['overwrite'] : trim(fgets(STDIN));
- if (!strncasecmp($answer, 'q', 1)) {
- return false;
- } else {
- if (!strncasecmp($answer, 'y', 1)) {
- echo " overwrite $target\n";
- } else {
- if (!strncasecmp($answer, 'a', 1)) {
- echo " overwrite $target\n";
- $all = true;
- } else {
- echo " skip $target\n";
- return true;
- }
- }
- }
- }
- file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
- return true;
- }
- echo " generate $target\n";
- @mkdir(dirname($root . '/' . $target), 0777, true);
- file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
- return true;
- }
-
- function getParams()
- {
- $rawParams = [];
- if (isset($_SERVER['argv'])) {
- $rawParams = $_SERVER['argv'];
- array_shift($rawParams);
- }
-
- $params = [];
- foreach ($rawParams as $param) {
- if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
- $name = $matches[1];
- $params[$name] = isset($matches[3]) ? $matches[3] : true;
- } else {
- $params[] = $param;
- }
- }
- return $params;
- }
-
- function setWritable($root, $paths)
- {
- foreach ($paths as $writable) {
- echo " chmod 0777 $writable\n";
- @chmod("$root/$writable", 0777);
- }
- }
-
- function setExecutable($root, $paths)
- {
- foreach ($paths as $executable) {
- echo " chmod 0755 $executable\n";
- @chmod("$root/$executable", 0755);
- }
- }
-
- function setCookieValidationKey($root, $paths)
- {
- foreach ($paths as $file) {
- echo " generate cookie validation key in $file\n";
- $file = $root . '/' . $file;
- $length = 32;
- $bytes = openssl_random_pseudo_bytes($length);
- $key = strtr(substr(base64_encode($bytes), 0, $length), '+/=', '_-.');
- $content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($file));
- file_put_contents($file, $content);
- }
- }
-
- function createSymlink($links)
- {
- foreach ($links as $link => $target) {
- echo " symlink $target as $link\n";
- if (!is_link($link)) {
- symlink($target, $link);
- }
- }
- }
|