brightnucleus/phpfeature

PHP Feature Detection Library

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

This package is auto-updated.

Last update: 2024-04-09 15:47:23 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Codacy Badge Code Climate

Latest Stable Version Total Downloads Latest Unstable Version License

PHPFeature is a first draft of a PHP Feature Detection Library similar to what Modernizr does for the browser features. So, instead of checking for a specific PHP version number (which forces you to know which and compare which features were introduced by which versions), you can simply tell the library what features you need, and it will tell you with a simple boolean value whether these are supported or not.

You can read more background information here: http://www.alainschlesser.com/php-feature/

Table Of Contents

Requirements

To develop using the library, you will need PHP 5.3.2+, as it uses Composer to manage its dependencies.

However, the runtime code only requires PHP 5.2+, as one of the goals was to make it useful for WordPress development.

Installation

To include the library in your project, you can use Composer:

composer require brightnucleus/phpfeature

Alternatively, you can copy the classes inside of your application and make sure that your application can find them.

Basic Usage

Usage is pretty simple, it only comes with two methods so far.

public function is_supported( $features );

This returns a boolean value telling you whether all of the features that you passed in are supported by the current PHP version.

public function get_minimum_required( $features );

This returns a SemanticVersion object that contains the minimum PHP version that supports all of the features that you passed in.

Here’s an example of how to use it to stop execution and inform the user:

// 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 )
    ) );
}

Known Issues

  • The library currently uses PHP's version_compare() to make the actual comparison. This should probably be done by a real SemVer comparison algorithm.

  • The list of features is not yet exhaustive. There should also be some guidelines to know how these are named.

  • The algorithm behind get_minimum_required() is still very basic, and NotEqual is not implemented.

Contributing

All feedback / bug reports / pull requests are welcome.

License

This code is released under the MIT license. For the full copyright and license information, please view the LICENSE file distributed with this source code.