Browse Source

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

Usage:
init --env=Development
init --env=Production
tags/2.0.0-beta
Alexander Makarov 11 years ago
parent
commit
4ab2e986c6
  1. 3
      apps/advanced/README.md
  2. 68
      apps/advanced/init

3
apps/advanced/README.md

@ -103,7 +103,8 @@ GETTING STARTED
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.
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`.
3. In `common/config/params.php` set your database details in `components.db` values.

68
apps/advanced/init

@ -1,27 +1,49 @@
#!/usr/bin/env php
<?php
$params = getParams();
$root = str_replace('\\', '/', __DIR__);
$envs = require("$root/environments/index.php");
$envNames = array_keys($envs);
echo "Yii Application Init Tool v1.0\n\n";
echo "Which environment do you want the application to be initialized in?\n\n";
foreach ($envNames as $i => $name) {
echo "Yii Application Initialization Tool v1.0\n\n";
$envName = null;
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) || !isset($envNames[$answer])) {
}
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";
return;
exit(1);
}
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[$envNames[$answer]];
echo "\n Initialize the application under '{$envNames[$answer]}' environment? [yes|no] ";
$answer = trim(fgets(STDIN));
if (strncasecmp($answer, 'y', 1)) {
$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";
return;
exit(1);
}
}
echo "\n Start initialization ...\n\n";
@ -110,3 +132,23 @@ function copyFile($root, $source, $target, &$all)
file_put_contents($root . '/' . $target, file_get_contents($root . '/' . $source));
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;
}

Loading…
Cancel
Save