PHPackages                             rkr/php-ioc-contract - 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. rkr/php-ioc-contract

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

rkr/php-ioc-contract
====================

0.1.3(1y ago)52.7k2MITPHPPHP &gt;= 7.4

Since Nov 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/rkrx/php-ioc-contract)[ Packagist](https://packagist.org/packages/rkr/php-ioc-contract)[ RSS](/packages/rkr-php-ioc-contract/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (11)Used By (2)

php-ioc-contract
================

[](#php-ioc-contract)

A contract for ioc-containers

**Do not use this library as long it is not 1.0.**

Abstract
--------

[](#abstract)

This project provides a documentation for the api and the behavior of three different concerns:

- InstanceContainer - a container that returns instances associated with keys.
- ObjectFactory - an abstract factory to create new object-instances from. E.g. used in factories to create new entities.
- MethodInvoker - invoke a method, function or closure. E.g. used in dispatchers to start a subprogram.

Motivation
----------

[](#motivation)

Lets say you wan't to build a rule-based service-dispatcher (or an http-router like silex), that invoke registered closures only if a certain condition is met:

```
$serviceDispatcher = new ServiceDispatcher();
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function () {
	/* do something every hour */
});
$serviceDispatcher->run();
```

It would be fun, if I already had the domain-objects I need to work with. This would look like this:

```
$serviceDispatcher = new ServiceDispatcher();
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) {
	/* do something every hour */
	$businessObject->doSomething();
});
$serviceDispatcher->run();
```

`$serviceDispatcher` should not be aware of a `BusinessObject` directly. But the `ServiceDispatcher` may know of an generic way to call a `callable` (like a `closure`) and resolve those parameters by a component outside of `ServiceDispatcher`'s scope. How this is archived is not a concern of the `ServiceDispatcher`. It just happens somehow.

The goal could be archived with an Dependency-Injection-Container. There are different ioc-containers out there with quite different interfaces *(my current favorite is [PHP-DI](http://php-di.org/))*. So we need a common interface to pass an instance around which is aware of how to instantiate our domain-objects so that we could directly use them:

```
$container = new Container(require 'config/di-cfg.php');
$serviceDispatcher = new ServiceDispatcher($container);
$serviceDispatcher->register('service-name', 3600 /* timeout sek. */, function (BusinessObject $businessObject) {
	/* do something every hour */
	$businessObject->doSomething();
});
$serviceDispatcher->run();
```

Now the run-method in the `ServiceDispatcher`-implementation could simply look like this:

```
use Ioc\MethodInvoker;

class ServiceDispatcher {
	/** @var MethodInvoker */
	private $methodInvoker;

	/* ... */

	/** @param MethodInvoker $methodInvoker */
	public function __construct(MethodInvoker $methodInvoker) {
		$this->methodInvoker = $methodInvoker;
	}

	/* ... */

	public function run() {
		foreach($this->registry as $serviceName => $service) {
			if($service['lockUntil'] > time()) {
				continue;
			}
			try {
				$this->methodInvoker->invoke($service['fn'], array('serviceName' => $serviceName));
			} finally {
				$service['lockUntil'] = time() + $service['timeout'];
			}
		}
	}
}
```

### InstanceContainer

[](#instancecontainer)

A `InstanceContainer` is mostly useful when in subjection to a di-container only a single instance of an object should be used. This is slightly different to the use of a singleton-pattern since you can have multiple di-containers with different configurations that may inject different implementations for the provided interfaces. For implementation details, look at the [phpdoc-blocks](https://github.com/rkrx/php-ioc-contract/blob/master/src/InstanceContainer.php).

### ObjectFactory

[](#objectfactory)

A `ObjectFactory` is mostly useful in common factories to create entities. For implementation details, look at the [phpdoc-blocks](https://github.com/rkrx/php-ioc-contract/blob/master/src/ObjectFactory.php).

### MethodInvoker

[](#methodinvoker)

Invokes a `callable` method, function or closure and resolve the required parameters automatically of not already provided. For implementation details, look at the [phpdoc-blocks](https://github.com/rkrx/php-ioc-contract/blob/master/src/MethodInvoker.php).

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance46

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~420 days

Recently: every ~946 days

Total

10

Last Release

419d ago

PHP version history (2 changes)0.0.1PHP &gt;= 5.3

0.1.2PHP &gt;= 7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/7f83d7e57a4bf3f1309680dbfbbf2d022f0ee6dae64a4b3bdfbed1226f2f6bef?d=identicon)[rkr](/maintainers/rkr)

---

Top Contributors

[![rkrx](https://avatars.githubusercontent.com/u/5672982?v=4)](https://github.com/rkrx "rkrx (26 commits)")

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rkr-php-ioc-contract/health.svg)

```
[![Health](https://phpackages.com/badges/rkr-php-ioc-contract/health.svg)](https://phpackages.com/packages/rkr-php-ioc-contract)
```

###  Alternatives

[hanson/vbot

高度自定义的微信机器人，能够实现群控制，自动回复，消息转发，防撤回，留言统计等功能

4.4k14.4k12](/packages/hanson-vbot)[pid/speakingurl

Generate of so called 'static' or 'Clean URL' or 'Pretty URL' or 'nice-looking URL' or 'Speaking URL' or 'user-friendly URL' or 'SEO-friendly URL' or 'slug' from a string.

1.1k5.3k1](/packages/pid-speakingurl)[bmidget/kohana-formo

Formo is a form module for Kohana 3 that lets you work with forms as objects

1328.4k](/packages/bmidget-kohana-formo)

PHPackages © 2026

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