donglei3370 2016-07-13 10:49
浏览 51

如何创建一个Cakephp 3 Shell,在没有请求权限的情况下覆盖文件

I'm using Cakephp 3.2, which is awesome!

I have a shell script is very politely asking if I want to overwrite my files -I'm worried the shell won't complete its intended outcome when run as a cron job.

#example of current shell output
File 'webroot/example.json' exists
Do you want to overwrite? (y/n/a/q)

The docs say: "If the shell’s interactive property is false, no question will be asked and the file will simply be overwritten." (http://book.cakephp.org/3.0/en/console-and-shells.html#creating-files) This seems to suggest that any shell can alter the "interactive" property but I haven't found a way to do that.

I could probably change this to false (http://api.cakephp.org/3.2/source-class-Cake.Console.Shell.html#84), but I think that would affect all shells, no thank you.


Example Code:

class ExampleShell extends Shell {

  public function initialize() {
      parent::initialize();
  }

  public function main() {
      $array = [1,2,3];
      $this->createFile('webroot/example.json', json_encode($array));  //this file already exists
  }
}
?>

tl;dr

Please let me know how I can make a single shell script's interactive property set to false so that it won't require a user to allow files to be overwritten. Please provide a code sample if possible, solution must not be applied globally.

Thank you.

  • 写回答

1条回答 默认 最新

  • duanba3707 2016-07-14 08:43
    关注

    Thanks to @AD7six for the comment that helped me see the bigger picture. I was able to replace a line of code and now my cron jobs work!

    //replaced this
    $this->createFile('webroot/example.json', json_encode($array));
    
    //with this
    file_put_contents('webroot/example.json', json_encode($array));
    
    评论

报告相同问题?