Final Class Yiisoft\Yii\Gii\Component\CodeFile\CodeFile
| Inheritance | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
|---|
CodeFile represents a code file to be generated.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Constructor. | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
| diff() | Returns diff or false if it cannot be calculated | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
| getContent() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getId() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getOperation() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getPath() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getRelativePath() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getState() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| getType() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| preview() | Returns preview or false if it cannot be rendered | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
| save() | Saves the code into the file specified by path. | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
| withBasePath() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| withNewDirMode() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile | |
| withNewFileMode() | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DIR_MODE | 0777 | The new directory mode | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
| FILE_MODE | 0666 | The new file mode | Yiisoft\Yii\Gii\Component\CodeFile\CodeFile |
Method Details
Constructor.
| public mixed __construct ( string $path, string $content ) | ||
| $path | string |
The file path that the new code should be saved to. |
| $content | string |
The newly generated code content. |
public function __construct(string $path, private string $content)
{
$this->path = $this->preparePath($path);
$this->id = dechex(crc32($this->path));
if (is_file($path)) {
if (file_get_contents($path) === $content) {
$this->operation = CodeFileWriteOperationEnum::SKIP;
$this->state = CodeFileStateEnum::PRESENT_SAME;
} else {
$this->operation = CodeFileWriteOperationEnum::SAVE;
$this->state = CodeFileStateEnum::PRESENT_DIFFERENT;
}
}
}
Returns diff or false if it cannot be calculated
| public false|string diff ( ) |
public function diff(): false|string
{
$type = strtolower($this->getType());
if (in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
return false;
}
if ($this->state === CodeFileStateEnum::PRESENT_DIFFERENT) {
return $this->renderDiff(file($this->path), $this->content);
}
return '';
}
| public \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileWriteOperationEnum getOperation ( ) |
public function getOperation(): CodeFileWriteOperationEnum
{
return $this->operation;
}
| public string getRelativePath ( ) | ||
| return | string |
The code file path relative to the application base path. |
|---|---|---|
public function getRelativePath(): string
{
if (!empty($this->basePath) && str_starts_with($this->path, $this->basePath)) {
return substr($this->path, strlen($this->basePath) + 1);
}
return $this->path;
}
| public \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileStateEnum getState ( ) |
public function getState(): CodeFileStateEnum
{
return $this->state;
}
| public string getType ( ) | ||
| return | string |
The code file extension (e.g. php, txt) |
|---|---|---|
public function getType(): string
{
if (($pos = strrpos($this->path, '.')) !== false) {
return substr($this->path, $pos + 1);
}
return 'unknown';
}
Returns preview or false if it cannot be rendered
| public false|string preview ( ) |
public function preview(): false|string
{
if (($pos = strrpos($this->path, '.')) !== false) {
$type = substr($this->path, $pos + 1);
} else {
$type = 'unknown';
}
if ($type === 'php') {
return highlight_string($this->content, true);
}
if (!in_array($type, ['jpg', 'gif', 'png', 'exe'])) {
$content = htmlspecialchars(
$this->content,
ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML5,
'UTF-8'
);
return nl2br($content);
}
return false;
}
Saves the code into the file specified by path.
| public \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileWriteStatusEnum save ( ) | ||
| return | \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileWriteStatusEnum |
The error occurred while saving the code file, or true if no error. |
|---|---|---|
public function save(): CodeFileWriteStatusEnum
{
if ($this->operation === CodeFileWriteOperationEnum::SAVE && $this->state !== CodeFileStateEnum::PRESENT_SAME) {
$dir = dirname($this->path);
if (!is_dir($dir)) {
if ($this->newDirMode !== self::DIR_MODE) {
$mask = @umask(0);
$result = @mkdir($dir, $this->newDirMode, true);
@umask($mask);
} else {
$result = @mkdir($dir, 0777, true);
}
if (!$result) {
throw new RuntimeException("Unable to create the directory '$dir'.");
}
}
}
$status = match ($this->state) {
CodeFileStateEnum::PRESENT_DIFFERENT => CodeFileWriteStatusEnum::OVERWROTE,
CodeFileStateEnum::NOT_EXIST => CodeFileWriteStatusEnum::CREATED,
default => CodeFileWriteStatusEnum::SKIPPED,
};
if (@file_put_contents($this->path, $this->content) === false) {
throw new RuntimeException("Unable to write the file '{$this->path}'.");
}
if ($this->newFileMode !== self::FILE_MODE) {
$mask = @umask(0);
@chmod($this->path, $this->newFileMode);
@umask($mask);
}
return $status;
}
| public self withBasePath ( string $basePath ) | ||
| $basePath | string | |
public function withBasePath(string $basePath): self
{
$new = clone $this;
$new->basePath = $this->preparePath($basePath);
return $new;
}
| public self withNewDirMode ( integer $mode ) | ||
| $mode | integer | |
public function withNewDirMode(int $mode): self
{
$new = clone $this;
$new->newDirMode = $mode;
return $new;
}
| public self withNewFileMode ( integer $mode ) | ||
| $mode | integer | |
public function withNewFileMode(int $mode): self
{
$new = clone $this;
$new->newFileMode = $mode;
return $new;
}
Signup or Login in order to comment.