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.
		
		
		
		
			
				
					156 lines
				
				3.7 KiB
			
		
		
			
		
	
	
					156 lines
				
				3.7 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\gii;
 | ||
|  | 
 | ||
|  | use Yii;
 | ||
|  | use yii\base\Object;
 | ||
| 
											12 years ago
										 | use yii\gii\components\TextDiff;
 | ||
|  | use yii\helpers\Html;
 | ||
| 
											12 years ago
										 | use yii\helpers\StringHelper;
 | ||
| 
											12 years ago
										 | 
 | ||
|  | /**
 | ||
| 
											12 years ago
										 |  * CodeFile represents a code file to be generated.
 | ||
| 
											12 years ago
										 |  *
 | ||
| 
											12 years ago
										 |  * @property string $relativePath The code file path relative to the application base path. This property is
 | ||
|  |  * read-only.
 | ||
|  |  * @property string $type The code file extension (e.g. php, txt). This property is read-only.
 | ||
| 
											12 years ago
										 |  *
 | ||
| 
											12 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class CodeFile extends Object
 | ||
|  | {
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * The code file is new.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	const OP_CREATE = 'create';
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * The code file already exists, and the new one may need to overwrite it.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	const OP_OVERWRITE = 'overwrite';
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * The new code file and the existing one are identical.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	const OP_SKIP = 'skip';
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var string an ID that uniquely identifies this code file.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $id;
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var string the file path that the new code should be saved to.
 | ||
|  | 	 */
 | ||
|  | 	public $path;
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @var string the newly generated code content
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public $content;
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @var string the operation to be performed. This can be [[OP_NEW]], [[OP_OVERWRITE]] or [[OP_SKIP]].
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public $operation;
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Constructor.
 | ||
|  | 	 * @param string $path the file path that the new code should be saved to.
 | ||
| 
											12 years ago
										 | 	 * @param string $content the newly generated code content.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public function __construct($path, $content)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$this->path = strtr($path, ['/' => DIRECTORY_SEPARATOR, '\\' => DIRECTORY_SEPARATOR]);
 | ||
| 
											12 years ago
										 | 		$this->content = $content;
 | ||
| 
											12 years ago
										 | 		$this->id = md5($this->path);
 | ||
| 
											12 years ago
										 | 		if (is_file($path)) {
 | ||
|  | 			$this->operation = file_get_contents($path) === $content ? self::OP_SKIP : self::OP_OVERWRITE;
 | ||
|  | 		} else {
 | ||
| 
											12 years ago
										 | 			$this->operation = self::OP_CREATE;
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Saves the code into the file specified by [[path]].
 | ||
|  | 	 * @return string|boolean the error occurred while saving the code file, or true if no error.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public function save()
 | ||
|  | 	{
 | ||
|  | 		$module = Yii::$app->controller->module;
 | ||
| 
											12 years ago
										 | 		if ($this->operation === self::OP_CREATE) {
 | ||
| 
											12 years ago
										 | 			$dir = dirname($this->path);
 | ||
|  | 			if (!is_dir($dir)) {
 | ||
| 
											12 years ago
										 | 				$mask = @umask(0);
 | ||
| 
											12 years ago
										 | 				$result = @mkdir($dir, $module->newDirMode, true);
 | ||
| 
											12 years ago
										 | 				@umask($mask);
 | ||
| 
											12 years ago
										 | 				if (!$result) {
 | ||
| 
											12 years ago
										 | 					return "Unable to create the directory '$dir'.";
 | ||
| 
											12 years ago
										 | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		if (@file_put_contents($this->path, $this->content) === false) {
 | ||
| 
											12 years ago
										 | 			return "Unable to write the file '{$this->path}'.";
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			$mask = @umask(0);
 | ||
| 
											12 years ago
										 | 			@chmod($this->path, $module->newFileMode);
 | ||
| 
											12 years ago
										 | 			@umask($mask);
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the code file path relative to the application base path.
 | ||
|  | 	 */
 | ||
|  | 	public function getRelativePath()
 | ||
|  | 	{
 | ||
|  | 		if (strpos($this->path, Yii::$app->basePath) === 0) {
 | ||
|  | 			return substr($this->path, strlen(Yii::$app->basePath) + 1);
 | ||
|  | 		} else {
 | ||
|  | 			return $this->path;
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the code file extension (e.g. php, txt)
 | ||
|  | 	 */
 | ||
|  | 	public function getType()
 | ||
|  | 	{
 | ||
|  | 		if (($pos = strrpos($this->path, '.')) !== false) {
 | ||
|  | 			return substr($this->path, $pos + 1);
 | ||
|  | 		} else {
 | ||
|  | 			return 'unknown';
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	public function preview()
 | ||
|  | 	{
 | ||
|  | 		if (($pos = strrpos($this->path, '.')) !== false) {
 | ||
|  | 			$type = substr($this->path, $pos + 1);
 | ||
|  | 		} else {
 | ||
|  | 			$type = 'unknown';
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		if ($type === 'php') {
 | ||
| 
											12 years ago
										 | 			return highlight_string($this->content, true);
 | ||
| 
											12 years ago
										 | 		} elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
 | ||
| 
											12 years ago
										 | 			return nl2br(Html::encode($this->content));
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
| 
											12 years ago
										 | 			return false;
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	public function diff()
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$type = strtolower($this->getType());
 | ||
| 
											12 years ago
										 | 		if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
 | ||
| 
											12 years ago
										 | 			return false;
 | ||
|  | 		} elseif ($this->operation === self::OP_OVERWRITE) {
 | ||
| 
											12 years ago
										 | 			return StringHelper::diff(file($this->path), $this->content);
 | ||
| 
											12 years ago
										 | 		} else {
 | ||
|  | 			return '';
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | }
 |