Browse Source

added ability to create own "yiic create" templates

tags/2.0.0-beta
Alexander Makarov 12 years ago
parent
commit
78ab5c654a
  1. 153
      framework/console/controllers/CreateController.php
  2. 20
      framework/console/create/config.php
  3. 7
      framework/console/create/default/index.php

153
framework/console/controllers/CreateController.php

@ -2,7 +2,6 @@
/** /**
* CreateController class file. * CreateController class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
@ -12,18 +11,40 @@ namespace yii\console\controllers;
use yii\console\Controller; use yii\console\Controller;
use yii\util\FileHelper; use yii\util\FileHelper;
use yii\base\Exception;
/** /**
* This command creates an Yii Web application at the specified location. * This command creates an Yii Web application at the specified location.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0 * @since 2.0
*/ */
class CreateController extends Controller class CreateController extends Controller
{ {
const EXIT_UNABLE_TO_LOCATE_SOURCE = 1;
private $_rootPath; 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. * Generates Yii application at the path specified via appPath parameter.
@ -31,11 +52,11 @@ class CreateController extends Controller
* @param string $path 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. * @throws \yii\base\Exception if path specified is not valid
* @return integer the exit status * @return integer the exit status
*/ */
public function actionIndex($path, $type = 'default') public function actionIndex($path)
{ {
$path = strtr($path, '/\\', DIRECTORY_SEPARATOR); $path = strtr($path, '/\\', DIRECTORY_SEPARATOR);
if(strpos($path, DIRECTORY_SEPARATOR) === false) { if(strpos($path, DIRECTORY_SEPARATOR) === false) {
@ -43,82 +64,104 @@ class CreateController extends Controller
} }
$dir = rtrim(realpath(dirname($path)), '\\/'); $dir = rtrim(realpath(dirname($path)), '\\/');
if($dir === false || !is_dir($dir)) { if($dir === false || !is_dir($dir)) {
$this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists."); throw new Exception("The directory '$path' is not valid. Please make sure the parent directory exists.");
} }
if(basename($path) === '.') { if(basename($path) === '.') {
$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 \"$type\" application under '$path'?")) {
$sourceDir = realpath(__DIR__.'/../create/'.$type); if($this->confirm("Create \"$this->type\" application under '$path'?")) {
if($sourceDir === false) { $sourceDir = $this->getSourceDir();
echo "\nUnable to locate the source directory for \"$type\".\n"; $config = $this->getConfig();
return self::EXIT_UNABLE_TO_LOCATE_SOURCE;
}
$list = FileHelper::buildFileList($sourceDir, $path); $list = FileHelper::buildFileList($sourceDir, $path);
$list['index.php']['callback'] = array($this, 'generateIndex');
$list['index-test.php']['callback'] = array($this, 'generateIndex'); if(is_array($config)) {
$list['protected/tests/bootstrap.php']['callback'] = array($this, 'generateTestBoostrap'); foreach($config as $file => $settings) {
$list['protected/yiic.php']['callback'] = array($this, 'generateYiic'); if(isset($settings['handler'])) {
$list[$file]['callback'] = $settings['handler'];
}
}
}
FileHelper::copyFiles($list); FileHelper::copyFiles($list);
//@chmod($path.'/assets', 0777);
//@chmod($path.'/protected/runtime', 0777); if(is_array($config)) {
//@chmod($path.'/protected/yiic', 0755); foreach($config as $file => $settings) {
if(isset($settings['permissions'])) {
@chmod($path.'/'.$file, $settings['permissions']);
}
}
}
echo "\nYour application has been created successfully under {$path}.\n"; echo "\nYour application has been created successfully under {$path}.\n";
} }
} }
/** /**
* Generates index.php file contents * @throws \yii\base\Exception if source directory wasn't located
* * @return string
* @param string $source path to index.php template
* @param array $params
*
* @return string final index.php file contents
*/ */
public function generateIndex($source, $params) protected function getSourceDir()
{ {
$content = file_get_contents($source); $customSource = realpath($this->templatesPath.'/'.$this->type);
$yii = realpath(dirname(__FILE__).'/../../yii.php'); $defaultSource = realpath($this->getDefaultTemplatesPath().'/'.$this->type);
$yii = $this->getRelativePath($yii, $this->_rootPath.DIRECTORY_SEPARATOR.'index.php');
$yii = str_replace('\\', '\\\\', $yii); if($customSource) {
return preg_replace('/\$yii\s*=(.*?);/', "\$yii=$yii;", $content); return $customSource;
}
elseif($defaultSource) {
return $defaultSource;
}
else {
throw new Exception('Unable to locate the source directory for "'.$this->type.'".');
}
} }
/** /**
* Generates index-test.php file contents * @return string default templates path
*
* @param string $source path to index-test.php template
* @param array $params
*
* @return string final index-test.php file contents
*/ */
public function generateTestBoostrap($source, $params) protected function getDefaultTemplatesPath()
{ {
$content = file_get_contents($source); return realpath(__DIR__.'/../create');
$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);
} }
/** /**
* Generates yiic.php file contents * @return array|null template configuration
* */
* @param string $source path to yiic.php template protected function getConfig()
* @param array $params {
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 final yiic.php file contents * @return string target file contetns
*/ */
public function generateYiic($source, $params) public function replaceRelativePath($source, $pathTo, $varName)
{ {
$content = file_get_contents($source); $content = file_get_contents($source);
$yiic = realpath(dirname(__FILE__).'/../../yiic.php'); $relativeFile = str_replace($this->getSourceDir(), '', $source);
$yiic = $this->getRelativePath($yiic, $this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php');
$yiic = str_replace('\\', '\\\\', $yiic); $relativePath = $this->getRelativePath($pathTo, $this->_rootPath.$relativeFile);
return preg_replace('/\$yiic\s*=(.*?);/', "\$yiic=$yiic;", $content); $relativePath = str_replace('\\', '\\\\', $relativePath);
return preg_replace('/\$'.$varName.'\s*=(.*?);/', "\$".$varName."=$relativePath;", $content);
} }
/** /**
@ -151,6 +194,6 @@ class CreateController extends Controller
$up.='/'.$segs1[$i]; $up.='/'.$segs1[$i];
} }
return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\''; return '__DIR__.\''.$up.'/'.basename($path1).'\'';
} }
} }

20
framework/console/create/config.php

@ -0,0 +1,20 @@
<?php
/** @var $controller \yii\console\controllers\CreateController */
$controller = $this;
return array(
'default' => array(
'index.php' => array(
'handler' => function($source) use ($controller) {
return $controller->replaceRelativePath($source, realpath(YII_PATH.'/yii.php'), 'yii');
},
'permissions' => 0777,
),
'assets' => array(
'permissions' => 0777,
),
'protected/runtime' => array(
'permissions' => 0755,
),
),
);

7
framework/console/create/default/index.php

@ -1 +1,8 @@
<?php <?php
// change the following paths if necessary
$yii=__DIR__.'/../framework/yii.php';
$config=__DIR__.'/protected/config/main.php';
require_once($yii);
Yii::createWebApplication($config)->run();

Loading…
Cancel
Save