yii\gii\CodeFile::save PHP Метод

save() публичный Метод

Saves the code into the file specified by [[path]].
public save ( ) : string | boolean
Результат string | boolean the error occurred while saving the code file, or true if no error.
    public function save()
    {
        $module = Yii::$app->controller->module;
        if ($this->operation === self::OP_CREATE) {
            $dir = dirname($this->path);
            if (!is_dir($dir)) {
                $mask = @umask(0);
                $result = @mkdir($dir, $module->newDirMode, true);
                @umask($mask);
                if (!$result) {
                    return "Unable to create the directory '{$dir}'.";
                }
            }
        }
        if (@file_put_contents($this->path, $this->content) === false) {
            return "Unable to write the file '{$this->path}'.";
        } else {
            $mask = @umask(0);
            @chmod($this->path, $module->newFileMode);
            @umask($mask);
        }
        return true;
    }

Usage Example

Пример #1
0
 public function actionCreateAction($id, $p = null)
 {
     $id = str_replace('/', '\\', $id);
     $arr = explode('\\', $id);
     $file = '';
     $c = '';
     $a = '';
     $fileView = '';
     if (count($arr) == 3) {
         $m = $arr[0];
         $c = ucfirst($arr[1]);
         $c1 = strtolower($arr[1]);
         $a = ucfirst($arr[2]);
         $a1 = strtolower($a);
         $file = \Yii::getAlias("@app/modules/{$m}/controllers/{$c}Controller.php");
         $fileView = \Yii::getAlias("@app/modules/{$m}/views/{$c1}/{$a1}.php");
     } elseif (count($arr) == 2) {
         $c = ucfirst($arr[0]);
         $c1 = strtolower($arr[0]);
         $a = ucfirst($arr[1]);
         $a1 = strtolower($a);
         $file = \Yii::getAlias("@app/controllers/{$c}Controller.php");
         $fileView = \Yii::getAlias("@app/views/{$c1}/{$a1}.php");
     } else {
     }
     if (file_exists($file)) {
         $content = trim(file_get_contents($file));
         if (strpos($content, "action" . $a . "(") === false) {
             $code = $this->getActionTemplate($a, explode(",", $p));
             $new = preg_replace("/}\$/", $code . "\n}", $content);
             file_put_contents($file, $new);
         }
         if (!file_exists($fileView)) {
             $codeFile = new CodeFile($fileView, "<?php \n //{$c}/{$a}");
             $codeFile->save();
         }
     }
 }