CodeFile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 = Yii::$app->controller->module;
  77. if ($this->operation === self::OP_CREATE) {
  78. $dir = dirname($this->path);
  79. if (!is_dir($dir)) {
  80. $mask = @umask(0);
  81. $result = @mkdir($dir, $module->newDirMode, true);
  82. @umask($mask);
  83. if (!$result) {
  84. return "Unable to create the directory '$dir'.";
  85. }
  86. }
  87. }
  88. if (@file_put_contents($this->path, $this->content) === false) {
  89. return "Unable to write the file '{$this->path}'.";
  90. }
  91. $mask = @umask(0);
  92. @chmod($this->path, $module->newFileMode);
  93. @umask($mask);
  94. return true;
  95. }
  96. /**
  97. * @return string the code file path relative to the application base path.
  98. */
  99. public function getRelativePath()
  100. {
  101. if (strpos($this->path, Yii::$app->basePath) === 0) {
  102. return substr($this->path, strlen(Yii::$app->basePath) + 1);
  103. }
  104. return $this->path;
  105. }
  106. /**
  107. * @return string the code file extension (e.g. php, txt)
  108. */
  109. public function getType()
  110. {
  111. if (($pos = strrpos($this->path, '.')) !== false) {
  112. return substr($this->path, $pos + 1);
  113. }
  114. return 'unknown';
  115. }
  116. /**
  117. * Returns preview or false if it cannot be rendered
  118. *
  119. * @return bool|string
  120. */
  121. public function preview()
  122. {
  123. if (($pos = strrpos($this->path, '.')) !== false) {
  124. $type = substr($this->path, $pos + 1);
  125. } else {
  126. $type = 'unknown';
  127. }
  128. if ($type === 'php') {
  129. return highlight_string($this->content, true);
  130. } elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  131. return nl2br(Html::encode($this->content));
  132. }
  133. return false;
  134. }
  135. /**
  136. * Returns diff or false if it cannot be calculated
  137. *
  138. * @return bool|string
  139. */
  140. public function diff()
  141. {
  142. $type = strtolower($this->getType());
  143. if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  144. return false;
  145. } elseif ($this->operation === self::OP_OVERWRITE) {
  146. return $this->renderDiff(file($this->path), $this->content);
  147. }
  148. return '';
  149. }
  150. /**
  151. * Renders diff between two sets of lines
  152. *
  153. * @param mixed $lines1
  154. * @param mixed $lines2
  155. * @return string
  156. */
  157. private function renderDiff($lines1, $lines2)
  158. {
  159. if (!is_array($lines1)) {
  160. $lines1 = explode("\n", $lines1);
  161. }
  162. if (!is_array($lines2)) {
  163. $lines2 = explode("\n", $lines2);
  164. }
  165. foreach ($lines1 as $i => $line) {
  166. $lines1[$i] = rtrim($line, "\r\n");
  167. }
  168. foreach ($lines2 as $i => $line) {
  169. $lines2[$i] = rtrim($line, "\r\n");
  170. }
  171. $renderer = new DiffRendererHtmlInline();
  172. $diff = new \Diff($lines1, $lines2);
  173. return $diff->render($renderer);
  174. }
  175. }