From a25f870090c3809a2235f97443f55a8a1aa2dff3 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 20 Feb 2013 00:30:02 +0400 Subject: [PATCH] renamed create -> webapp/create, specified global options --- framework/console/Application.php | 2 +- framework/console/controllers/CreateController.php | 199 -------------------- framework/console/controllers/WebappController.php | 209 +++++++++++++++++++++ framework/console/create/config.php | 17 -- framework/console/webapp/config.php | 17 ++ framework/console/webapp/default/index.php | 10 + .../webapp/default/protected/config/main.php | 16 ++ .../protected/controllers/SiteController.php | 15 ++ .../default/protected/views/layouts/main.php | 16 ++ .../webapp/default/protected/views/site/index.php | 1 + 10 files changed, 285 insertions(+), 217 deletions(-) delete mode 100644 framework/console/controllers/CreateController.php create mode 100644 framework/console/controllers/WebappController.php delete mode 100644 framework/console/create/config.php create mode 100644 framework/console/webapp/config.php create mode 100644 framework/console/webapp/default/index.php create mode 100644 framework/console/webapp/default/protected/config/main.php create mode 100644 framework/console/webapp/default/protected/controllers/SiteController.php create mode 100644 framework/console/webapp/default/protected/views/layouts/main.php create mode 100644 framework/console/webapp/default/protected/views/site/index.php diff --git a/framework/console/Application.php b/framework/console/Application.php index fb579e2..537821c 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -126,7 +126,7 @@ class Application extends \yii\base\Application 'message' => 'yii\console\controllers\MessageController', 'help' => 'yii\console\controllers\HelpController', 'migrate' => 'yii\console\controllers\MigrateController', - 'app' => 'yii\console\controllers\CreateController', + 'webapp' => 'yii\console\controllers\WebappController', 'cache' => 'yii\console\controllers\CacheController', ); } diff --git a/framework/console/controllers/CreateController.php b/framework/console/controllers/CreateController.php deleted file mode 100644 index 7bd7fd0..0000000 --- a/framework/console/controllers/CreateController.php +++ /dev/null @@ -1,199 +0,0 @@ - - * @author Alexander Makarov - * @since 2.0 - */ -class CreateController extends Controller -{ - private $_rootPath; - private $_config; - - /** - * @var string custom template path. If specified, templates will be - * searched there additionally to `framework/console/create`. - */ - public $templatesPath; - - /** - * @var string application type. If not specified default application - * skeleton will be used. - */ - public $type = 'default'; - - public function init() - { - parent::init(); - - if($this->templatesPath && !is_dir($this->templatesPath)) { - throw new Exception('--templatesPath "'.$this->templatesPath.'" does not exist or can not be read.'); - } - } - - /** - * 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. - * - * @throws \yii\base\Exception if path specified is not valid - * @return integer the exit status - */ - public function actionIndex($path) - { - $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)) { - throw new Exception("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 \"$this->type\" application under '$path'?")) { - $sourceDir = $this->getSourceDir(); - $config = $this->getConfig(); - - $list = FileHelper::buildFileList($sourceDir, $path); - - if(is_array($config)) { - foreach($config as $file => $settings) { - if(isset($settings['handler'])) { - $list[$file]['callback'] = $settings['handler']; - } - } - } - - FileHelper::copyFiles($list); - - if(is_array($config)) { - foreach($config as $file => $settings) { - if(isset($settings['permissions'])) { - @chmod($path.'/'.$file, $settings['permissions']); - } - } - } - - echo "\nYour application has been created successfully under {$path}.\n"; - } - } - - /** - * @throws \yii\base\Exception if source directory wasn't located - * @return string - */ - protected function getSourceDir() - { - $customSource = realpath($this->templatesPath.'/'.$this->type); - $defaultSource = realpath($this->getDefaultTemplatesPath().'/'.$this->type); - - if($customSource) { - return $customSource; - } - elseif($defaultSource) { - return $defaultSource; - } - else { - throw new Exception('Unable to locate the source directory for "'.$this->type.'".'); - } - } - - /** - * @return string default templates path - */ - protected function getDefaultTemplatesPath() - { - return realpath(__DIR__.'/../create'); - } - - /** - * @return array|null template configuration - */ - protected function getConfig() - { - if($this->_config===null) { - $this->_config = require $this->getDefaultTemplatesPath().'/config.php'; - if($this->templatesPath && file_exists($this->templatesPath)) { - $this->_config = array_merge($this->_config, require $this->templatesPath.'/config.php'); - } - } - if(isset($this->_config[$this->type])) { - return $this->_config[$this->type]; - } - } - - /** - * @param string $source path to source file - * @param string $pathTo path to file we want to get relative path for - * @param string $varName variable name w/o $ to replace value with relative path for - * - * @return string target file contetns - */ - public function replaceRelativePath($source, $pathTo, $varName) - { - $content = file_get_contents($source); - $relativeFile = str_replace($this->getSourceDir(), '', $source); - - $relativePath = $this->getRelativePath($pathTo, $this->_rootPath.$relativeFile); - $relativePath = str_replace('\\', '\\\\', $relativePath); - - return preg_replace('/\$'.$varName.'\s*=(.*?);/', "\$".$varName."=$relativePath;", $content); - } - - /** - * @param string $path1 absolute path - * @param string $path2 absolute path - * - * @return string relative path - */ - 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 '__DIR__.\''.$up.'/'.basename($path1).'\''; - } -} \ No newline at end of file diff --git a/framework/console/controllers/WebappController.php b/framework/console/controllers/WebappController.php new file mode 100644 index 0000000..c19eff3 --- /dev/null +++ b/framework/console/controllers/WebappController.php @@ -0,0 +1,209 @@ + + * @author Alexander Makarov + * @since 2.0 + */ +class WebappController extends Controller +{ + private $_rootPath; + private $_config; + + /** + * @var string custom template path. If specified, templates will be + * searched there additionally to `framework/console/webapp`. + */ + public $templatesPath; + + /** + * @var string application type. If not specified default application + * skeleton will be used. + */ + public $type = 'default'; + + public function init() + { + parent::init(); + + if($this->templatesPath && !is_dir($this->templatesPath)) { + throw new Exception('--templatesPath "'.$this->templatesPath.'" does not exist or can not be read.'); + } + } + + public function globalOptions() + { + return array('templatesPath', 'type'); + } + + public function actionIndex() + { + $this->forward('help/index', array('-args' => array('webapp/create'))); + } + + /** + * 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. + * + * @throws \yii\base\Exception if path specified is not valid + * @return integer the exit status + */ + public function actionCreate($path) + { + $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)) { + throw new Exception("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 \"$this->type\" application under '$path'?")) { + $sourceDir = $this->getSourceDir(); + $config = $this->getConfig(); + + $list = FileHelper::buildFileList($sourceDir, $path); + + if(is_array($config)) { + foreach($config as $file => $settings) { + if(isset($settings['handler'])) { + $list[$file]['callback'] = $settings['handler']; + } + } + } + + FileHelper::copyFiles($list); + + if(is_array($config)) { + foreach($config as $file => $settings) { + if(isset($settings['permissions'])) { + @chmod($path.'/'.$file, $settings['permissions']); + } + } + } + + echo "\nYour application has been created successfully under {$path}.\n"; + } + } + + /** + * @throws \yii\base\Exception if source directory wasn't located + * @return string + */ + protected function getSourceDir() + { + $customSource = realpath($this->templatesPath.'/'.$this->type); + $defaultSource = realpath($this->getDefaultTemplatesPath().'/'.$this->type); + + if($customSource) { + return $customSource; + } + elseif($defaultSource) { + return $defaultSource; + } + else { + throw new Exception('Unable to locate the source directory for "'.$this->type.'".'); + } + } + + /** + * @return string default templates path + */ + protected function getDefaultTemplatesPath() + { + return realpath(__DIR__.'/../webapp'); + } + + /** + * @return array|null template configuration + */ + protected function getConfig() + { + if($this->_config===null) { + $this->_config = require $this->getDefaultTemplatesPath().'/config.php'; + if($this->templatesPath && file_exists($this->templatesPath)) { + $this->_config = array_merge($this->_config, require $this->templatesPath.'/config.php'); + } + } + if(isset($this->_config[$this->type])) { + return $this->_config[$this->type]; + } + } + + /** + * @param string $source path to source file + * @param string $pathTo path to file we want to get relative path for + * @param string $varName variable name w/o $ to replace value with relative path for + * + * @return string target file contetns + */ + public function replaceRelativePath($source, $pathTo, $varName) + { + $content = file_get_contents($source); + $relativeFile = str_replace($this->getSourceDir(), '', $source); + + $relativePath = $this->getRelativePath($pathTo, $this->_rootPath.$relativeFile); + $relativePath = str_replace('\\', '\\\\', $relativePath); + + return preg_replace('/\$'.$varName.'\s*=(.*?);/', "\$".$varName."=$relativePath;", $content); + } + + /** + * @param string $path1 absolute path + * @param string $path2 absolute path + * + * @return string relative path + */ + 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 '__DIR__.\''.$up.'/'.basename($path1).'\''; + } +} \ No newline at end of file diff --git a/framework/console/create/config.php b/framework/console/create/config.php deleted file mode 100644 index 29f0b0b..0000000 --- a/framework/console/create/config.php +++ /dev/null @@ -1,17 +0,0 @@ - array( - 'index.php' => array( - 'handler' => function($source) use ($controller) { - return $controller->replaceRelativePath($source, realpath(YII_PATH.'/yii.php'), 'yii'); - }, - 'permissions' => 0777, - ), - 'protected/runtime' => array( - 'permissions' => 0755, - ), - ), -); \ No newline at end of file diff --git a/framework/console/webapp/config.php b/framework/console/webapp/config.php new file mode 100644 index 0000000..6a8d28a --- /dev/null +++ b/framework/console/webapp/config.php @@ -0,0 +1,17 @@ + array( + 'index.php' => array( + 'handler' => function($source) use ($controller) { + return $controller->replaceRelativePath($source, realpath(YII_PATH.'/yii.php'), 'yii'); + }, + 'permissions' => 0777, + ), + 'protected/runtime' => array( + 'permissions' => 0755, + ), + ), +); \ No newline at end of file diff --git a/framework/console/webapp/default/index.php b/framework/console/webapp/default/index.php new file mode 100644 index 0000000..461b364 --- /dev/null +++ b/framework/console/webapp/default/index.php @@ -0,0 +1,10 @@ +run(); \ No newline at end of file diff --git a/framework/console/webapp/default/protected/config/main.php b/framework/console/webapp/default/protected/config/main.php new file mode 100644 index 0000000..1e3f981 --- /dev/null +++ b/framework/console/webapp/default/protected/config/main.php @@ -0,0 +1,16 @@ + 'My Web Application', + + 'components' => array( + // uncomment the following to use a MySQL database + /* + 'db' => array( + 'class' => 'yii\db\Connection', + 'dsn' => 'mysql:host=localhost;dbname=testdrive', + 'username' => 'root', + 'password' => '', + ), + */ + ), +); \ No newline at end of file diff --git a/framework/console/webapp/default/protected/controllers/SiteController.php b/framework/console/webapp/default/protected/controllers/SiteController.php new file mode 100644 index 0000000..b47b93c --- /dev/null +++ b/framework/console/webapp/default/protected/controllers/SiteController.php @@ -0,0 +1,15 @@ +render('index', array( + 'name' => 'Qiang', + )); + } +} \ No newline at end of file diff --git a/framework/console/webapp/default/protected/views/layouts/main.php b/framework/console/webapp/default/protected/views/layouts/main.php new file mode 100644 index 0000000..197b4a2 --- /dev/null +++ b/framework/console/webapp/default/protected/views/layouts/main.php @@ -0,0 +1,16 @@ + + + + + <?php echo $this->context->pageTitle?> + + +

context->pageTitle?>

+
+ +
+ + + \ No newline at end of file diff --git a/framework/console/webapp/default/protected/views/site/index.php b/framework/console/webapp/default/protected/views/site/index.php new file mode 100644 index 0000000..0fb8784 --- /dev/null +++ b/framework/console/webapp/default/protected/views/site/index.php @@ -0,0 +1 @@ +Hello, ! \ No newline at end of file