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.
209 lines
5.3 KiB
209 lines
5.3 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
12 years ago
|
* WebappController class file.
|
||
13 years ago
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright © 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\console\controllers;
|
||
|
|
||
|
use yii\console\Controller;
|
||
12 years ago
|
use yii\util\FileHelper;
|
||
12 years ago
|
use yii\base\Exception;
|
||
12 years ago
|
|
||
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
|
* @author Alexander Makarov <sam@rmcreative.ru>
|
||
12 years ago
|
* @since 2.0
|
||
13 years ago
|
*/
|
||
12 years ago
|
class WebappController extends Controller
|
||
13 years ago
|
{
|
||
|
private $_rootPath;
|
||
12 years ago
|
private $_config;
|
||
|
|
||
|
/**
|
||
|
* @var string custom template path. If specified, templates will be
|
||
12 years ago
|
* searched there additionally to `framework/console/webapp`.
|
||
12 years ago
|
*/
|
||
|
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.');
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
12 years ago
|
public function globalOptions()
|
||
|
{
|
||
|
return array('templatesPath', 'type');
|
||
|
}
|
||
|
|
||
|
public function actionIndex()
|
||
|
{
|
||
|
$this->forward('help/index', array('-args' => array('webapp/create')));
|
||
|
}
|
||
|
|
||
13 years ago
|
/**
|
||
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
|
*
|
||
|
* @throws \yii\base\Exception if path specified is not valid
|
||
12 years ago
|
* @return integer the exit status
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function actionCreate($path)
|
||
13 years ago
|
{
|
||
12 years ago
|
$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)) {
|
||
12 years ago
|
throw new Exception("The directory '$path' is not valid. Please make sure the parent directory exists.");
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
if(basename($path) === '.') {
|
||
|
$this->_rootPath = $path = $dir;
|
||
|
}
|
||
|
else {
|
||
|
$this->_rootPath = $path = $dir.DIRECTORY_SEPARATOR.basename($path);
|
||
|
}
|
||
12 years ago
|
|
||
|
if($this->confirm("Create \"$this->type\" application under '$path'?")) {
|
||
|
$sourceDir = $this->getSourceDir();
|
||
|
$config = $this->getConfig();
|
||
|
|
||
12 years ago
|
$list = FileHelper::buildFileList($sourceDir, $path);
|
||
12 years ago
|
|
||
|
if(is_array($config)) {
|
||
|
foreach($config as $file => $settings) {
|
||
|
if(isset($settings['handler'])) {
|
||
|
$list[$file]['callback'] = $settings['handler'];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
FileHelper::copyFiles($list);
|
||
12 years ago
|
|
||
|
if(is_array($config)) {
|
||
|
foreach($config as $file => $settings) {
|
||
|
if(isset($settings['permissions'])) {
|
||
|
@chmod($path.'/'.$file, $settings['permissions']);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
echo "\nYour application has been created successfully under {$path}.\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @throws \yii\base\Exception if source directory wasn't located
|
||
|
* @return string
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function getSourceDir()
|
||
13 years ago
|
{
|
||
12 years ago
|
$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.'".');
|
||
|
}
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @return string default templates path
|
||
12 years ago
|
*/
|
||
12 years ago
|
protected function getDefaultTemplatesPath()
|
||
13 years ago
|
{
|
||
12 years ago
|
return realpath(__DIR__.'/../webapp');
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @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
|
||
12 years ago
|
*
|
||
12 years ago
|
* @return string target file contetns
|
||
12 years ago
|
*/
|
||
12 years ago
|
public function replaceRelativePath($source, $pathTo, $varName)
|
||
13 years ago
|
{
|
||
12 years ago
|
$content = file_get_contents($source);
|
||
12 years ago
|
$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);
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @param string $path1 absolute path
|
||
|
* @param string $path2 absolute path
|
||
12 years ago
|
*
|
||
|
* @return string relative path
|
||
|
*/
|
||
|
protected function getRelativePath($path1, $path2)
|
||
13 years ago
|
{
|
||
12 years ago
|
$segs1 = explode(DIRECTORY_SEPARATOR, $path1);
|
||
|
$segs2 = explode(DIRECTORY_SEPARATOR, $path2);
|
||
|
$n1 = count($segs1);
|
||
|
$n2 = count($segs2);
|
||
13 years ago
|
|
||
12 years ago
|
for($i=0; $i<$n1 && $i<$n2; ++$i) {
|
||
|
if($segs1[$i] !== $segs2[$i]) {
|
||
13 years ago
|
break;
|
||
12 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
if($i===0) {
|
||
13 years ago
|
return "'".$path1."'";
|
||
12 years ago
|
}
|
||
13 years ago
|
$up='';
|
||
12 years ago
|
for($j=$i;$j<$n2-1;++$j) {
|
||
13 years ago
|
$up.='/..';
|
||
12 years ago
|
}
|
||
|
for(; $i<$n1-1; ++$i) {
|
||
13 years ago
|
$up.='/'.$segs1[$i];
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
return '__DIR__.\''.$up.'/'.basename($path1).'\'';
|
||
13 years ago
|
}
|
||
|
}
|