You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.9 KiB
117 lines
3.9 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
12 years ago
|
* CreateController class file.
|
||
13 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright © 2008-2012 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\console\controllers;
|
||
|
|
||
|
use yii\console\Controller;
|
||
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* This command creates an Yii Web application at the specified location.
|
||
13 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
12 years ago
|
* @since 2.0
|
||
13 years ago
|
*/
|
||
12 years ago
|
class CreateController extends Controller
|
||
13 years ago
|
{
|
||
|
private $_rootPath;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Generates Yii application at the path specified via appPath parameter.
|
||
|
*
|
||
12 years ago
|
* @param string $path the directory where the new application will be created.
|
||
12 years ago
|
* If the directory does not exist, it will be created. After the application
|
||
|
* is created, please make sure the directory has enough permissions.
|
||
12 years ago
|
* @param string $type application type. If not specified default application
|
||
|
* skeleton will be used.
|
||
12 years ago
|
* @return integer the exit status
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function actionIndex($path, $type = 'default')
|
||
13 years ago
|
{
|
||
12 years ago
|
$path=strtr($path,'/\\',DIRECTORY_SEPARATOR);
|
||
13 years ago
|
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);
|
||
12 years ago
|
if($this->confirm("Create \"$type\" application under '$path'?"))
|
||
13 years ago
|
{
|
||
12 years ago
|
$sourceDir=realpath(__DIR__.'/../create/'.$type);
|
||
13 years ago
|
if($sourceDir===false)
|
||
12 years ago
|
die("\nUnable to locate the source directory for \"$type\".\n");
|
||
13 years ago
|
$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).'\'';
|
||
|
}
|
||
|
}
|