0 follower

Final Class Yiisoft\Yii\Gii\Component\CodeFile\CodeFile

InheritanceYiisoft\Yii\Gii\Component\CodeFile\CodeFile

CodeFile represents a code file to be generated.

Constants

Hide inherited 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

Hide inherited methods

__construct() public method

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;
        }
    }
}

            
diff() public method

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 '';
}

            
getContent() public method

public string getContent ( )

                public function getContent(): string
{
    return $this->content;
}

            
getId() public method

public string getId ( )

                public function getId(): string
{
    return $this->id;
}

            
getOperation() public method

public \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileWriteOperationEnum getOperation ( )

                public function getOperation(): CodeFileWriteOperationEnum
{
    return $this->operation;
}

            
getPath() public method

public string getPath ( )

                public function getPath(): string
{
    return $this->path;
}

            
getRelativePath() public method

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;
}

            
getState() public method

public \Yiisoft\Yii\Gii\Component\CodeFile\CodeFileStateEnum getState ( )

                public function getState(): CodeFileStateEnum
{
    return $this->state;
}

            
getType() public method

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';
}

            
preview() public method

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;
}

            
save() public method

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;
}

            
withBasePath() public method

public self withBasePath ( string $basePath )
$basePath string

                public function withBasePath(string $basePath): self
{
    $new = clone $this;
    $new->basePath = $this->preparePath($basePath);
    return $new;
}

            
withNewDirMode() public method

public self withNewDirMode ( integer $mode )
$mode integer

                public function withNewDirMode(int $mode): self
{
    $new = clone $this;
    $new->newDirMode = $mode;
    return $new;
}

            
withNewFileMode() public method

public self withNewFileMode ( integer $mode )
$mode integer

                public function withNewFileMode(int $mode): self
{
    $new = clone $this;
    $new->newFileMode = $mode;
    return $new;
}