CreateController.php 3.89 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Alexander Makarov committed
3
 * CreateController class file.
Qiang Xue committed
4 5 6
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
7
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
Qiang Xue committed
8 9 10
 * @license http://www.yiiframework.com/license/
 */

11 12 13 14
namespace yii\console\controllers;

use yii\console\Controller;

Qiang Xue committed
15
/**
16
 * This command creates an Yii Web application at the specified location.
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
19
 * @since 2.0
Qiang Xue committed
20
 */
Alexander Makarov committed
21
class CreateController extends Controller
Qiang Xue committed
22 23 24 25
{
	private $_rootPath;

	/**
26 27
	 * Generates Yii application at the path specified via appPath parameter.
	 *
Alexander Makarov committed
28
	 * @param string $path the directory where the new application will be created.
29 30
	 * If the directory does not exist, it will be created. After the application
	 * is created, please make sure the directory has enough permissions.
Alexander Makarov committed
31 32
	 * @param string $type application type. If not specified default application
	 * skeleton will be used.
33
	 * @return integer the exit status
Qiang Xue committed
34
	 */
Alexander Makarov committed
35
	public function actionIndex($path, $type = 'default')
Qiang Xue committed
36
	{
Alexander Makarov committed
37
		$path=strtr($path,'/\\',DIRECTORY_SEPARATOR);
Qiang Xue committed
38 39 40 41 42 43 44 45 46
		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);
Alexander Makarov committed
47
		if($this->confirm("Create \"$type\" application under '$path'?"))
Qiang Xue committed
48
		{
Alexander Makarov committed
49
			$sourceDir=realpath(__DIR__.'/../create/'.$type);
Qiang Xue committed
50
			if($sourceDir===false)
Alexander Makarov committed
51
				die("\nUnable to locate the source directory for \"$type\".\n");
Qiang Xue committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
			$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).'\'';
	}
}