bylexus/php-prereqcheck

用于创建软件包依赖性检查脚本的辅助类

0.2.1 2015-07-14 19:17 UTC

This package is auto-updated.

Last update: 2024-09-14 14:06:03 UTC


README

Build Status

php-prereqcheck

创建依赖性检查的辅助类。它允许您轻松地为项目创建依赖性检查脚本,例如匹配(php)版本、已安装的扩展、设置等。它还允许您编写自己的检查以实现单个检查。

特性

  • 简单的API用于创建自己的依赖性脚本/检查
  • 控制台、Web或静默(程序性)输出
  • 内置检查
    • php版本检查
    • php ini设置检查
    • php扩展检查
    • PDO数据库连接检查
    • 目录可写性检查
  • 可扩展:轻松编写自己的检查类

计划中的特性

  • 输出渲染器以自定义输出
  • 更多内置检查,如HTTP服务可用性、网络访问等

使用composer安装

因为这是一个“零”版本(0.x.y),composer需要一些提示才能安装此包

composer.json:

{
    "minimum-stability": "dev"
}

然后

$ composer require bylexus/php-prereqcheck

或使用--dev标志安装

$ composer require --dev bylexus/php-prereqcheck

不使用composer安装

只需克隆git存储库

git clone https://github.com/bylexus/php-prereqcheck.git

然后只需要求prereq-loader.php文件来设置自动加载器。

示例用法

对于工作示例,请参阅example-usage.php

# Include composer's autoload facility (recommended):
require_once('vendor/autoload.php');

# OR use the own internal autoloader, whatever fits you best:
require_once($here.'/prereq-loader.php');

$pc = new \Prereq\PrereqChecker();

# Check PHP version:
$pc->checkMandatory('php_version','>=','5.3.0');

# Check for installed PHP extensions:
$pc->checkMandatory('php_extension','gd');
$pc->checkMandatory('php_extension','mbstring');
$pc->checkMandatory('php_extension','pdo');

# Check for php.ini settings:
$pc->checkOptional('php_ini','display_errors','off','boolean');
$pc->checkOptional('php_ini','memory_limit','>=256MB','number');
$pc->checkOptional('php_ini','error_reporting',E_STRICT,'bit_enabled');
# check a php.ini string using a regular expression:
$pc->checkOptional('php_ini','date.timezone','/Europe\/.+/','string');

# Check if dir exists and is writable:
$pc->checkMandatory('dir_writable','/tmp/');

# Check if a PDO DB Connection could be established:
$pc->checkOptional('db_pdo_connection',array('dsn'=>'mysql:host=127.0.0.1','username'=>'test','password'=>'test'));

# Create own checks:
class FileExistsChecker extends \Prereq\PrereqCheck {
    public function check($filename = null) {
        $this->name = "File exists: {$filename}";
        if (file_exists($filename)) {
            $this->setSucceed();
        } else {
            $this->setFailed('File does not exists.');
        }
    }
}
$pc->registerCheck('file_exists','FileExistsChecker');
$pc->checkMandatory('file_exists','some_file.txt');

# Each check returns a CheckResult instance:
$res = $pc->checkMandatory('php_version','>=','5.3.0');
if ($res->success()) {
	echo "Yes, your PHP version is compliant.";
}


# did all the checks succeed?
if ($pc->didAllSucceed()) {
    echo "All tests succeeded!\n";
} else {
    echo "Some tests failed. Please check.\n";
}

内置检查

php_version

检查实际PHP版本是否匹配版本比较。

示例

$pc->checkMandatory('php_version','>=','5.3.0');

php_extension

检查给定的PHP扩展是否可用。

示例

$pc->checkMandatory('php_extension','pdo');

php_ini

检查给定的PHP ini设置是否符合标准。由于无法(总是)确定值的类型,配置中需要使用比较函数

示例

$pc->checkOptional('php_ini','date.timezone','Europe/Zurich','string');
$pc->checkOptional('php_ini','date.timezone','/Europe\/.+/','string');
$pc->checkOptional('php_ini','display_errors','off','boolean');
$pc->checkOptional('php_ini','error_reporting',E_STRICT,'bit_enabled');
$pc->checkOptional('php_ini','error_reporting',E_NOTICE,'bit_disabled');
$pc->checkOptional('php_ini','memory_limit','>=128M','number');

可能的比较函数

  • 布尔值:检查给定的值是否为真或假(例如,'Off'表示假)
  • 字符串:精确字符串匹配(例如,default_timezone = 'Europe/Zurich')。如果用 '/' 封装(例如,/search/),则字符串被视为Perl正则表达式。
  • 启用:检查给定的位是否在ini值中设置(例如,检查E_WARNING是否在error_reporting中设置)
  • 位禁用:检查给定的位是否未在ini值中设置(例如,检查E_NOTICE是否在error_reporting中禁用)
  • 数字:比较数字值,例如,如果memory_limit >= 512m。

dir_writable

检查给定的目录是否存在且可写。

示例

$pc->checkMandatory('dir_writable','/tmp/');

db_pdo_connection

检查是否可以建立到数据库的PDO连接。

示例

$pc->checkOptional('db_pdo_connection',array('dsn'=>'mysql:host=127.0.0.1','username'=>'test','password'=>'test'));

注意

选项数组必须包含以下键

  • dsn:PDO dsn
  • username:连接的用户名
  • password:使用的密码

编写自己的检查

编写自己的检查非常简单。只需提供\Prereq\PrereqCheck类并将其与PrereqChecker注册。然后您可以运行定义的检查

# Define a class that extends PrereqCheck and implements the check() function:
class FileExistsChecker extends \Prereq\PrereqCheck {
    public function check($filename = null) {
        $this->name = "File exists: {$filename}";
        if (file_exists($filename)) {
        	# mark check as succeed (default, don't have to be called):
            $this->setSucceed();
        } else {
        	# mark check as failed, add a failure message:
            $this->setFailed('File does not exists.');
        }
    }
}

# Register check with the PrereqChecker:
$pc->registerCheck('file_exists','FileExistsChecker');

# Execute the check:
$pc->checkMandatory('file_exists','some_file.txt');

依赖性(是的,它甚至可以检查自己!)

  • PHP >= 5.3.0

版本历史

  • 0.1.1 第一个版本
  • 0.2.0 引入了命名空间Prereq,并使用Composer自动加载功能。注意:此版本不再与0.1.1兼容!
  • 0.2.1 修复PHP ini数字比较:-1现在计为“无限”或最大整数(例如,支持memory_limit = -1