webqamdev/laravel-self-diagnosis

在您的Laravel应用程序上执行各种自我诊断测试。

1.0 2023-12-10 14:09 UTC

This package is auto-updated.

Last update: 2024-09-10 15:58:33 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

此包允许您在Laravel应用程序上运行自我诊断测试。它自带了多个检查,并允许您自行添加自定义检查。

以下是命令的示例输出

All Checks passed

包含的检查

  • 是否已设置 APP_KEY?
  • 您的 composer 依赖项是否与 composer.lock 文件同步更新?
  • 您是否安装了正确的 PHP 版本?
  • 您是否安装了正确的 PHP 扩展?
  • 能否建立到数据库的连接?
  • 是否为 storagebootstrap/cache 目录设置了正确的权限?
  • 是否存在 .env 文件?
  • 维护模式是否已禁用?
  • 系统上是否安装了所需的区域设置?
  • .env.example 中存在但在 .env 中不存在的环境变量吗?
  • 是否有需要运行的迁移?
  • 存储目录是否已链接?
  • 能否访问 Redis?

开发环境检查

  • 配置是否未缓存?
  • 路由是否未缓存?
  • .env 中存在但在 .env.example 中不存在的环境变量吗?

生产环境检查

  • 配置是否已缓存?
  • 路由是否已缓存?
  • xdebug PHP 扩展是否已禁用?
  • 是否将 APP_DEBUG 设置为 false?
  • 是否可以访问某些服务器?
  • 是否正在运行某些守护进程程序?

安装

您可以通过 composer 安装此包

composer require beyondcode/laravel-self-diagnosis

如果您使用的是 Laravel 5.5+,则将自动为您注册 SelfDiagnosisServiceProvider

使用方法

只需调用 artisan 命令以开始检查

php artisan self-diagnosis

自定义

您可以使用以下命令发布配置文件,该文件包含所有可用的检查:

php artisan vendor:publish --provider=BeyondCode\\SelfDiagnosis\\SelfDiagnosisServiceProvider

这将发布一个位于您的 config 文件夹中的 self-diagnosis.php 文件。此文件包含将在您的应用程序上执行的所有检查。

<?php

return [

    /*
     * A list of environment aliases mapped to the actual environment configuration.
     */
    'environment_aliases' => [
        'prod' => 'production',
        'live' => 'production',
        'local' => 'development',
    ],

    /*
     * Common checks that will be performed on all environments.
     */
    'checks' => [
        \BeyondCode\SelfDiagnosis\Checks\AppKeyIsSet::class,
        \BeyondCode\SelfDiagnosis\Checks\CorrectPhpVersionIsInstalled::class,
        \BeyondCode\SelfDiagnosis\Checks\DatabaseCanBeAccessed::class => [
            'default_connection' => true,
            'connections' => [],
        ],
        \BeyondCode\SelfDiagnosis\Checks\DirectoriesHaveCorrectPermissions::class => [
            'directories' => [
                storage_path(),
                base_path('bootstrap/cache'),
            ],
        ],
        \BeyondCode\SelfDiagnosis\Checks\EnvFileExists::class,
        \BeyondCode\SelfDiagnosis\Checks\ExampleEnvironmentVariablesAreSet::class,
        \BeyondCode\SelfDiagnosis\Checks\LocalesAreInstalled::class => [
            'required_locales' => [
                'en_US',
                'en_US.utf8',
            ],
        ],
        \BeyondCode\SelfDiagnosis\Checks\MaintenanceModeNotEnabled::class,
        \BeyondCode\SelfDiagnosis\Checks\MigrationsAreUpToDate::class,
        \BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreInstalled::class => [
            'extensions' => [
                'openssl',
                'PDO',
                'mbstring',
                'tokenizer',
                'xml',
                'ctype',
                'json',
            ],
            'include_composer_extensions' => true,
        ],
        \BeyondCode\SelfDiagnosis\Checks\StorageDirectoryIsLinked::class,
    ],

    /*
     * Environment specific checks that will only be performed for the corresponding environment.
     */
    'environment_checks' => [
        'development' => [
            \BeyondCode\SelfDiagnosis\Checks\ComposerWithDevDependenciesIsUpToDate::class => [
                'additional_options' => '--ignore-platform-reqs',
            ],
            \BeyondCode\SelfDiagnosis\Checks\ConfigurationIsNotCached::class,
            \BeyondCode\SelfDiagnosis\Checks\RoutesAreNotCached::class,
        ],
        'production' => [
            \BeyondCode\SelfDiagnosis\Checks\ComposerWithoutDevDependenciesIsUpToDate::class => [
                'additional_options' => '--ignore-platform-reqs',
            ],
            \BeyondCode\SelfDiagnosis\Checks\ConfigurationIsCached::class,
            \BeyondCode\SelfDiagnosis\Checks\DebugModeIsNotEnabled::class,
            \BeyondCode\SelfDiagnosis\Checks\PhpExtensionsAreDisabled::class => [
                'extensions' => [
                    'xdebug',
                ],
            ],
            \BeyondCode\SelfDiagnosis\Checks\RedisCanBeAccessed::class => [
                'default_connection' => true,
                'connections' => [],
            ],
            \BeyondCode\SelfDiagnosis\Checks\RoutesAreCached::class,
            \BeyondCode\SelfDiagnosis\Checks\ServersArePingable::class => [
                'servers' => [
                    'www.google.com',
                    ['host' => 'www.google.com', 'port' => 8080],
                    '8.8.8.8',
                    ['host' => '8.8.8.8', 'port' => 8080, 'timeout' => 5],
                ],
            ],
            \BeyondCode\SelfDiagnosis\Checks\SupervisorProgramsAreRunning::class => [
                'programs' => [
                    'horizon',
                ],
                'restarted_within' => 300, // max seconds since last restart, 0 to disable check
            ],
        ],
    ],

];

可用的配置选项

以下选项适用于单个检查

自定义检查

您可以通过实现BeyondCode\SelfDiagnosis\Checks\Check接口并将类添加到配置文件中来创建自定义检查。如下所示

<?php

use BeyondCode\SelfDiagnosis\Checks\Check;

class MyCustomCheck implements Check
{
    /**
     * The name of the check.
     *
     * @param array $config
     * @return string
     */
    public function name(array $config): string
    {
        return 'My custom check.';
    }

    /**
     * Perform the actual verification of this check.
     *
     * @param array $config
     * @return bool
     */
    public function check(array $config): bool
    {
        return true;
    }

    /**
     * The error message to display in case the check does not pass.
     *
     * @param array $config
     * @return string
     */
    public function message(array $config): string
    {
        return 'This is the error message that users see if "check" returns false.';
    }
}

示例输出

Some Checks failed

测试

composer test

变更日志

请参阅CHANGELOG以获取更多信息,了解最近有哪些变化。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件marcel@beyondco.de联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件