CodeFile.php 3.74 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?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;
Qiang Xue committed
12 13
use yii\gii\components\TextDiff;
use yii\helpers\Html;
Qiang Xue committed
14
use yii\helpers\StringHelper;
Qiang Xue committed
15 16

/**
17
 * CodeFile represents a code file to be generated.
Qiang Xue committed
18
 *
19 20 21
 * @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.
22
 *
Qiang Xue committed
23 24 25 26 27
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class CodeFile extends Object
{
28 29 30
	/**
	 * The code file is new.
	 */
31
	const OP_CREATE = 'create';
32 33 34
	/**
	 * The code file already exists, and the new one may need to overwrite it.
	 */
Qiang Xue committed
35
	const OP_OVERWRITE = 'overwrite';
36 37 38
	/**
	 * The new code file and the existing one are identical.
	 */
Qiang Xue committed
39 40
	const OP_SKIP = 'skip';

41 42 43
	/**
	 * @var string an ID that uniquely identifies this code file.
	 */
Qiang Xue committed
44
	public $id;
Qiang Xue committed
45 46 47 48 49
	/**
	 * @var string the file path that the new code should be saved to.
	 */
	public $path;
	/**
50
	 * @var string the newly generated code content
Qiang Xue committed
51 52 53
	 */
	public $content;
	/**
54
	 * @var string the operation to be performed. This can be [[OP_NEW]], [[OP_OVERWRITE]] or [[OP_SKIP]].
Qiang Xue committed
55 56 57 58 59 60
	 */
	public $operation;

	/**
	 * Constructor.
	 * @param string $path the file path that the new code should be saved to.
61
	 * @param string $content the newly generated code content.
Qiang Xue committed
62 63 64
	 */
	public function __construct($path, $content)
	{
Alexander Makarov committed
65
		$this->path = strtr($path, ['/' => DIRECTORY_SEPARATOR, '\\' => DIRECTORY_SEPARATOR]);
Qiang Xue committed
66
		$this->content = $content;
Qiang Xue committed
67
		$this->id = md5($this->path);
Qiang Xue committed
68 69 70
		if (is_file($path)) {
			$this->operation = file_get_contents($path) === $content ? self::OP_SKIP : self::OP_OVERWRITE;
		} else {
71
			$this->operation = self::OP_CREATE;
Qiang Xue committed
72 73 74 75
		}
	}

	/**
76 77
	 * 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.
Qiang Xue committed
78 79 80 81
	 */
	public function save()
	{
		$module = Yii::$app->controller->module;
82
		if ($this->operation === self::OP_CREATE) {
Qiang Xue committed
83 84
			$dir = dirname($this->path);
			if (!is_dir($dir)) {
85
				$mask = @umask(0);
Qiang Xue committed
86
				$result = @mkdir($dir, $module->newDirMode, true);
87
				@umask($mask);
Qiang Xue committed
88
				if (!$result) {
Qiang Xue committed
89
					return "Unable to create the directory '$dir'.";
Qiang Xue committed
90 91 92 93
				}
			}
		}
		if (@file_put_contents($this->path, $this->content) === false) {
Qiang Xue committed
94
			return "Unable to write the file '{$this->path}'.";
Qiang Xue committed
95
		} else {
96
			$mask = @umask(0);
Qiang Xue committed
97
			@chmod($this->path, $module->newFileMode);
98
			@umask($mask);
Qiang Xue committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		}
		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';
		}
	}
Qiang Xue committed
126 127 128 129 130 131 132 133 134 135

	public function preview()
	{
		if (($pos = strrpos($this->path, '.')) !== false) {
			$type = substr($this->path, $pos + 1);
		} else {
			$type = 'unknown';
		}

		if ($type === 'php') {
136
			return highlight_string($this->content, true);
Alexander Makarov committed
137
		} elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
138
			return nl2br(Html::encode($this->content));
Qiang Xue committed
139
		} else {
140
			return false;
Qiang Xue committed
141 142 143 144 145
		}
	}

	public function diff()
	{
146
		$type = strtolower($this->getType());
Alexander Makarov committed
147
		if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
Qiang Xue committed
148 149
			return false;
		} elseif ($this->operation === self::OP_OVERWRITE) {
Qiang Xue committed
150
			return StringHelper::diff(file($this->path), $this->content);
Qiang Xue committed
151 152 153 154
		} else {
			return '';
		}
	}
Qiang Xue committed
155
}