fabrikage/requirements
最新稳定版本:0.2.0
Composer 安装命令:
composer require fabrikage/requirements
包简介
A helper class to set requirements for your PHP application.
README 文档
README
fabrikage/requirements
An elegant solution to check if your PHP application meets the requirements to run on a server.
Requirements
Will be added soon.
Installation with Composer
composer require fabrikage/requirements
Usage
Valid version strings are:
1Major1.0Major and minor1.0.0Major, minor and patch
Or anything that is readable by version_compare().
Valid comparators are:
=Equal to!=Not equal to>Greater than>=Greater than or equal to<Less than<=Less than or equal to
The default comparator, when none is provided is >=.
A version string with a comparator looks like this: >=1.0.0.
Example
require_once __DIR__ . '/vendor/autoload.php'; use Fabrikage\Requirements; use Fabrikage\Requirements\Requirement; $requirements = [ new Requirement\PHP('8.1'), new Requirement\PHPExtension('curl'), new Requirement\WordPress('6.4.1'), new Requirement\WordPressPlugin('woocommerce/woocommerce.php', '8.3.1'), ]; // Pass false to the constructor to disable exception throwing, leave empty to enable. $validator = new Requirements\Validator(false); $validator->addRequirements($requirements); if (!$validator->valid()) { // Your application does not meet the requirements. } // Alternatively, you can invoke the validator as a function. if (!$validator()) { // Your application does not meet the requirements. } App::run();
WordPress example
use Fabrikage\Requirements\Validator; class App { private static Validator $validator; public static function run(Validator $validator): void { if (!$validator->valid()) { static::$validator = $validator; static::handleErrors(); return; // Block further execution } static::init(); } private static function init(): void { // Initialize your application } private static function handleErrors(): void { add_action('admin_notices', function () { static::renderErrors(); }); } private static function renderErrors(): void { if (empty($errors = static::$validator->getErrors())) { return; } echo sprintf( '<div class="notice notice-error"><p><strong>%s</strong></p><p>%s</p><ul>%s</ul></div></p>', PLUGIN_NAME, __('The plugin could not be loaded because the following requirements are not met:', 'plugin-name'), implode('', array_map(function (string $error) { return sprintf('<li>- %s</li>', $error); }, $errors)) ); } }
This would result in the following output if not all requirements are met:
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-09
