brightnucleus/phpfeature

PHP 功能检测库

v0.2.7 2019-03-23 10:23 UTC

This package is auto-updated.

Last update: 2024-09-09 16:42:31 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Codacy Badge Code Climate

Latest Stable Version Total Downloads Latest Unstable Version License

PHPFeature 是一个类似于 Modernizr 用于浏览器功能的 PHP 功能检测库的初稿。因此,您无需检查特定的 PHP 版本号(这要求您知道哪些版本以及哪些功能被引入),只需简单地向库说明您需要的功能,它就会以一个简单的布尔值告诉您这些功能是否受支持。

您可以在这里了解更多背景信息: http://www.alainschlesser.com/php-feature/

目录

需求

要使用此库进行开发,您需要 PHP 5.3.2+,因为它使用 Composer 来管理其依赖项。

但是,运行时代码只需 PHP 5.2+,因为其中一个目标是为 WordPress 开发提供帮助。

安装

要将库包含到您的项目中,您可以使用 Composer

composer require brightnucleus/phpfeature

或者,您可以将应用程序内部的类复制过来,并确保您的应用程序可以找到它们。

基本用法

用法很简单,目前只有两种方法。

public function is_supported( $features );

这个方法返回一个布尔值,告诉您您传递的所有功能是否都受当前 PHP 版本支持。

public function get_minimum_required( $features );

这个方法返回一个 SemanticVersion 对象,其中包含支持您传递的所有功能的最低 PHP 版本。

以下是一个使用它的示例,以停止执行并通知用户

// Array of strings to define what features you need.
$features = array( 'namespaces', 'traits' );

// Instantiate the PHPFeature library.
// When you don't provide a version number as the first argument,
// the version of the currently used PHP interpreter is fetched.
$php = new PHPFeature();

// Check whether all of the features are supported. If not...
if ( ! $php->is_supported( $features ) ) {

    // ... throw exception and let user know the minimum needed.
    throw new RuntimeException( sprintf(
        'Your PHP interpreter does not support some features needed to run this application. Please upgrade to version %1$s or newer.',
        $php->get_minimum_required( $features )
    ) );
}

已知问题

  • 该库目前使用 PHP 的 version_compare() 进行实际比较。这可能应该通过真正的 SemVer 比较算法来完成。

  • 功能列表尚未详尽。还应有一些指南来了解它们的命名方式。

  • get_minimum_required() 后面的算法仍然非常基础,且 NotEqual 尚未实现。

贡献

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

许可

此代码在 MIT 许可下发布。有关完整的版权和许可信息,请查看与此源代码一起分发的 LICENSE 文件。