PHPackages                             drewm/morse - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. drewm/morse

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

drewm/morse
===========

A PHP feature detection library for code portability

v0.6(10y ago)1601674[1 issues](https://github.com/drewm/morse/issues)MITPHPPHP &gt;=5.3

Since Jun 10Pushed 10y ago9 watchersCompare

[ Source](https://github.com/drewm/morse)[ Packagist](https://packagist.org/packages/drewm/morse)[ RSS](/packages/drewm-morse/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

Morse: the PHP detective-inspector
==================================

[](#morse-the-php-detective-inspector)

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

[![Build Status](https://camo.githubusercontent.com/0824130ebcaa1e5d2184f65f73366a05421cdbb38138862a73dd99d86e31866a/68747470733a2f2f7472617669732d63692e6f72672f647265776d2f6d6f7273652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/drewm/morse)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f52e326295d4dfb5b5c970ecf51dae2f45ec1079f77c2f14cd7919d5467f97b3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f647265776d2f6d6f7273652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/drewm/morse/?branch=master)

Supports PHP 5.3 and up.

Why?
----

[](#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
-------

[](#install)

Either download and include, or install via Composer:

```
composer require drewm/morse

```

How to
------

[](#how-to)

```
use \DrewM\Morse\Morse;

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

Testing if a feature exists
---------------------------

[](#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
---------------------------------

[](#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
--------

[](#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
--------------------------

[](#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

[](#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')

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~17 days

Total

5

Last Release

3926d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/818b5806bb09c97b6150c8756c6abe0afd16c79097c0396a26687d88cc28638d?d=identicon)[drewm](/maintainers/drewm)

---

Top Contributors

[![drewm](https://avatars.githubusercontent.com/u/225402?v=4)](https://github.com/drewm "drewm (61 commits)")[![royopa](https://avatars.githubusercontent.com/u/442991?v=4)](https://github.com/royopa "royopa (7 commits)")

---

Tags

morse

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/drewm-morse/health.svg)

```
[![Health](https://phpackages.com/badges/drewm-morse/health.svg)](https://phpackages.com/packages/drewm-morse)
```

###  Alternatives

[0x46616c6b/etherpad-lite-client

PHP Client for Etherpad Lite

1514.0k1](/packages/0x46616c6b-etherpad-lite-client)[overtrue/laravel-qcloud-federation-token

QCloud COS FederationToken generator for Laravel.

178.5k](/packages/overtrue-laravel-qcloud-federation-token)[jsyqw/potato-bot

poptato chat, potato-bot sdk

191.0k](/packages/jsyqw-potato-bot)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
