PHPackages                             thecodingmachine/service-provider-registry - 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. thecodingmachine/service-provider-registry

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

thecodingmachine/service-provider-registry
==========================================

This package provides a service provider registry that can lazily instantiate the service providers it contains.

v3.0.0(6y ago)14.9k—0%31MITPHPPHP &gt;=7.1

Since Dec 12Pushed 6y ago7 watchersCompare

[ Source](https://github.com/thecodingmachine/service-provider-registry)[ Packagist](https://packagist.org/packages/thecodingmachine/service-provider-registry)[ RSS](/packages/thecodingmachine-service-provider-registry/feed)WikiDiscussions 3.0 Synced 1mo ago

READMEChangelog (4)Dependencies (6)Versions (8)Used By (1)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8ce6b8540279fba38366b9c559a421b28129c248c70d19cc9f19140dd22bc854/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f746865636f64696e676d616368696e652f736572766963652d70726f76696465722d72656769737472792f6261646765732f7175616c6974792d73636f72652e706e673f623d332e30)](https://scrutinizer-ci.com/g/thecodingmachine/service-provider-registry/?branch=3.0)[![Build Status](https://camo.githubusercontent.com/4e6b61d88642097a03a344cec24c1a98093fd0459f7dd5fe88fce0453160f7bd/68747470733a2f2f7472617669732d63692e6f72672f746865636f64696e676d616368696e652f736572766963652d70726f76696465722d72656769737472792e7376673f6272616e63683d332e30)](https://travis-ci.org/thecodingmachine/service-provider-registry)[![Coverage Status](https://camo.githubusercontent.com/d6f21be7c11dbab5546f92a23d946470b1a16d0c6c5922a72ca1f4b247467cd6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746865636f64696e676d616368696e652f736572766963652d70726f76696465722d72656769737472792f62616467652e7376673f6272616e63683d332e3026736572766963653d676974687562)](https://coveralls.io/github/thecodingmachine/service-provider-registry?branch=3.0)

What is it?
===========

[](#what-is-it)

This project contains a registry that stores service providers. The registry implements `\ArrayAccess` and behaves like an array. However, service providers in this object can be instantiated only when the key in the array is fetched, so you don't have to create the instance right away. This is useful for performance considerations, especially for compiled or cached containers.

This class is meant to be used by compiled/cached containers that want to implement [container-interop service providers](http://github.com/container-interop/service-provider). It is not meant for the mere mortals.

How does it work?
=================

[](#how-does-it-work)

Easy, you create a new `Registry` object, and then, you push objects in it.

```
$registry = new Registry();

$key = $registry->push(MyServiceProvider::class);

// This will trigger the creation of the MyServiceProvider object and return it.
$serviceProvider = $registry[$key];
```

You can also pass parameters to the constructor of the object:

```
$registry = new Registry();

$key = $registry->push(MyServiceProvider::class, "param1", "param2");
```

And because we are kind, you can also push into the lazy array an already instantiated object:

```
$registry = new Registry();

// This is possible, even if we loose the interest of the Registry.
$key = $registry->push(new MyServiceProvider());
```

Finally, if you are performance oriented (and I'm sure you are, otherwise you wouldn't be looking at this package), you can create the whole registry in one call:

```
$registry = new Registry([
    MyServiceProvider::class, // Is you simply want to create an instance without passing parameters
    [ MyServiceProvider2::class, [ "param1", "param2 ] ],  // Is you simply want to create an instance and pass parameters to the constructor
    new MyServiceProvider4('foo') // If you directly want to push the constructed instance.
]);
```

Iterating the registry
======================

[](#iterating-the-registry)

The registry implements the `\Traversable` interface, so iterating it is as simple as a `foreach`:

```
foreach ($registry as $serviceProvider) {
    // Do stuff for each service provider.
    // Service providers will be instantiated on the fly if needed.
}
```

Discovery with thecodingmachine/discovery
=========================================

[](#discovery-with-thecodingmachinediscovery)

The registry supports a discovery mechanism based on [thecodingmachine/discovery](https://thecodingmachine.github.io/discovery/) (to automatically find and attach service providers to your application).

As a second parameter, the `Registry` accepts the `Discovery` object from [thecodingmachine/discovery](https://github.com/thecodingmachine/discovery). Pass this object and thecodingmachine/discovery will be used to fetch service providers from your packages.

```
$registry = new Registry([], TheCodingMachine\Discovery::getInstance());

// The registry now contains all the service providers discoverable by thecodingmachine/discovery.
```

Caching of `getFactories` and `getExtensions`
=============================================

[](#caching-of-getfactories-and-getextensions)

You can use the shortcut `Registry::getFactories($key)` or `Registry::getExtensions($key)` methods to call the `getFactories` and `getExtensions` methods on a service provider. The result is cached: 2 successive calls will not call the `getFactories` or `getExtensions` methods twice.

```
$factories = $registry->getFactories(0);

$extensions = $registry->getExtensions(0);
```

Using the registry to create services
=====================================

[](#using-the-registry-to-create-services)

Even better, using the `createService` method of the registry, you can directly call the service factory:

```
$myService = $registry->createService(0, 'serviceName', $container);
```

Using the `extendService` method of the registry, you can directly call the service extension:

```
$myService = $registry->extendService(0, 'serviceName', $container, $previousService);
```

Why?
====

[](#why)

This was built for improving the performance of service providers loading (in compiled container environment).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 90% 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 ~173 days

Recently: every ~251 days

Total

8

Last Release

2221d ago

Major Versions

v0.3.0 → v1.1.02017-07-10

1.1.x-dev → 2.0.x-dev2017-09-17

v2.0.0 → 3.0.x-dev2020-04-10

PHP version history (3 changes)1.0.x-devPHP &gt;=5.6

v1.1.0PHP &gt;=7.0

3.0.x-devPHP &gt;=7.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

![](https://avatars.githubusercontent.com/u/1847918?v=4)[TheCodingMachine](/maintainers/thecodingmachine)[@thecodingmachine](https://github.com/thecodingmachine)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (27 commits)")[![romm](https://avatars.githubusercontent.com/u/6342901?v=4)](https://github.com/romm "romm (2 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thecodingmachine-service-provider-registry/health.svg)

```
[![Health](https://phpackages.com/badges/thecodingmachine-service-provider-registry/health.svg)](https://phpackages.com/packages/thecodingmachine-service-provider-registry)
```

PHPackages © 2026

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