italystrap/platform-requirements-check

检查应用程序的最小和或最大要求

2.0.0 2023-08-06 11:08 UTC

This package is auto-updated.

Last update: 2024-09-06 18:37:24 UTC


README

Build status Latest Stable Version Total Downloads Latest Unstable Version License PHP from Packagist Mutation testing badge

平台要求检查 是一个PHP库,允许您检查PHP项目或插件的系统要求。该库提供了一个简单的接口,可以用于定义和检查系统要求,例如最小和最大PHP版本、PHP扩展和任何其他特定项目要求。

此库支持PHP 7.4或更高版本。如果您需要支持PHP <7.4,可以使用原始的 Minimum Requirement 库,其中此库是从该库分叉出来的。

目录

安装

使用此包的最佳方式是通过Composer

composer require italystrap/platform-requirements-check

此包遵循 SemVer 规范,并且将在次要版本之间完全向后兼容。

基本用法

首先要做的重要事情是在Composer自动加载器之前需要此包中包含的自动加载器文件

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/italystrap/platform-requirements-check/autoload.php';

此文件将为 ItalyStrap\PlatformRequirementsCheck 命名空间注册一个PSR-4自动加载器。您需要在其他任何内容之前需要它,因为此库不能依赖于Composer自动加载器,因为Composer自动加载器本身在应用程序引导过程中加载得太晚,所以您需要先执行PlatformRequirementsCheck,如果所有要求都满足,则可以加载Composer自动加载器。

该库提供了一个 RequirementInterface 接口,您可以使用它来定义系统要求。实现此接口以定义新的要求。

<?php
use ItalyStrap\PlatformRequirementsCheck\RequirementInterface;

class MyRequirement implements RequirementInterface
{
    // implement the interface methods
}

我不知道你的项目,所以我不知道你需要添加自己的要求的所有情况,无论如何,如果你只需要单个要求,使用匿名类可能是一个好主意。

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/italystrap/platform-requirements-check/autoload.php';

use ItalyStrap\PlatformRequirementsCheck\RequirementInterface;

$requirement = new class implements RequirementInterface
{
    // implement the interface methods
}

if (!$requirement->check()) {
    echo $requirement->errorMessage();
}

此外,该库还提供了两个特质,WithNameTraitWithConstraintTrait,可用于简化新要求类的创建。

此外,还有一个 Requirements 类,可用于将一组要求组合在一起并检查。

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/italystrap/platform-requirements-check/autoload.php';

use ItalyStrap\PlatformRequirementsCheck\Requirements;
use ItalyStrap\PlatformRequirementsCheck\RangeVersionRequirement;

$requirements = new Requirements(
    new RangeVersionRequirement(
    'PHP', // Give the name of the requirement, the name can be anything you want as a string
    PHP_VERSION, // The current version to check against
    '7.4', // The minimum version you need, this is optional
    '8.0' // The maximum version you need, this is optional
    ),
    // add other requirements...
);

if (!$requirements->check()) {
    // not all requirements are met, handle this case
    foreach ($requirements->errorMessages() as $errorMessage) {
        echo $errorMessage;
    }
}

RangeVersionRequirement 是一个实现 RequirementInterface 的具体类。此类检查某个组件(例如,PHP、主题或插件)的当前版本是否在版本范围内。使用 RangeVersionRequirement 定义基于版本范围的版本要求。

由于最小和最大版本是可选的,如果您省略了其中之一,省略的版本将被您正在检查的组件的当前版本所取代。

让我们看看一个例子

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/italystrap/platform-requirements-check/autoload.php';

use ItalyStrap\PlatformRequirementsCheck\RangeVersionRequirement;

$current_version = '7.4';

$requirement = new RangeVersionRequirement('PHP', $current_version, '', '8.0');

$requirement->check(); // Is true because the current version is 7.4, the min version is empty so will be replaced with 7.4 and the max version is 8.0, so 7.4 >= 7.4 and 7.4 <= 8.0

#######################################

$current_version = '8.0';

$requirement = new RangeVersionRequirement('PHP', $current_version, '7.4', '');

$requirement->check(); // Is true because the current version is 8.0, the min version is 7.4 and the max version is empty so will be replaced with 8.0, so 8.0 >= 7.4 and 8.0 <= 8.0

#######################################

$current_version = '7.3';

$requirement = new RangeVersionRequirement('PHP', $current_version, '7.4', '8.0');

$requirement->check(); // Is false because the current version is 7.3, the min version is 7.4 and the max version is 8.0, so 7.3 >= 7.4 and 7.3 <= 8.0 is false

你明白了。

现在,最后一件事情,如果您需要检查最大PHP版本小于8.1但大于8.0,因为现在用于检查最大版本的运算符是 '<=',您可以通过将 PHP_INT_MAX 添加到最大版本,如 '8.0.' . PHP_INT_MAX,来实现,所以任何小于8.1但最大大于8.0的版本都是有效的,是的,我知道,这有点hacky,但如果你不喜欢这个,就创建自己的要求类,然后你就完成了。

如果$requirement->check()返回false,则可以使用$requirement->errorMessage()获取错误信息,如果需要的话可以将其打印出来。

<?php
use ItalyStrap\PlatformRequirementsCheck\RangeVersionRequirement;

$requirement = new RangeVersionRequirement('PHP', PHP_VERSION, '7.4', '8.0');

if (!$requirement->check()) {
    echo $requirement->errorMessage();
}

关于基本用法,我想这就是你需要知道的所有内容了。

高级用法

如果你想在WordPress插件中使用这个库,可以这样使用

<?php

declare(strict_types=1);

// ... Main plugin file
/**
 * Plugin Name:       YOUR PLUGIN NAME
 * Plugin URI:        YOUR PLUGIN SITE
 * Description:       YOUR PLUGIN DESCRIPTION
 * Version:           1.0.0
 * Requires at least: 5.3
 * Requires PHP:      8.0.29
 * Author:            YOUR NAME
 * Author URI:        YOUR SITE
 * License:           GPL-2.0-or-later
 * License URI:       https://gnu.ac.cn/licenses/gpl-2.0.html
 * Text Domain:       YOUR TEXT DOMAIN
 * Domain Path:       /languages
 */
 
 // ... Do yout stuff

你可以在主文件中进行检查,或者像我一样创建一个bootstrap.php文件来完成所有其他操作。

<?php

declare(strict_types=1);

// ...bootstrap.php
require __DIR__ . '/vendor/italystrap/platform-requirements-check/autoload.php';

$requirementsList = [
   new \ItalyStrap\PlatformRequirementsCheck\RangeVersionRequirement(
       'PHP',
       \PHP_VERSION,
       (string)$plugin_data['RequiresPHP'],
       '8.0.29'
   ),
   new \ItalyStrap\PlatformRequirementsCheck\RangeVersionRequirement(
       'WP',
       $GLOBALS['wp_version'],
       (string)$plugin_data['RequiresWP'],
       '6.2'
   ),
];

$requirements = (new \ItalyStrap\PlatformRequirementsCheck\Requirements(...$requirementsList));

$requirementsAreFullFilled = $requirements->check();

// If you need you can also call `\register_activation_hook()` here to deactivate the plugin if the requirements are not met as soon as user try to activate the plugin.

// This will show the error messages in the admin area
// Remember to always escape any output before printing it
if (!$requirementsAreFullFilled) {
   \add_action(
       'admin_notices',
       static function () use ($requirements): void {
           ?>
           <div class="notice notice-error">
               <?php foreach ($requirements->errorMessages() as $message): ?>
                   <p><?php \esc_html_e($message); ?></p>
               <?php endforeach; ?>
           </div>
           <?php
       }
   );
}

如果未满足要求,你可以选择禁用插件或主题,或者可以选择向用户显示错误信息,让他们自己决定怎么做,你拥有这个权利。

目前我决定不包含对所需插件的检查,就像原始包所做的那样,如果将来我有时间,我可以实现它。

贡献

欢迎所有反馈/错误报告/拉取请求。

许可证

版权所有(c)2019 Enea Overclokk,ItalyStrap

此代码遵照MIT许可。