【问题标题】:How to create a Cakephp 3 Shell that overwrites files without asking permission如何创建一个无需请求权限即可覆盖文件的 Cakephp 3 Shell
【发布时间】:2016-07-13 10:49:07
【问题描述】:

我使用的是 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/en/console-and-shells.html#creating-files) 这似乎表明任何 shell 都可以更改“交互式”属性,但我还没有找到方法。

我可能会将其更改为 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;博士

请告诉我如何将单个 shell 脚本的交互属性设置为 false,这样就不需要用户允许文件被覆盖。如果可能,请提供代码示例,解决方案不得全局应用。

谢谢。

【问题讨论】:

  • 我建议您简单地尝试一下interactive 属性,只是假设可能发生的事情并不是任何选择的良好基础。
  • 为什么要使用专门设计用于询问用户的功能 - 如果您特别不想这样做?
  • @AD7six 感谢您的回复,我想您是在告诉我,我正在使用 cake shell 方法来创建文件,而我可以只使用 php 方法来创建文件而不是绕过提示用户。我会尝试,我相信它会工作。

标签: php shell cakephp cakephp-3.0


【解决方案1】:

感谢@AD7six 的评论帮助我看到了更大的图景。我能够替换一行代码,现在我的 cron 作业可以工作了!

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2019-10-21
    • 2014-07-19
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多