簡體   English   中英

如何創建Cakephp 3 Shell,無需征求許可即可覆蓋文件

[英]How to create a Cakephp 3 Shell that overwrites files without asking permission

我正在使用Cakephp 3.2,這太棒了!

我有一個Shell腳本非常有禮貌地詢問我是否要覆蓋我的文件-我擔心Shell作為cron作業運行時將無法完成其預期的結果。

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

文檔說:“ 如果shell的交互屬性為false,則不會提出任何問題,並且文件將被簡單覆蓋。 ”( http://book.cakephp.org/3.0/zh/console-and-shells.html#創建檔案這似乎表明,任何外殼可以改變“互動”的屬性,但我還沒有找到一種方法來做到這一點。

我可能可以將其更改為false( http://api.cakephp.org/3.2/source-class-Cake.Console.Shell.html#84 ),但我認為這會影響所有shell,不用了,謝謝。


示例代碼:

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

請讓我知道如何將單個Shell腳本的交互式屬性設置為false,以便不需要用戶允許覆蓋文件。 請提供一個代碼示例(如果可能的話),該解決方案不得在全球范圍內應用。

謝謝。

感謝@ AD7six的評論幫助我看到了更大的圖景。 我能夠替換一行代碼,現在我的cron作業可以工作了!

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

//with this
file_put_contents('webroot/example.json', json_encode($array));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM