|
|
@ -11,6 +11,7 @@ |
|
|
|
namespace yii\console\controllers; |
|
|
|
namespace yii\console\controllers; |
|
|
|
|
|
|
|
|
|
|
|
use yii\console\Controller; |
|
|
|
use yii\console\Controller; |
|
|
|
|
|
|
|
use yii\util\FileHelper; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* This command creates an Yii Web application at the specified location. |
|
|
|
* This command creates an Yii Web application at the specified location. |
|
|
@ -20,6 +21,8 @@ use yii\console\Controller; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class CreateController extends Controller |
|
|
|
class CreateController extends Controller |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
const EXIT_UNABLE_TO_LOCATE_SOURCE = 1; |
|
|
|
|
|
|
|
|
|
|
|
private $_rootPath; |
|
|
|
private $_rootPath; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -34,83 +37,119 @@ class CreateController extends Controller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function actionIndex($path, $type = 'default') |
|
|
|
public function actionIndex($path, $type = 'default') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$path=strtr($path,'/\\',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)),'\\/'); |
|
|
|
} |
|
|
|
if($dir===false || !is_dir($dir)) |
|
|
|
$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."); |
|
|
|
$this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists."); |
|
|
|
if(basename($path)==='.') |
|
|
|
} |
|
|
|
$this->_rootPath=$path=$dir; |
|
|
|
if(basename($path) === '.') { |
|
|
|
else |
|
|
|
$this->_rootPath = $path = $dir; |
|
|
|
$this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path); |
|
|
|
} |
|
|
|
if($this->confirm("Create \"$type\" application under '$path'?")) |
|
|
|
else { |
|
|
|
{ |
|
|
|
$this->_rootPath = $path = $dir.DIRECTORY_SEPARATOR.basename($path); |
|
|
|
$sourceDir=realpath(__DIR__.'/../create/'.$type); |
|
|
|
} |
|
|
|
if($sourceDir===false) |
|
|
|
if($this->confirm("Create \"$type\" application under '$path'?")) { |
|
|
|
die("\nUnable to locate the source directory for \"$type\".\n"); |
|
|
|
$sourceDir = realpath(__DIR__.'/../create/'.$type); |
|
|
|
$list=$this->buildFileList($sourceDir,$path); |
|
|
|
if($sourceDir === false) { |
|
|
|
$list['index.php']['callback']=array($this,'generateIndex'); |
|
|
|
echo "\nUnable to locate the source directory for \"$type\".\n"; |
|
|
|
$list['index-test.php']['callback']=array($this,'generateIndex'); |
|
|
|
return self::EXIT_UNABLE_TO_LOCATE_SOURCE; |
|
|
|
$list['protected/tests/bootstrap.php']['callback']=array($this,'generateTestBoostrap'); |
|
|
|
} |
|
|
|
$list['protected/yiic.php']['callback']=array($this,'generateYiic'); |
|
|
|
$list = FileHelper::buildFileList($sourceDir, $path); |
|
|
|
$this->copyFiles($list); |
|
|
|
$list['index.php']['callback'] = array($this, 'generateIndex'); |
|
|
|
@chmod($path.'/assets',0777); |
|
|
|
$list['index-test.php']['callback'] = array($this, 'generateIndex'); |
|
|
|
@chmod($path.'/protected/runtime',0777); |
|
|
|
$list['protected/tests/bootstrap.php']['callback'] = array($this, 'generateTestBoostrap'); |
|
|
|
@chmod($path.'/protected/data',0777); |
|
|
|
$list['protected/yiic.php']['callback'] = array($this, 'generateYiic'); |
|
|
|
@chmod($path.'/protected/data/testdrive.db',0777); |
|
|
|
FileHelper::copyFiles($list); |
|
|
|
@chmod($path.'/protected/yiic',0755); |
|
|
|
//@chmod($path.'/assets', 0777); |
|
|
|
|
|
|
|
//@chmod($path.'/protected/runtime', 0777); |
|
|
|
|
|
|
|
//@chmod($path.'/protected/yiic', 0755); |
|
|
|
echo "\nYour application has been created successfully under {$path}.\n"; |
|
|
|
echo "\nYour application has been created successfully under {$path}.\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generateIndex($source,$params) |
|
|
|
/** |
|
|
|
|
|
|
|
* Generates index.php file contents |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param string $source path to index.php template |
|
|
|
|
|
|
|
* @param array $params |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string final index.php file contents |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function generateIndex($source, $params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$content=file_get_contents($source); |
|
|
|
$content = file_get_contents($source); |
|
|
|
$yii=realpath(dirname(__FILE__).'/../../yii.php'); |
|
|
|
$yii = realpath(dirname(__FILE__).'/../../yii.php'); |
|
|
|
$yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'index.php'); |
|
|
|
$yii = $this->getRelativePath($yii, $this->_rootPath.DIRECTORY_SEPARATOR.'index.php'); |
|
|
|
$yii=str_replace('\\','\\\\',$yii); |
|
|
|
$yii = str_replace('\\', '\\\\', $yii); |
|
|
|
return preg_replace('/\$yii\s*=(.*?);/',"\$yii=$yii;",$content); |
|
|
|
return preg_replace('/\$yii\s*=(.*?);/', "\$yii=$yii;", $content); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generateTestBoostrap($source,$params) |
|
|
|
/** |
|
|
|
|
|
|
|
* Generates index-test.php file contents |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @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) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$content=file_get_contents($source); |
|
|
|
$content = file_get_contents($source); |
|
|
|
$yii=realpath(dirname(__FILE__).'/../../yiit.php'); |
|
|
|
$yii = realpath(dirname(__FILE__).'/../../yiit.php'); |
|
|
|
$yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'bootstrap.php'); |
|
|
|
$yii = $this->getRelativePath($yii, $this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'bootstrap.php'); |
|
|
|
$yii=str_replace('\\','\\\\',$yii); |
|
|
|
$yii = str_replace('\\', '\\\\', $yii); |
|
|
|
return preg_replace('/\$yiit\s*=(.*?);/',"\$yiit=$yii;",$content); |
|
|
|
return preg_replace('/\$yiit\s*=(.*?);/', "\$yiit=$yii;", $content); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generateYiic($source,$params) |
|
|
|
/** |
|
|
|
|
|
|
|
* Generates yiic.php file contents |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param string $source path to yiic.php template |
|
|
|
|
|
|
|
* @param array $params |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string final yiic.php file contents |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function generateYiic($source, $params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$content=file_get_contents($source); |
|
|
|
$content = file_get_contents($source); |
|
|
|
$yiic=realpath(dirname(__FILE__).'/../../yiic.php'); |
|
|
|
$yiic = realpath(dirname(__FILE__).'/../../yiic.php'); |
|
|
|
$yiic=$this->getRelativePath($yiic,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php'); |
|
|
|
$yiic = $this->getRelativePath($yiic, $this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php'); |
|
|
|
$yiic=str_replace('\\','\\\\',$yiic); |
|
|
|
$yiic = str_replace('\\', '\\\\', $yiic); |
|
|
|
return preg_replace('/\$yiic\s*=(.*?);/',"\$yiic=$yiic;",$content); |
|
|
|
return preg_replace('/\$yiic\s*=(.*?);/', "\$yiic=$yiic;", $content); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected function getRelativePath($path1,$path2) |
|
|
|
/** |
|
|
|
|
|
|
|
* @param string $path1 abosolute path |
|
|
|
|
|
|
|
* @param string $path2 abosolute path |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return string relative path |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function getRelativePath($path1, $path2) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$segs1=explode(DIRECTORY_SEPARATOR,$path1); |
|
|
|
$segs1 = explode(DIRECTORY_SEPARATOR, $path1); |
|
|
|
$segs2=explode(DIRECTORY_SEPARATOR,$path2); |
|
|
|
$segs2 = explode(DIRECTORY_SEPARATOR, $path2); |
|
|
|
$n1=count($segs1); |
|
|
|
$n1 = count($segs1); |
|
|
|
$n2=count($segs2); |
|
|
|
$n2 = count($segs2); |
|
|
|
|
|
|
|
|
|
|
|
for($i=0;$i<$n1 && $i<$n2;++$i) |
|
|
|
for($i=0; $i<$n1 && $i<$n2; ++$i) { |
|
|
|
{ |
|
|
|
if($segs1[$i] !== $segs2[$i]) { |
|
|
|
if($segs1[$i]!==$segs2[$i]) |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if($i===0) |
|
|
|
if($i===0) { |
|
|
|
return "'".$path1."'"; |
|
|
|
return "'".$path1."'"; |
|
|
|
|
|
|
|
} |
|
|
|
$up=''; |
|
|
|
$up=''; |
|
|
|
for($j=$i;$j<$n2-1;++$j) |
|
|
|
for($j=$i;$j<$n2-1;++$j) { |
|
|
|
$up.='/..'; |
|
|
|
$up.='/..'; |
|
|
|
for(;$i<$n1-1;++$i) |
|
|
|
} |
|
|
|
|
|
|
|
for(; $i<$n1-1; ++$i) { |
|
|
|
$up.='/'.$segs1[$i]; |
|
|
|
$up.='/'.$segs1[$i]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\''; |
|
|
|
return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\''; |
|
|
|
} |
|
|
|
} |
|
|
|