Commit 95b18d55 by Alexander Makarov

more on console apps

parent ea1c5c78
...@@ -132,7 +132,7 @@ class Application extends \yii\base\Application ...@@ -132,7 +132,7 @@ class Application extends \yii\base\Application
'help' => 'yii\console\controllers\HelpController', 'help' => 'yii\console\controllers\HelpController',
'migrate' => 'yii\console\controllers\MigrateController', 'migrate' => 'yii\console\controllers\MigrateController',
'shell' => 'yii\console\controllers\ShellController', 'shell' => 'yii\console\controllers\ShellController',
'app' => 'yii\console\controllers\AppController', 'create' => 'yii\console\controllers\CreateController',
); );
} }
......
...@@ -60,4 +60,56 @@ class Controller extends \yii\base\Controller ...@@ -60,4 +60,56 @@ class Controller extends \yii\base\Controller
\Yii::$application->end(1); \Yii::$application->end(1);
} }
} }
/**
* Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
*
* @param string $message to echo out before waiting for user input
* @param string $default the default string to be returned when user does not write anything.
* Defaults to null, means that default string is disabled.
* @return mixed line read as a string, or false if input has been closed
*/
public function prompt($message, $default = null)
{
if($default !== null) {
$message .= " [$default] ";
}
else {
$message .= ' ';
}
if(extension_loaded('readline')) {
$input = readline($message);
if($input !== false) {
readline_add_history($input);
}
}
else {
echo $message;
$input = fgets(STDIN);
}
if($input === false) {
return false;
}
else {
$input = trim($input);
return ($input === '' && $default !== null) ? $default : $input;
}
}
/**
* Asks user to confirm by typing y or n.
*
* @param string $message to echo out before waiting for user input
* @param boolean $default this value is returned if no selection is made.
* @return boolean whether user confirmed
*/
public function confirm($message, $default = false)
{
echo $message . ' (yes|no) [' . ($default ? 'yes' : 'no') . ']:';
$input = trim(fgets(STDIN));
return empty($input) ? $default : !strncasecmp($input, 'y', 1);
}
} }
\ No newline at end of file
<?php <?php
/** /**
* WebAppCommand class file. * CreateController class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
...@@ -18,21 +18,23 @@ use yii\console\Controller; ...@@ -18,21 +18,23 @@ use yii\console\Controller;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class AppController extends Controller class CreateController extends Controller
{ {
private $_rootPath; private $_rootPath;
/** /**
* Generates Yii application at the path specified via appPath parameter. * Generates Yii application at the path specified via appPath parameter.
* *
* @param string $appPath the directory where the new application will be created. * @param string $path the directory where the new application will be created.
* If the directory does not exist, it will be created. After the application * If the directory does not exist, it will be created. After the application
* is created, please make sure the directory has enough permissions. * is created, please make sure the directory has enough permissions.
* @param string $type application type. If not specified default application
* skeleton will be used.
* @return integer the exit status * @return integer the exit status
*/ */
public function actionIndex($appPath) public function actionIndex($path, $type = 'default')
{ {
$path=strtr($appPath,'/\\',DIRECTORY_SEPARATOR); $path=strtr($path,'/\\',DIRECTORY_SEPARATOR);
if(strpos($path,DIRECTORY_SEPARATOR)===false) if(strpos($path,DIRECTORY_SEPARATOR)===false)
$path='.'.DIRECTORY_SEPARATOR.$path; $path='.'.DIRECTORY_SEPARATOR.$path;
$dir=rtrim(realpath(dirname($path)),'\\/'); $dir=rtrim(realpath(dirname($path)),'\\/');
...@@ -42,11 +44,11 @@ class AppController extends Controller ...@@ -42,11 +44,11 @@ class AppController extends Controller
$this->_rootPath=$path=$dir; $this->_rootPath=$path=$dir;
else else
$this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path); $this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path);
if($this->confirm("Create a Web application under '$path'?")) if($this->confirm("Create \"$type\" application under '$path'?"))
{ {
$sourceDir=realpath(dirname(__FILE__).'/../views/webapp'); $sourceDir=realpath(__DIR__.'/../create/'.$type);
if($sourceDir===false) if($sourceDir===false)
die("\nUnable to locate the source directory.\n"); die("\nUnable to locate the source directory for \"$type\".\n");
$list=$this->buildFileList($sourceDir,$path); $list=$this->buildFileList($sourceDir,$path);
$list['index.php']['callback']=array($this,'generateIndex'); $list['index.php']['callback']=array($this,'generateIndex');
$list['index-test.php']['callback']=array($this,'generateIndex'); $list['index-test.php']['callback']=array($this,'generateIndex');
......
<?php <?php
/** /**
* MessageCommand class file. * MessageController class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
......
<?php <?php
/** /**
* MigrateCommand class file. * MigrateController class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
......
<?php <?php
/** /**
* ShellCommand class file. * ShellController class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
...@@ -13,7 +13,7 @@ namespace yii\console\controllers; ...@@ -13,7 +13,7 @@ namespace yii\console\controllers;
use yii\console\Controller; use yii\console\Controller;
/** /**
* ShellCommand executes the specified Web application and provides a shell for interaction. * This command executes the specified Web application and provides a shell for interaction.
* *
* @property string $help The help information for the shell command. * @property string $help The help information for the shell command.
* *
......
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