Commit 4ab2e986 by Alexander Makarov

Advanced application: init script can now be executed in non-interactive input mode

Usage: init --env=Development init --env=Production
parent 907d24fb
...@@ -103,7 +103,8 @@ GETTING STARTED ...@@ -103,7 +103,8 @@ GETTING STARTED
After you install the application, you have to conduct the following steps to initialize After you install the application, you have to conduct the following steps to initialize
the installed application. You only need to do these once for all. the installed application. You only need to do these once for all.
1. Execute the `init` command and select `dev` as environment. 1. Execute the `init` command and select `dev` as environment. Alternatively you can execute it as `init --env=Development`
or `init --env=Production`.
2. Create a new database. It is assumed that MySQL InnoDB is used. If not, adjust `console/migrations/m130524_201442_init.php`. 2. Create a new database. It is assumed that MySQL InnoDB is used. If not, adjust `console/migrations/m130524_201442_init.php`.
3. In `common/config/params.php` set your database details in `components.db` values. 3. In `common/config/params.php` set your database details in `components.db` values.
......
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
$params = getParams();
$root = str_replace('\\', '/', __DIR__); $root = str_replace('\\', '/', __DIR__);
$envs = require("$root/environments/index.php"); $envs = require("$root/environments/index.php");
$envNames = array_keys($envs); $envNames = array_keys($envs);
echo "Yii Application Init Tool v1.0\n\n"; echo "Yii Application Initialization Tool v1.0\n\n";
echo "Which environment do you want the application to be initialized in?\n\n";
foreach ($envNames as $i => $name) { $envName = null;
echo " [$i] $name\n"; if (empty($params['env'])) {
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(1);
}
if(isset($envNames[$answer])) {
$envName = $envNames[$answer];
}
}
else {
$envName = $params['env'];
} }
echo "\n Your choice [0-" . (count($envs) - 1) . ', or "q" to quit] ';
$answer = trim(fgets(STDIN)); if (!in_array($envName, $envNames)) {
if (!ctype_digit($answer) || !isset($envNames[$answer])) { $envsList = implode(', ', $envNames);
echo "\n Quit initialization.\n"; echo "\n $envName is not a valid environment. Try one of the following: $envsList. \n";
return; exit(2);
} }
$env = $envs[$envNames[$answer]]; $env = $envs[$envName];
echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
$answer = trim(fgets(STDIN)); if (empty($params['env'])) {
if (strncasecmp($answer, 'y', 1)) { echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
echo "\n Quit initialization.\n"; $answer = trim(fgets(STDIN));
return; if (strncasecmp($answer, 'y', 1)) {
echo "\n Quit initialization.\n";
exit(1);
}
} }
echo "\n Start initialization ...\n\n"; echo "\n Start initialization ...\n\n";
...@@ -110,3 +132,23 @@ function copyFile($root, $source, $target, &$all) ...@@ -110,3 +132,23 @@ function copyFile($root, $source, $target, &$all)
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source)); file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
return true; return true;
} }
function getParams()
{
$rawParams = array();
if (isset($_SERVER['argv'])) {
$rawParams = $_SERVER['argv'];
array_shift($rawParams);
}
$params = array();
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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment