PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/simpus/vendor/yiisoft/yii2-gii/CodeFile.php

https://gitlab.com/isdzulqor/Slis-Dev
PHP | 194 lines | 147 code | 6 blank | 41 comment | 0 complexity | c8d363b50c2f137c05987f7f0851226f MD5 | raw file
  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\Object;
  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 Object
  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. */
  57. public function __construct($path, $content)
  58. {
  59. $this->path = strtr($path, '/\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR);
  60. $this->content = $content;
  61. $this->id = md5($this->path);
  62. if (is_file($path)) {
  63. $this->operation = file_get_contents($path) === $content ? self::OP_SKIP : self::OP_OVERWRITE;
  64. } else {
  65. $this->operation = self::OP_CREATE;
  66. }
  67. }
  68. /**
  69. * Saves the code into the file specified by [[path]].
  70. * @return string|boolean the error occurred while saving the code file, or true if no error.
  71. */
  72. public function save()
  73. {
  74. $module = Yii::$app->controller->module;
  75. if ($this->operation === self::OP_CREATE) {
  76. $dir = dirname($this->path);
  77. if (!is_dir($dir)) {
  78. $mask = @umask(0);
  79. $result = @mkdir($dir, $module->newDirMode, true);
  80. @umask($mask);
  81. if (!$result) {
  82. return "Unable to create the directory '$dir'.";
  83. }
  84. }
  85. }
  86. if (@file_put_contents($this->path, $this->content) === false) {
  87. return "Unable to write the file '{$this->path}'.";
  88. } else {
  89. $mask = @umask(0);
  90. @chmod($this->path, $module->newFileMode);
  91. @umask($mask);
  92. }
  93. return true;
  94. }
  95. /**
  96. * @return string the code file path relative to the application base path.
  97. */
  98. public function getRelativePath()
  99. {
  100. if (strpos($this->path, Yii::$app->basePath) === 0) {
  101. return substr($this->path, strlen(Yii::$app->basePath) + 1);
  102. } else {
  103. return $this->path;
  104. }
  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. } else {
  114. return 'unknown';
  115. }
  116. }
  117. /**
  118. * Returns preview or false if it cannot be rendered
  119. *
  120. * @return boolean|string
  121. */
  122. public function preview()
  123. {
  124. if (($pos = strrpos($this->path, '.')) !== false) {
  125. $type = substr($this->path, $pos + 1);
  126. } else {
  127. $type = 'unknown';
  128. }
  129. if ($type === 'php') {
  130. return highlight_string($this->content, true);
  131. } elseif (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  132. return nl2br(Html::encode($this->content));
  133. } else {
  134. return false;
  135. }
  136. }
  137. /**
  138. * Returns diff or false if it cannot be calculated
  139. *
  140. * @return boolean|string
  141. */
  142. public function diff()
  143. {
  144. $type = strtolower($this->getType());
  145. if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
  146. return false;
  147. } elseif ($this->operation === self::OP_OVERWRITE) {
  148. return $this->renderDiff(file($this->path), $this->content);
  149. } else {
  150. return '';
  151. }
  152. }
  153. /**
  154. * Renders diff between two sets of lines
  155. *
  156. * @param mixed $lines1
  157. * @param mixed $lines2
  158. * @return string
  159. */
  160. private function renderDiff($lines1, $lines2)
  161. {
  162. if (!is_array($lines1)) {
  163. $lines1 = explode("\n", $lines1);
  164. }
  165. if (!is_array($lines2)) {
  166. $lines2 = explode("\n", $lines2);
  167. }
  168. foreach ($lines1 as $i => $line) {
  169. $lines1[$i] = rtrim($line, "\r\n");
  170. }
  171. foreach ($lines2 as $i => $line) {
  172. $lines2[$i] = rtrim($line, "\r\n");
  173. }
  174. $renderer = new DiffRendererHtmlInline();
  175. $diff = new \Diff($lines1, $lines2);
  176. return $diff->render($renderer);
  177. }
  178. }