CodeFile.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii;
  8. use Yii;
  9. use yii\base\BaseObject;
  10. use yii\gii\components\DiffRendererHtmlInline;
  11. use yii\helpers\Html;
  12. /**
  13. * CodeFile represents a code file to be generated.
  14. *
  15. * @property string $relativePath The code file path relative to the application base path. This property is
  16. * read-only.
  17. * @property string $type The code file extension (e.g. php, txt). This property is read-only.
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. */
  22. class CodeFile extends BaseObject
  23. {
  24. /**
  25. * The code file is new.
  26. */
  27. const OP_CREATE = 'create';
  28. /**
  29. * The code file already exists, and the new one may need to overwrite it.
  30. */
  31. const OP_OVERWRITE = 'overwrite';
  32. /**
  33. * The new code file and the existing one are identical.
  34. */
  35. const OP_SKIP = 'skip';
  36. /**
  37. * @var string an ID that uniquely identifies this code file.
  38. */
  39. public $id;
  40. /**
  41. * @var string the file path that the new code should be saved to.
  42. */
  43. public $path;
  44. /**
  45. * @var string the newly generated code content
  46. */
  47. public $content;
  48. /**
  49. * @var string the operation to be performed. This can be [[OP_CREATE]], [[OP_OVERWRITE]] or [[OP_SKIP]].
  50. */
  51. public $operation;
  52. /**
  53. * Constructor.
  54. * @param string $path the file path that the new code should be saved to.
  55. * @param string $content the newly generated code content.
  56. * @param array $config name-value pairs that will be used to initialize the object properties
  57. */
  58. public function __construct($path, $content, $config = [])
  59. {
  60. parent::__construct($config);
  61. $this->path = strtr($path, '/\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR);
  62. $this->content = $content;
  63. $this->id = md5($this->path);
  64. if (is_file($path)) {
  65. $this->operation = file_get_contents($path) === $content ? self::OP_SKIP : self::OP_OVERWRITE;
  66. } else {
  67. $this->operation = self::OP_CREATE;
  68. }
  69. }
  70. /**
  71. * Saves the code into the file specified by [[path]].
  72. * @return string|bool the error occurred while saving the code file, or true if no error.
  73. */
  74. public function save()
  75. {
  76. $module = isset(Yii::$app->controller) ? Yii::$app->controller->module : null;
  77. if ($this->operation === self::OP_CREATE) {
  78. $dir = dirname($this->path);
  79. if (!is_dir($dir)) {
  80. if ($module instanceof \yii\gii\Module) {
  81. $mask = @umask(0);
  82. $result = @mkdir($dir, $module->newDirMode, true);
  83. @umask($mask);
  84. } else {
  85. $result = @mkdir($dir, 0777, true);
  86. }
  87. if (!$result) {
  88. return "Unable to create the directory '$dir'.";
  89. }
  90. }
  91. }
  92. if (@file_put_contents($this->path, $this->content) === false) {
  93. return "Unable to write the file '{$this->path}'.";
  94. }
  95. if ($module instanceof \yii\gii\Module) {
  96. $mask = @umask(0);
  97. @chmod($this->path, $module->newFileMode);
  98. @umask($mask);
  99. }
  100. return true;
  101. }
  102. /**
  103. * @return string the code file path relative to the application base path.
  104. */
  105. public function getRelativePath()
  106. {
  107. if (strpos($this->path, Yii::$app->basePath) === 0) {
  108. return substr($this->path, strlen(Yii::$app->basePath) + 1);
  109. }
  110. return $this->path;
  111. }
  112. /**
  113. * @return string the code file extension (e.g. php, txt)
  114. */
  115. public function getType()
  116. {
  117. if (($pos = strrpos($this->path, '.')) !== false) {
  118. return substr($this->path, $pos + 1);
  119. }
  120. return 'unknown';
  121. }
  122. /**
  123. * Returns preview or false if it cannot be rendered
  124. *
  125. * @return bool|string
  126. */
  127. public function preview()
  128. {
  129. if (($pos = strrpos($this->path, '.')) !== false) {
  130. $type = substr($this->path, $pos + 1);
  131. } else {
  132. $type = 'unknown';
  133. }
  134. if ($type === 'php') {
  135. return highlight_string($this->content, true);
  136. } elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  137. return nl2br(Html::encode($this->content));
  138. }
  139. return false;
  140. }
  141. /**
  142. * Returns diff or false if it cannot be calculated
  143. *
  144. * @return bool|string
  145. */
  146. public function diff()
  147. {
  148. $type = strtolower($this->getType());
  149. if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  150. return false;
  151. } elseif ($this->operation === self::OP_OVERWRITE) {
  152. return $this->renderDiff(file($this->path), $this->content);
  153. }
  154. return '';
  155. }
  156. /**
  157. * Renders diff between two sets of lines
  158. *
  159. * @param mixed $lines1
  160. * @param mixed $lines2
  161. * @return string
  162. */
  163. private function renderDiff($lines1, $lines2)
  164. {
  165. if (!is_array($lines1)) {
  166. $lines1 = explode("\n", $lines1);
  167. }
  168. if (!is_array($lines2)) {
  169. $lines2 = explode("\n", $lines2);
  170. }
  171. foreach ($lines1 as $i => $line) {
  172. $lines1[$i] = rtrim($line, "\r\n");
  173. }
  174. foreach ($lines2 as $i => $line) {
  175. $lines2[$i] = rtrim($line, "\r\n");
  176. }
  177. $renderer = new DiffRendererHtmlInline();
  178. $diff = new \Diff($lines1, $lines2);
  179. return $diff->render($renderer);
  180. }
  181. }