From 95b18d553b6aa6e880dc11cc3a42ac48d23c917f Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 9 Sep 2012 04:29:39 +0400 Subject: [PATCH] more on console apps --- framework/console/Application.php | 2 +- framework/console/Controller.php | 52 +++++++++ framework/console/controllers/AppController.php | 115 -------------------- framework/console/controllers/CreateController.php | 117 +++++++++++++++++++++ .../console/controllers/MessageController.php | 2 +- .../console/controllers/MigrateController.php | 2 +- framework/console/controllers/ShellController.php | 4 +- framework/console/create/default/index.php | 1 + 8 files changed, 175 insertions(+), 120 deletions(-) delete mode 100644 framework/console/controllers/AppController.php create mode 100644 framework/console/controllers/CreateController.php create mode 100644 framework/console/create/default/index.php diff --git a/framework/console/Application.php b/framework/console/Application.php index 1e1796f..334f5e5 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -132,7 +132,7 @@ class Application extends \yii\base\Application 'help' => 'yii\console\controllers\HelpController', 'migrate' => 'yii\console\controllers\MigrateController', 'shell' => 'yii\console\controllers\ShellController', - 'app' => 'yii\console\controllers\AppController', + 'create' => 'yii\console\controllers\CreateController', ); } diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 472bb49..042a084 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -60,4 +60,56 @@ class Controller extends \yii\base\Controller \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 diff --git a/framework/console/controllers/AppController.php b/framework/console/controllers/AppController.php deleted file mode 100644 index 83050a0..0000000 --- a/framework/console/controllers/AppController.php +++ /dev/null @@ -1,115 +0,0 @@ - - * @link http://www.yiiframework.com/ - * @copyright Copyright © 2008-2012 Yii Software LLC - * @license http://www.yiiframework.com/license/ - */ - -namespace yii\console\controllers; - -use yii\console\Controller; - -/** - * This command creates an Yii Web application at the specified location. - * - * @author Qiang Xue - * @since 2.0 - */ -class AppController extends Controller -{ - private $_rootPath; - - /** - * Generates Yii application at the path specified via appPath parameter. - * - * @param string $appPath the directory where the new application will be created. - * If the directory does not exist, it will be created. After the application - * is created, please make sure the directory has enough permissions. - * @return integer the exit status - */ - public function actionIndex($appPath) - { - $path=strtr($appPath,'/\\',DIRECTORY_SEPARATOR); - if(strpos($path,DIRECTORY_SEPARATOR)===false) - $path='.'.DIRECTORY_SEPARATOR.$path; - $dir=rtrim(realpath(dirname($path)),'\\/'); - if($dir===false || !is_dir($dir)) - $this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists."); - if(basename($path)==='.') - $this->_rootPath=$path=$dir; - else - $this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path); - if($this->confirm("Create a Web application under '$path'?")) - { - $sourceDir=realpath(dirname(__FILE__).'/../views/webapp'); - if($sourceDir===false) - die("\nUnable to locate the source directory.\n"); - $list=$this->buildFileList($sourceDir,$path); - $list['index.php']['callback']=array($this,'generateIndex'); - $list['index-test.php']['callback']=array($this,'generateIndex'); - $list['protected/tests/bootstrap.php']['callback']=array($this,'generateTestBoostrap'); - $list['protected/yiic.php']['callback']=array($this,'generateYiic'); - $this->copyFiles($list); - @chmod($path.'/assets',0777); - @chmod($path.'/protected/runtime',0777); - @chmod($path.'/protected/data',0777); - @chmod($path.'/protected/data/testdrive.db',0777); - @chmod($path.'/protected/yiic',0755); - echo "\nYour application has been created successfully under {$path}.\n"; - } - } - - public function generateIndex($source,$params) - { - $content=file_get_contents($source); - $yii=realpath(dirname(__FILE__).'/../../yii.php'); - $yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'index.php'); - $yii=str_replace('\\','\\\\',$yii); - return preg_replace('/\$yii\s*=(.*?);/',"\$yii=$yii;",$content); - } - - public function generateTestBoostrap($source,$params) - { - $content=file_get_contents($source); - $yii=realpath(dirname(__FILE__).'/../../yiit.php'); - $yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'bootstrap.php'); - $yii=str_replace('\\','\\\\',$yii); - return preg_replace('/\$yiit\s*=(.*?);/',"\$yiit=$yii;",$content); - } - - public function generateYiic($source,$params) - { - $content=file_get_contents($source); - $yiic=realpath(dirname(__FILE__).'/../../yiic.php'); - $yiic=$this->getRelativePath($yiic,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php'); - $yiic=str_replace('\\','\\\\',$yiic); - return preg_replace('/\$yiic\s*=(.*?);/',"\$yiic=$yiic;",$content); - } - - protected function getRelativePath($path1,$path2) - { - $segs1=explode(DIRECTORY_SEPARATOR,$path1); - $segs2=explode(DIRECTORY_SEPARATOR,$path2); - $n1=count($segs1); - $n2=count($segs2); - - for($i=0;$i<$n1 && $i<$n2;++$i) - { - if($segs1[$i]!==$segs2[$i]) - break; - } - - if($i===0) - return "'".$path1."'"; - $up=''; - for($j=$i;$j<$n2-1;++$j) - $up.='/..'; - for(;$i<$n1-1;++$i) - $up.='/'.$segs1[$i]; - - return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\''; - } -} \ No newline at end of file diff --git a/framework/console/controllers/CreateController.php b/framework/console/controllers/CreateController.php new file mode 100644 index 0000000..72f0611 --- /dev/null +++ b/framework/console/controllers/CreateController.php @@ -0,0 +1,117 @@ + + * @link http://www.yiiframework.com/ + * @copyright Copyright © 2008-2012 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +namespace yii\console\controllers; + +use yii\console\Controller; + +/** + * This command creates an Yii Web application at the specified location. + * + * @author Qiang Xue + * @since 2.0 + */ +class CreateController extends Controller +{ + private $_rootPath; + + /** + * Generates Yii application at the path specified via appPath parameter. + * + * @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 + * 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 + */ + public function actionIndex($path, $type = 'default') + { + $path=strtr($path,'/\\',DIRECTORY_SEPARATOR); + if(strpos($path,DIRECTORY_SEPARATOR)===false) + $path='.'.DIRECTORY_SEPARATOR.$path; + $dir=rtrim(realpath(dirname($path)),'\\/'); + if($dir===false || !is_dir($dir)) + $this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists."); + if(basename($path)==='.') + $this->_rootPath=$path=$dir; + else + $this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path); + if($this->confirm("Create \"$type\" application under '$path'?")) + { + $sourceDir=realpath(__DIR__.'/../create/'.$type); + if($sourceDir===false) + die("\nUnable to locate the source directory for \"$type\".\n"); + $list=$this->buildFileList($sourceDir,$path); + $list['index.php']['callback']=array($this,'generateIndex'); + $list['index-test.php']['callback']=array($this,'generateIndex'); + $list['protected/tests/bootstrap.php']['callback']=array($this,'generateTestBoostrap'); + $list['protected/yiic.php']['callback']=array($this,'generateYiic'); + $this->copyFiles($list); + @chmod($path.'/assets',0777); + @chmod($path.'/protected/runtime',0777); + @chmod($path.'/protected/data',0777); + @chmod($path.'/protected/data/testdrive.db',0777); + @chmod($path.'/protected/yiic',0755); + echo "\nYour application has been created successfully under {$path}.\n"; + } + } + + public function generateIndex($source,$params) + { + $content=file_get_contents($source); + $yii=realpath(dirname(__FILE__).'/../../yii.php'); + $yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'index.php'); + $yii=str_replace('\\','\\\\',$yii); + return preg_replace('/\$yii\s*=(.*?);/',"\$yii=$yii;",$content); + } + + public function generateTestBoostrap($source,$params) + { + $content=file_get_contents($source); + $yii=realpath(dirname(__FILE__).'/../../yiit.php'); + $yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'bootstrap.php'); + $yii=str_replace('\\','\\\\',$yii); + return preg_replace('/\$yiit\s*=(.*?);/',"\$yiit=$yii;",$content); + } + + public function generateYiic($source,$params) + { + $content=file_get_contents($source); + $yiic=realpath(dirname(__FILE__).'/../../yiic.php'); + $yiic=$this->getRelativePath($yiic,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php'); + $yiic=str_replace('\\','\\\\',$yiic); + return preg_replace('/\$yiic\s*=(.*?);/',"\$yiic=$yiic;",$content); + } + + protected function getRelativePath($path1,$path2) + { + $segs1=explode(DIRECTORY_SEPARATOR,$path1); + $segs2=explode(DIRECTORY_SEPARATOR,$path2); + $n1=count($segs1); + $n2=count($segs2); + + for($i=0;$i<$n1 && $i<$n2;++$i) + { + if($segs1[$i]!==$segs2[$i]) + break; + } + + if($i===0) + return "'".$path1."'"; + $up=''; + for($j=$i;$j<$n2-1;++$j) + $up.='/..'; + for(;$i<$n1-1;++$i) + $up.='/'.$segs1[$i]; + + return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\''; + } +} \ No newline at end of file diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index dddad78..53a240f 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -1,6 +1,6 @@ * @link http://www.yiiframework.com/ diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 4f7d310..d211837 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -1,6 +1,6 @@ * @link http://www.yiiframework.com/ diff --git a/framework/console/controllers/ShellController.php b/framework/console/controllers/ShellController.php index 0dcaa52..349288c 100644 --- a/framework/console/controllers/ShellController.php +++ b/framework/console/controllers/ShellController.php @@ -1,6 +1,6 @@ * @link http://www.yiiframework.com/ @@ -13,7 +13,7 @@ namespace yii\console\controllers; 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. * diff --git a/framework/console/create/default/index.php b/framework/console/create/default/index.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/framework/console/create/default/index.php @@ -0,0 +1 @@ +