定制 drewm/morse 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drewm/morse

Composer 安装命令:

composer require drewm/morse

包简介

A PHP feature detection library for code portability

关键字:

README 文档

README

Morse is a feature detection library for PHP code that needs to run in multiple different environments.

Build Status Scrutinizer Code Quality

Supports PHP 5.3 and up.

Why?

Writing PHP that works in unknown (some sometimes hostile) environments is hard. You don't always know what functionality is available to you, so you have to test for it. Morse is a library to encapsulate those tests.

Most tests are really simple - just a function_exists() or similar - but you can often end up needing to repeat that test over and over across your codebase. Morse centralises those tests, providing reusability and consistancy.

Some tests aren't so simple, perhaps due to that one weird PHP bug or unusual hosting configurations or whatever. You have to do a weird dance to check if something is really going to work. Morse takes care of that, and keeps the weird dancing out of your application code, safe from the next developer who thinks it's dumb and rips it out.

Install

Either download and include, or install via Composer:

composer require drewm/morse

How to

use \DrewM\Morse\Morse;

if (Morse::featureExists('http/curl')) {
	// use curl
}else{
	// use sockets
}

Testing if a feature exists

Morse::featureExists('group/feature');

Features that may be available in either newer class support or older function support can return a value of Morse::CLASS_SUPPORT or Morse::FUNCTION_SUPPORT, both of which are truthy.

So this works:

if (Morse::featureExists('file/finfo')) {
	...
}

but equally:

switch(Morse::featureExists('file/finfo')) {
	case Morse::CLASS_SUPPORT:
		$finfo = new finfo(...);
		break;

	case Morse::FUNCTION_SUPPORT:
		$finfo = finfo_open(...);
		break;

	default:
		die('No finfo support!');
		break;
}

If class support is found, this returns regardless of function support.

Finding the first match in a list

$best_match = Morse::getFirstAvailable(['image/gd', 'image/imagick']);

or

$best_match = Morse::getFirstAvailable([
					'image/gd' => 'gd',
					'image/imagick' => 'imagick'
				]);

switch($best_match) {

	case 'gd':
		...
		break;

	case 'imagick':
		...
		break;
}

Features

Feature detection tests currently exist for the following:

  • cache
    • apc
    • memcache
    • memcached
    • opcache
  • crypto
    • mcrypt
    • openssl
    • password
  • data
    • json
  • db
    • mysqli
    • pdo
    • pdo-mysql
    • pdo-pgsql
    • pdo-sqlite
  • file
    • finfo
    • zip
  • http
    • curl
    • filter
    • sockets
  • image
    • gd
    • imagick
  • number
    • bigint
  • protocol
    • ldap
  • system
    • exec
    • ignore_user_abort
    • ini_set
    • passthru
    • popen
    • proc_open
    • set_time_limit
    • shell_exec
    • system
  • text
    • ctype
    • iconv
    • intl
    • multibyte
    • transliterate

Contributing feature tests

Feature tests are functions in the appropriate class that return true or false to indicate support for a feature.

Let's say you wanted to add a feature detection test for a database called Pongo. You would test for it with the feature identifier db/pongo, which would map to a function called testPongo in the Feature/Db.php class file.

Both half of the feature identifier are run through ucwords() to correct case. Dashes are changed to underscores. So db/pongo-panda would map to Feature\Db::testPongo_Panda.

namespace DrewM\Morse\Feature;

class Db extends \DrewM\Morse\Feature
{
	public function testPongo_Panda()
	{
		// do whatever needs to be done to determine support
		// return true for support, false for no support;
		return true;
	}
}

If a feature can exist in both OO-style classes and procedural-style function form, return \DrewM\Morse\Morse::CLASS_SUPPORT or \DrewM\Morse\Morse::FUNCTION_SUPPORT as your truthy value. Check for classes first.

namespace DrewM\Morse\Feature;

class Db extends \DrewM\Morse\Feature
{
	public function testPongo_Panda()
	{
		if (class_exists('PongoPanda')) {
			return \DrewM\Morse\Morse::CLASS_SUPPORT;
		}

		if (self::functionAvailable('pongo_panda')) {
			return \DrewM\Morse\Morse::FUNCTION_SUPPORT;
		}

		return false;
	}
}

Feature classes should be big concepts (image, text, database) and the tests themselves should be specific features.

Please write a corresponding PHPUnit test for the feature you're adding. Note that you can't rely on the environment, so just test that the detection works and returns a sane value.

Testing for function availability

PHP gives us function_exists() for testing if a function has been declared. In some circumstances (e.g. when suhosin blacklisting is invoked), this can return true even if the function has been disabled and isn't available for use. Therefore, do the following within a feature class to detect whether a function is both declared and not disabled:

self::functionAvailable('pongo_panda')

drewm/morse 适用场景与选型建议

drewm/morse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 167 次下载、GitHub Stars 达 160, 最近一次更新时间为 2015 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「morse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 drewm/morse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drewm/morse 我们能提供哪些服务?
定制开发 / 二次开发

基于 drewm/morse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 167
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 160
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 160
  • Watchers: 9
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-24