所有检查均返回false

时间:2019-11-17 14:49:41

标签: php loops docker symfony foreach

我有一些方法可以检查命令的响应,并根据该响应执行其他操作或失败。

我已经到达它检查命令输出并返回false的位置,然后继续执行其余代码。

这是一些代码

public function upgradeImages($images)
{
    $this->pull($images);

    echo "Do next method - Fake destroy\n";

    echo "Do next method - Fake re-build\n";
}

public function pull($images)
{
    // Count how many containers will be attempted to be updated
    $countImages = count($images);

    echo("Updrading $countImages containers...");

    foreach ($images as $key => $image) {
        $key = $key + 1;

        // Is there a new version of the image?
        if ($this->isNewVersion($image)) {
            echo("($key/$countImages) Upgrading '$image'");

            $this->docker('pull ' . $image);
        }

        echo("'$image' is at the latest version");
    }
}

public function isNewVersion($image)
{
    $cmdOutput = (new Process($this->docker('pull ' . $image)));
    $cmdOutput->start();

    $cmdOutput->wait(function ($type, $buffer) {
        if (contains($buffer, 'Image is up to date')) {
            return false;
        }

        return true;
    });
}

这里发生的是,upgradeImage运行pull方法,而pull方法正在检查Docker映像数组,并循环查看该映像是否具有新版本。这是通过运行isNewVersion()方法来完成的,该方法正在检查$buffer的输出并进行比较。

如果isNewVersion()返回true,它将循环浏览并更新图像。

如果isNewVersion()返回false,则会通过一条消息告诉您它是最新的。

如果该方法返回false,则我希望在执行 ALL 检查之后退出循环,而不希望在upgradeImages之后继续运行$this->pull()方法中的代码。如果我在检查后添加exit,它将在第一次检查后退出,而不检查循环的其余部分。

假设我选择了2张图片进行更新,并且都不需要更新 ,则输出如下所示:

Updrading 2 containers...
'composer:latest' is at the latest version
'php:7.2-fpm-alpine' is at the latest version
Fake destroy
Fake re-build

3 个答案:

答案 0 :(得分:1)

方法upgradeImages可以具有多个销毁/重建功能。与其在pull中进行操作,不如创建另一个可以多次调用的方法。我已将其命名为doUpgrade(),并将其传递给$image进行升级。

public function upgradeImages($images)
{
    $this->pull($images);
}

public function doUpgrade($image)
{
    echo "Do next method - Fake destroy\n";
    echo "Do next method - Fake re-build\n";
}

public function pull($images)
{
    // Count how many containers will be attempted to be updated
    $countImages = count($images);

    echo("Updrading $countImages containers...");

    foreach ($images as $key => $image) {
        $key = $key + 1;

        // Is there a new version of the image?
        if ($this->isNewVersion($image)) {
            echo("($key/$countImages) Upgrading '$image'");

            $this->docker('pull ' . $image);

            /* call the destroy/re-build */
            echo("'$image' being upgraded");
            $this->doUpgrade($image);
        }

        echo("'$image' is at the latest version");
    }
}

public function isNewVersion($image)
{
    $cmdOutput = (new Process($this->docker('pull ' . $image)));
    $cmdOutput->start();

    $cmdOutput->wait(function ($type, $buffer) {
        if (contains($buffer, 'Image is up to date')) {
            return false;
        }

        return true;
    });
}

答案 1 :(得分:1)

像这样吗?

public function upgradeImages($images)
{
    $imagesUpdated = $this->pull($images);

    if ($imagesUpdated > 0) {
        echo "Do next method - Fake destroy\n";

        echo "Do next method - Fake re-build\n";

    } else {
        // do nothing or explicit call 'exit' if you want
    }


}

public function pull($images)
{
    // Count how many containers will be attempted to be updated
    $countImages = count($images);
    $countUpdated = 0;
    echo("Updrading $countImages containers...");

    foreach ($images as $key => $image) {
        $key = $key + 1;

        // Is there a new version of the image?
        if ($this->isNewVersion($image)) {
            echo("($key/$countImages) Upgrading '$image'");

            $this->docker('pull ' . $image);
            $countUpdated++;
        }

        echo("'$image' is at the latest version");
    }
    return $countUpdated;
}

public function isNewVersion($image)
{
    $cmdOutput = (new Process($this->docker('pull ' . $image)));
    $cmdOutput->start();

    $cmdOutput->wait(function ($type, $buffer) {
        if (contains($buffer, 'Image is up to date')) {
            return false;
        }

        return true;
    });
}

答案 2 :(得分:1)

根据您的问题(所有检查均返回false后无法退出循环) 如果您希望在满足条件的情况下停止执行,则可以使用Break强制停止执行;否则,请执行以下步骤:

查看更多信息 https://www.php.net/manual/en/control-structures.break.php