dadehpardaz/laravel-executor

可配置的代码,可在安装或更新您的Web应用时运行。

v2.2.0 2020-12-06 00:26 UTC

This package is not auto-updated.

Last update: 2024-09-29 18:09:15 UTC


README

Latest Version on Packagist Build Status Total Downloads PHP from Packagist GitHub license

目录

概述

一个Laravel包,简化了在安装或更新您的Web应用时运行代码和命令。

安装

要求

该包已开发和测试,以确保与以下最低要求兼容

  • PHP 7.2
  • Laravel 6

安装包

您可以通过Composer安装此包

composer require ashallendesign/laravel-executor

用法

创建执行器

创建新的执行器

要创建新的执行器,可以使用以下命令

php artisan make:executor YourExecutorNameHere

上述命令将创建一个名为 YourExecutorNameHere 的执行器,该执行器位于 app/Executor 文件夹中。

使用命令创建执行器

通常,预期执行器将在控制台中运行。因此,在创建新的执行器时,如果您打算在控制台中运行它,可以使用以下命令

php artisan make:executor YourExecutorNameHere -c

上述命令将为您的新的执行器创建与创建新的执行器中命令相同的模板。但是,它将在您的 app/Commands 文件夹中创建一个新的命令,名为 RunYourExecutorNameHereExecutor。这意味着您不需要手动创建新的命令来运行您的执行器。

通过控制台运行中了解更多信息,以了解如何在命令中运行执行器。

更新执行器

添加Artisan命令

要使用您的执行器类通过Artisan命令运行,可以在您的执行器的 run() 方法中添加 runArtisan() 方法。以下代码示例展示了如何设置执行器运行内置的Laravel php artisan cache:clear 命令

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->runArtisan('cache:clear');
    }
}

在某些情况下,您可能希望运行需要您输入的命令。例如,您可能有一个在数据库中创建新用户的命令,并需要您输入一些详细信息。在这种情况下,您可以将 true 作为 ->runArtisan() 方法的第二个参数传递,以指定它是一个交互式命令。

要确定命令的进程超时,您还可以将秒数作为第三个参数传递给 ->runArtisan() 方法。

添加命令

要使用您的执行器类通过外部命令运行,可以在您的执行器的 run() 方法中添加 runExternal() 方法。以下代码示例展示了如何设置执行器运行内置的Composer composer install 命令

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->runExternal('composer install');
    }
}

在某些情况下,您可能希望运行需要您输入的命令。例如,您可能有一个在数据库中创建新用户的命令,并需要您输入一些详细信息。在这种情况下,您可以将 true 作为 ->runExternal() 方法的第二个参数传递,以指定它是一个交互式命令。

要确定命令的进程超时,您还可以将秒数作为第三个参数传递给 ->runExternal() 方法。

添加闭包

有时你可能需要运行一些不适合现有命令的代码。在这种情况下,你可以在Executor中添加一个闭包。以下示例展示了如何向Executor类传递一个简单的闭包。

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->runClosure(function () {
            return 'I am running inside a closure.';
        });
    }
}

添加桌面通知

如果你通过控制台运行Executor,你可能在某些步骤之间想要显示桌面通知。要显示桌面通知,你可以使用->simpleDesktopNotification()->desktopNotification()

使用->simpleDesktopNotification(),你可以传递应显示的标题和正文。以下示例展示了如何创建一个简单的桌面通知。

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->simpleDesktopNotification('Notification title', 'Notification body');
    }
}

如果你想要自定义通知,你可以使用->desktopNotification()并传递一个Joli\JoliNotif\Notification对象作为参数。有关构建此类通知的更多信息,请参阅Joli\JoliNotif文档

你还可以将->completeNotification()添加到Executor中,以便在类中所有代码运行完毕后显示桌面通知。

ping URL

如果你使用Executor在实时服务器上更新应用程序,你可能希望在完成时ping一个URL。这可以用于向Webhook发送警报,告知脚本已成功运行。要ping一个URL,你可以简单地使用->ping()方法。

以下示例展示了如何ping一个网站。

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->ping('https://ashallendesign.co.uk/executor-webhook-route');
    }
}

如果你想在ping()中发送头信息,你可以将它们作为第二个参数传递。如果你想在Webhook请求中添加签名,以确认它们来自授权的发送者,这可能很有用。

以下示例展示了如何带头信息ping一个网站。

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->ping('https://ashallendesign.co.uk/executor-webhook-route', [
            'X-Webhook-Signature' => 'secret-signature-to-go-here'
        ]);
    }
}

运行执行器

通过控制台运行

如上所述,Executors主要用于从控制台运行。这使得它们非常适合添加到部署脚本中,例如在Laravel Forge和Runcloud中找到的脚本。

如果你创建了一个与Executor类同时使用的命令,如使用命令创建Executor中找到的命令,则你的命令已分配了一个签名。签名是通过将Executor的类名转换为短横线命名法来创建的。例如,名称为AppInstall的Executor将获得命令签名executor:app-install

以下示例展示了如何运行未经修改的命令(AppInstall Executor)。

php artisan executor:app-install

注意:要将命令注册到你的Laravel应用程序中,你需要在app/Console/Kernel.php文件中的$commands数组中添加命令类名。

手动运行

有时你可能希望在命令行之外运行Executor类。为此,你只需在你的类上调用->run()方法。以下示例展示了如何手动运行名为AppInstall的Executor。

<?php

namespace App\Http\Controllers;

use App\Executor\AppInstall;

class Controller
{
    public function index()
    {
        (new AppInstall())->run();
    }
}

示例

以下示例展示了如何创建一个Executor类,该类可以在从远程仓库拉取项目的新分支后运行。

<?php

namespace App\Executor;

use AshAllenDesign\LaravelExecutor\Classes\Executor;

class AppUpdate extends Executor
{
    public function run(): Executor
    {
        return $this->simpleDesktopNotification('Starting Executor', 'Starting the AppUpdate Executor.')
                    ->runExternal('composer install')
                    ->runArtisan('migrate')
                    ->runArtisan('cache:clear')
                    ->completeNotification();
    }
}

假设上述Executor类仍在使用默认的命令签名,每次拉取分支时,都可以运行以下命令:php artisan executor:app-update

以下图片展示了如何运行一个简单的Executor命令。它只执行了composer du -o,但展示了Laravel Executor如何提供实时输出和桌面通知。

安全

如果你发现任何安全问题,请直接联系我mail@ashallendesign.co.uk以报告。

贡献

如果你希望对包进行任何更改或改进,请随时提出pull request。

注意:将很快添加贡献指南。

致谢

变更日志

查看变更日志以获取关于最新更改的更多信息。

许可证

MIT 许可协议(MIT)。请参阅许可文件以获取更多信息。