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.
		
		
		
		
			
				
					317 lines
				
				9.1 KiB
			
		
		
			
		
	
	
					317 lines
				
				9.1 KiB
			| 
								 
											14 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								namespace yii\console\controllers;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use yii\console\Controller;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\base\Exception;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * This command creates an Yii Web application at the specified location.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Qiang Xue <qiang.xue@gmail.com>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @author Alexander Makarov <sam@rmcreative.ru>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @since 2.0
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								class AppController extends Controller
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									private $_rootPath;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									private $_config;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var string custom template path. If specified, templates will be
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * searched there additionally to `framework/console/webapp`.
							 | 
						||
| 
								 
											13 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();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->templatesPath && !is_dir($this->templatesPath)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											throw new Exception('--templatesPath "'.$this->templatesPath.'" does not exist or can not be read.');
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function globalOptions()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return array('templatesPath', 'type');
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									public function actionIndex()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$this->forward('help/index', array('-args' => array('app/create')));
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Generates Yii application at the path specified via appPath parameter.
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param string $path the directory where the new application will be created.
							 | 
						||
| 
								 
											13 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.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * @throws \yii\base\Exception if path specified is not valid
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return integer the exit status
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function actionCreate($path)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$path = strtr($path, '/\\', DIRECTORY_SEPARATOR);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (strpos($path, DIRECTORY_SEPARATOR) === false) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$path = '.'.DIRECTORY_SEPARATOR.$path;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										$dir = rtrim(realpath(dirname($path)), '\\/');
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($dir === false || !is_dir($dir)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											throw new Exception("The directory '$path' is not valid. Please make sure the parent directory exists.");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (basename($path) === '.') {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->_rootPath = $path = $dir;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->_rootPath = $path = $dir.DIRECTORY_SEPARATOR.basename($path);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->confirm("Create \"$this->type\" application under '$path'?")) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$sourceDir = $this->getSourceDir();
							 | 
						||
| 
								 | 
							
											$config = $this->getConfig();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$list = $this->buildFileList($sourceDir, $path);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (is_array($config)) {
							 | 
						||
| 
								 | 
							
												foreach ($config as $file => $settings) {
							 | 
						||
| 
								 | 
							
													if (isset($settings['handler'])) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														$list[$file]['callback'] = $settings['handler'];
							 | 
						||
| 
								 | 
							
													}
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$this->copyFiles($list);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (is_array($config)) {
							 | 
						||
| 
								 | 
							
												foreach ($config as $file => $settings) {
							 | 
						||
| 
								 | 
							
													if (isset($settings['permissions'])) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														@chmod($path.'/'.$file, $settings['permissions']);
							 | 
						||
| 
								 | 
							
													}
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											echo "\nYour application has been created successfully under {$path}.\n";
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @throws \yii\base\Exception if source directory wasn't located
							 | 
						||
| 
								 | 
							
									 * @return string
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function getSourceDir()
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$customSource = realpath($this->templatesPath.'/'.$this->type);
							 | 
						||
| 
								 | 
							
										$defaultSource = realpath($this->getDefaultTemplatesPath().'/'.$this->type);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($customSource) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											return $customSource;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										} elseif ($defaultSource) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											return $defaultSource;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											throw new Exception('Unable to locate the source directory for "'.$this->type.'".');
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return string default templates path
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function getDefaultTemplatesPath()
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return realpath(__DIR__.'/../webapp');
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return array|null template configuration
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function getConfig()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($this->_config === null) {
							 | 
						||
| 
								 | 
							
											$this->_config = require $this->getDefaultTemplatesPath() . '/config.php';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if ($this->templatesPath && file_exists($this->templatesPath)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$this->_config = array_merge($this->_config, require $this->templatesPath . '/config.php');
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if (isset($this->_config[$this->type])) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											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
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return string target file contents
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function replaceRelativePath($source, $pathTo, $varName)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$content = file_get_contents($source);
							 | 
						||
| 
								 
											13 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);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param string $path1 absolute path
							 | 
						||
| 
								 | 
							
									 * @param string $path2 absolute path
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 *
							 | 
						||
| 
								 | 
							
									 * @return string relative path
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function getRelativePath($path1, $path2)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$segs1 = explode(DIRECTORY_SEPARATOR, $path1);
							 | 
						||
| 
								 | 
							
										$segs2 = explode(DIRECTORY_SEPARATOR, $path2);
							 | 
						||
| 
								 | 
							
										$n1 = count($segs1);
							 | 
						||
| 
								 | 
							
										$n2 = count($segs2);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										for ($i = 0; $i < $n1 && $i < $n2; ++$i) {
							 | 
						||
| 
								 | 
							
											if ($segs1[$i] !== $segs2[$i]) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												break;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										if ($i === 0) {
							 | 
						||
| 
								 | 
							
											return "'" . $path1 . "'";
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$up = '';
							 | 
						||
| 
								 | 
							
										for ($j = $i; $j < $n2 - 1; ++$j) {
							 | 
						||
| 
								 | 
							
											$up .= '/..';
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										for(; $i < $n1 - 1; ++$i) {
							 | 
						||
| 
								 | 
							
											$up .= '/' . $segs1[$i];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return '__DIR__.\'' . $up . '/' . basename($path1) . '\'';
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Copies a list of files from one place to another.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param array $fileList the list of files to be copied (name => spec).
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * The array keys are names displayed during the copy process, and array values are specifications
							 | 
						||
| 
								 | 
							
									 * for files to be copied. Each array value must be an array of the following structure:
							 | 
						||
| 
								 | 
							
									 * <ul>
							 | 
						||
| 
								 | 
							
									 * <li>source: required, the full path of the file/directory to be copied from</li>
							 | 
						||
| 
								 | 
							
									 * <li>target: required, the full path of the file/directory to be copied to</li>
							 | 
						||
| 
								 | 
							
									 * <li>callback: optional, the callback to be invoked when copying a file. The callback function
							 | 
						||
| 
								 | 
							
									 *   should be declared as follows:
							 | 
						||
| 
								 | 
							
									 *   <pre>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 *   function foo($source, $params)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 *   </pre>
							 | 
						||
| 
								 | 
							
									 *   where $source parameter is the source file path, and the content returned
							 | 
						||
| 
								 | 
							
									 *   by the function will be saved into the target file.</li>
							 | 
						||
| 
								 | 
							
									 * <li>params: optional, the parameters to be passed to the callback</li>
							 | 
						||
| 
								 | 
							
									 * </ul>
							 | 
						||
| 
								 | 
							
									 * @see buildFileList
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function copyFiles($fileList)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$overwriteAll = false;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										foreach ($fileList as $name => $file) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$source = strtr($file['source'], '/\\', DIRECTORY_SEPARATOR);
							 | 
						||
| 
								 | 
							
											$target = strtr($file['target'], '/\\', DIRECTORY_SEPARATOR);
							 | 
						||
| 
								 | 
							
											$callback = isset($file['callback']) ? $file['callback'] : null;
							 | 
						||
| 
								 | 
							
											$params = isset($file['params']) ? $file['params'] : null;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (is_dir($source)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												if (!is_dir($target)) {
							 | 
						||
| 
								 | 
							
													mkdir($target, 0777, true);
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
												continue;
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if ($callback !== null) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$content = call_user_func($callback, $source, $params);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$content = file_get_contents($source);
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (is_file($target)) {
							 | 
						||
| 
								 | 
							
												if ($content === file_get_contents($target)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													echo "  unchanged $name\n";
							 | 
						||
| 
								 | 
							
													continue;
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												if ($overwriteAll) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													echo "  overwrite $name\n";
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
												else {
							 | 
						||
| 
								 | 
							
													echo "      exist $name\n";
							 | 
						||
| 
								 | 
							
													echo "            ...overwrite? [Yes|No|All|Quit] ";
							 | 
						||
| 
								 | 
							
													$answer = trim(fgets(STDIN));
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													if (!strncasecmp($answer, 'q', 1)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														return;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													} elseif (!strncasecmp($answer, 'y', 1)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														echo "  overwrite $name\n";
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													} elseif (!strncasecmp($answer, 'a', 1)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														echo "  overwrite $name\n";
							 | 
						||
| 
								 | 
							
														$overwriteAll = true;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													} else {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
														echo "       skip $name\n";
							 | 
						||
| 
								 | 
							
														continue;
							 | 
						||
| 
								 | 
							
													}
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											else {
							 | 
						||
| 
								 | 
							
												if (!is_dir(dirname($target))) {
							 | 
						||
| 
								 | 
							
													mkdir(dirname($target), 0777, true);
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
												echo "   generate $name\n";
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											file_put_contents($target, $content);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Builds the file list of a directory.
							 | 
						||
| 
								 | 
							
									 * This method traverses through the specified directory and builds
							 | 
						||
| 
								 | 
							
									 * a list of files and subdirectories that the directory contains.
							 | 
						||
| 
								 | 
							
									 * The result of this function can be passed to {@link copyFiles}.
							 | 
						||
| 
								 | 
							
									 * @param string $sourceDir the source directory
							 | 
						||
| 
								 | 
							
									 * @param string $targetDir the target directory
							 | 
						||
| 
								 | 
							
									 * @param string $baseDir base directory
							 | 
						||
| 
								 | 
							
									 * @param array $ignoreFiles list of the names of files that should
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * be ignored in list building process.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @param array $renameMap hash array of file names that should be
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * renamed. Example value: array('1.old.txt' => '2.new.txt').
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return array the file list (see {@link copyFiles})
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function buildFileList($sourceDir, $targetDir, $baseDir='', $ignoreFiles=array(), $renameMap=array())
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$list = array();
							 | 
						||
| 
								 | 
							
										$handle = opendir($sourceDir);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										while (($file = readdir($handle)) !== false) {
							 | 
						||
| 
								 | 
							
											if (in_array($file, array('.', '..', '.svn', '.gitignore', '.hgignore')) || in_array($file, $ignoreFiles)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												continue;
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											$sourcePath = $sourceDir.DIRECTORY_SEPARATOR.$file;
							 | 
						||
| 
								 | 
							
											$targetPath = $targetDir.DIRECTORY_SEPARATOR.strtr($file, $renameMap);
							 | 
						||
| 
								 | 
							
											$name = $baseDir === '' ? $file : $baseDir.'/'.$file;
							 | 
						||
| 
								 | 
							
											$list[$name] = array(
							 | 
						||
| 
								 | 
							
												'source' => $sourcePath,
							 | 
						||
| 
								 | 
							
												'target' => $targetPath,
							 | 
						||
| 
								 | 
							
											);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (is_dir($sourcePath)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$list = array_merge($list, self::buildFileList($sourcePath, $targetPath, $name, $ignoreFiles, $renameMap));
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										closedir($handle);
							 | 
						||
| 
								 | 
							
										return $list;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 |