PHPackages                             intraworlds/service-container - 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. intraworlds/service-container

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

intraworlds/service-container
=============================

Lightweight yet powerful implementation of dependancy injection container with autowiring

v0.8.4(2y ago)075.8k↓50%1[1 PRs](https://github.com/peoplepath/service-container/pulls)MITPHPPHP ^8.0CI failing

Since Nov 6Pushed 5mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (6)Versions (24)Used By (0)

Lightweight yet powerful implementation of dependancy injection container with autowiring.

Installation
------------

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
composer require intraworlds/service-container

```

Usage
-----

[](#usage)

The container is implementing standard PSR-11 interface. You can use it with autowiring out-of-the-box.

Let's say that we're building a cache client. This client will not implement a cache directly but only provides common API - it will depend on given adapter.

We'll start will start with defining memory adapter

```
namespace Acme\Cache;

class MemoryAdapter {
  function get(string $key): string {}
  function set(string $key, string $string): void {}
}
```

Cache client

```
namespace Acme\Cache;

class Client {
  private $adapter;

  function __construct(MemoryAdapter $adapter) {
    $this->adapter = $adapter;
  }

  function get(string $key) {
    $string = $this->adapter->get($key);
    return unserialize($string);
  }

  function set(string $key, $value): void {
    $string = serialize($value);
    $this->adapter->set($key, $string);
  }
}
```

Now in the main program we can instantiate cache client with ease - dependancy of the client will be resolved automatically.

```
namespace Acme;

use IW\ServiceContainer;

$container = new ServiceContainer;
$client = $container->get(Cache\Client::class);
```

Our still implementing serialization by itself. Let's move it into dependancy.

```
namespace Acme\Cache;

class PhpSerializer {
  function serialize($value): string {}
  function unserialize(string $string) {}
}
```

And little change in the client.

```
namespace Acme\Cache;

class Client {
  private $adapter;

  function __construct(MemoryAdapter $adapter, PhpSerializer $serializer) {
    $this->adapter = $adapter;
    $this->serializer = $serializer;
  }

  function get(string $key) {
    $string = $this->adapter->get($key);
    return $this->serializer->unserialize($string);
  }

  function set(string $key, $value): void {
    $string = $this->serializer->serialize($value);
    $this->adapter->set($key, $string);
  }
}
```

Our main code will stay the same.

```
$client = $container->get(Cache\Client::class);
```

Method `resolve` is useful for resolving any dependencies of a *callable*. Especially it's useful for `init` template method. See following example.

```
abstract class Parent {
  function __construct(Dependency $dependency, ServiceContainer $container) {
    $this->dependency = $dependency;
    $container->resolve([$this, 'init']);
  }
}

class Child extends Parent {
  function init(AhotherDependency $another) {
    // ...
  }
}
```

#### Manual Wiring

[](#manual-wiring)

Sometimes you want to configure container manually. Let's consider following example on *Command Pattern*

```
interface OrderCommand
{
  function execute();
}

class OrderInvoker
{
  function __construct(private OrderCommand ...$commands) {}

  function execute() : void {
    array_walk($this->commands, fn($command) => $command->execute());
  }
}
```

With `IW\ServiceContainer` you have several options how to resolve `OrderInvoker`'s dependencies.

```
// an alias but that's no good for multiple commands
$container->alias('OrderInvoker', 'ReserveItems');

// external factory
$container->bind('OrderInvoker', function (IW\ServiceContainer $container) {
  return new OrderInvoker($container->get('ReserveItems'), $container->get('SendInvoice'));
});

// internal factory
$container->bind('OrderInvoker', 'OrderInvoker::create');

class OrderInvoker
{
  static function create(ReserveItems $reserveItems, SendInvoice $sendInvoice) : OrderInvoker {
    return new OrderInvoker($reserveItems, $sendInvoice);
  }
}

// wiring factory
$container->wire('OrderInvoker', 'ReserveItems', 'SendInvoice');

// using annotations (TBD PHP 8.0), used as a fallback (can be overridden by defining factory directly (eg. in tests)
class OrderInvoker
{
  #[IW\ServiceContainer\Bind('create')]
  function __construct(private OrderCommand ...$commands) {}

  static function create(ReserveItems $reserveItems, SendInvoice $sendInvoice) : OrderInvoker {
    return new OrderInvoker($reserveItems, $sendInvoice);
  }
}
```

Arguably all approaches have their advantages. *internal factory* approach is good for static analysis. *wiring factory* is useful for common application pattern and when dependencies may vary.

**TODO** keep going with examples

TODO
----

[](#todo)

```
$exception->getOrigin(); // returns first exception outside the framework (useful for avoiding of tracing)
```

License
-------

[](#license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance50

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~88 days

Recently: every ~122 days

Total

23

Last Release

813d ago

PHP version history (3 changes)v0.6PHP ^7.2

v0.6.3PHP ^7.2||^8.0

v0.8PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2031969?v=4)[Ondřej Ešler](/maintainers/esler)[@esler](https://github.com/esler)

---

Top Contributors

[![esler](https://avatars.githubusercontent.com/u/2031969?v=4)](https://github.com/esler "esler (28 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/intraworlds-service-container/health.svg)

```
[![Health](https://phpackages.com/badges/intraworlds-service-container/health.svg)](https://phpackages.com/packages/intraworlds-service-container)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31178.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

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