PHPackages                             doiftrue/litewire-di - 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. doiftrue/litewire-di

ActiveLibrary

doiftrue/litewire-di
====================

A tiny single-file PSR-11-style autowire DI container for PHP, with no dependencies.

1.3.0(1mo ago)3148↓25%[1 issues](https://github.com/doiftrue/litewire-di/issues)MITPHPPHP &gt;=7.4CI passing

Since Jul 7Pushed 1mo agoCompare

[ Source](https://github.com/doiftrue/litewire-di)[ Packagist](https://packagist.org/packages/doiftrue/litewire-di)[ RSS](/packages/doiftrue-litewire-di/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

[![Dependencies: none](https://camo.githubusercontent.com/91fc644509aa2c02af4d7c2a33d5ffcd277bddd4941ca1f06f478d814e0a7d92/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d6e6f6e652d677265656e2e737667)](https://camo.githubusercontent.com/91fc644509aa2c02af4d7c2a33d5ffcd277bddd4941ca1f06f478d814e0a7d92/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d6e6f6e652d677265656e2e737667)[![PHP 7.4 and 8.x](https://camo.githubusercontent.com/c7e59b8e657a2b82648a8d7a45e36fff40d208d7e0c4ec425587d161adc0a48a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e34253230253743253230382e782d3737376262342e737667)](https://camo.githubusercontent.com/c7e59b8e657a2b82648a8d7a45e36fff40d208d7e0c4ec425587d161adc0a48a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e34253230253743253230382e782d3737376262342e737667)[![License: MIT](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)[![PHPUnit 100%](https://camo.githubusercontent.com/50e3c00c9bd3d5dfc6920b2dac9be7022a15b683b3ef7aa18b09954e5cda8358/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d3130302532352d677265656e2e737667)](https://camo.githubusercontent.com/50e3c00c9bd3d5dfc6920b2dac9be7022a15b683b3ef7aa18b09954e5cda8358/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d3130302532352d677265656e2e737667)[![PHPStan level 9](https://camo.githubusercontent.com/2b8268e54793ccb2a39084ff63318f1c3aa937d008d8426ec2c914533c69a2f8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d677265656e2e737667)](https://camo.githubusercontent.com/2b8268e54793ccb2a39084ff63318f1c3aa937d008d8426ec2c914533c69a2f8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d677265656e2e737667)[![Tests & Analysis](https://camo.githubusercontent.com/306aebb5d7ae02754d088e38b8817d03848e99e1971b8133109b72c3faf0f625/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646f6966747275652f6c697465776972652d64692f63692e796d6c3f6c6162656c3d5465737473253230253236253230416e616c79736973)](https://github.com/doiftrue/litewire-di/actions/workflows/ci.yml)

LiteWire DI Container
=====================

[](#litewire-di-container)

A tiny single-file autowiring DI container for PHP and WordPress.

Use it when Symfony DI, PHP-DI, or a framework container feels too heavy. You get a familiar `get()` / `has()` API, autowiring, factories, shared services, and no runtime dependencies.

Full documentation:

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

[](#installation)

Install with Composer:

```
composer require doiftrue/litewire-di
```

Or copy the single [`Container.php`](Container.php) file into your project. When copying it, change the `Kama\LiteWireDI` namespace to one owned by your project to avoid collisions with another copy of the container.

Compatibility
-------------

[](#compatibility)

PHP 7.4 and PHP 8.0-8.5 are tested in CI.

Features
--------

[](#features)

- Single portable PHP file.
- No runtime dependencies.
- Register existing objects, classes, closure factories, and configured constructor parameters with `set()`.
- Autowire registered and unregistered classes.
- Return shared service instances with `get()`.
- Create fresh instances with `make()`.
- Pass named runtime parameters to `make()`.
- Check whether classes and interfaces can be resolved with `has()`.
- Detect circular dependencies and show the full resolution chain.
- Use an object-first design with class and interface names as service IDs.

API
---

[](#api)

Four public methods:

- `has()` checks if a service can be resolved.
- `set()` registers an object, class, factory, or named parameters for an instantiable class.
- `get()` returns a shared object.
- `make()` creates a fresh object.

```
$container->has( class-string $id ): bool;
$container->set( class-string $id, object|Closure|class-string|array $service ): void;
$container->get( class-string $id );
$container->make( class-string $id, array $parameters = [] );
```

Service IDs must be class or interface names. Plain strings like `logger` are not supported.

Examples
--------

[](#examples)

### Quick start

[](#quick-start)

```
use Kama\LiteWireDI\Container;

final class Logger {
	public function log( string $message ): void {
		error_log( $message );
	}
}

final class Service {
	public function __construct(
		private readonly Logger $logger,
	) {}

	public function run(): void {
		$this->logger->log( 'Service started.' );
	}
}

$container = new Container();
$service = $container->get( Service::class );
$service->run(); // Logger is created automatically
```

### Interface binding

[](#interface-binding)

```
$container->set( Logger_Interface::class, File_Logger::class );

$logger = $container->get( Logger_Interface::class );
```

### Configured parameters

[](#configured-parameters)

Pass an associative array to register named constructor parameters for an instantiable class. Missing class dependencies are autowired. `get()` creates and stores the configured service.

```
final class Plugin {
	public function __construct(
		private readonly string $main_file,
		private readonly Options $options,
	) {}
}

$container->set( Plugin::class, [
	'main_file' => __FILE__,
] );

$plugin = $container->get( Plugin::class );
```

Configured parameters cannot be registered directly for an interface because the array does not identify an implementation. Use a class binding when the implementation needs no scalar configuration; otherwise use a factory.

### Factory registration

[](#factory-registration)

Factories must return an object. Their parameters are autowired too.

```
$container->set( Mailer::class, static function ( Logger $logger ) {
	return new Mailer( $logger );
} );

$shared_mailer = $container->get( Mailer::class );
$fresh_mailer = $container->make( Mailer::class );
```

### Runtime parameters

[](#runtime-parameters)

`make()` creates a fresh root object and accepts named constructor values.

```
final class Mailer {
	public function __construct(
		private readonly Logger $logger,
		private readonly string $from,
	) {}
}

$mailer = $container->make( Mailer::class, [
	'from' => 'admin@example.com',
] );
```

When the class has configured parameters from `set()`, `make()` uses them as defaults. Parameters passed directly to `make()` take priority.

Documentation
-------------

[](#documentation)

- [Full documentation](https://doiftrue.github.io/litewire-di/)
- [Configuration guide](https://doiftrue.github.io/litewire-di/guide/configuring-services)
- [WordPress plugin guide](https://doiftrue.github.io/litewire-di/documentation/full-wordpress)
- [Detailed benchmark report](benchmarks/README.md)

Benchmarks
----------

[](#benchmarks)

Benchmarks cover direct `new`, `get()`, `make()`, factories, and deep autowiring.

Treat timings as local numbers, not universal limits.

Results for PHP 8.5.5 (with OPcache enabled):

SubjectRuns × RoundsMem PeakTime (Variance)direct\_instantiation10 000 × 5678.904kb0.078μs (±5.48%)get\_\_cold10 000 × 516.449mb2.027μs (±0.65%)get\_\_stored10 000 × 5678.880kb0.058μs (±4.47%)get\_\_deep\_autowiring\_\_cold10 000 × 518.494mb2.895μs (±0.32%)get\_\_deep\_autowiring\_\_stored10 000 × 5666.744kb0.059μs (±4.00%)make\_\_reflection\_\_cold10 000 × 515.889mb2.016μs (±1.19%)make\_\_reflection\_\_cached10 000 × 5678.904kb0.804μs (±3.53%)make\_\_registered\_factory10 000 × 5678.904kb0.416μs (±6.95%)Legend:

- **Subject** - the operation measured by PHPBench.
- **Runs** - time benchmark method executed per round.
- **Rounds** - how many times the complete benchmark is repeated.
- **Time** - modal execution time per run (1 μs = 0.001 ms).
- **Variance** - how much execution time differs between rounds.
- **Mem Peak** - peak memory usage of the entire benchmark process.

Conclusions:

LiteWire DI does not compile a container between requests. In this benchmark, compilation would save about 0.121 ms for 100 objects or 1.21 ms for 1,000 objects.

That can matter for large apps. For small dependency graphs, LiteWire DI favors simple setup and predictable runtime behavior.

- Reflection caching makes `make()` about 2.5× faster.
- A registered factory is about 1.8× faster than cached reflection.
- Deep autowiring costs 2.744 μs initially, then 0.061 μs for stored results.

See: [Detailed benchmark results](benchmarks/README.md)

Limitations
-----------

[](#limitations)

LiteWire DI keeps the API small. There is no compiled container, service providers, scopes, tags, standalone scalar storage, string service IDs, or config format. Pass required scalar values as configured class parameters, factory arguments, `make()` parameters, or through a config object.

See the [full documentation](https://doiftrue.github.io/litewire-di/more/limitations) for the detailed list.

Inspired by
-----------

[](#inspired-by)

Inspired by [Simple DIC](https://github.com/renakdup/simple-dic)

LiteWire DI keeps the same single-file, dependency-free approach, but uses a stricter, object-only service model:

1. Service IDs must be existing class or interface names. Arbitrary string keys are rejected.
2. Resolved container entries are always objects; associative arrays may only configure named parameters for an instantiable class.
3. `set()` accepts named constructor parameters for shared concrete services while autowiring the remaining dependencies.
4. `make()` accepts named runtime parameters for constructors and factories.
5. `make()` respects registered class and factory definitions instead of resolving only the class passed as its ID.
6. `has()` reports existing concrete classes that can be autowired without prior registration.
7. Factory parameters are resolved in the same way as constructor parameters in both `get()` and `make()`.
8. Generic PHPDoc preserves the concrete return type of `get()` and `make()` for IDEs and static analysis.
9. A factory may request the container, other services, default values, and runtime values.
10. Factory results are validated: returning a primitive, array, or `null` throws a `ContainerException`.
11. Circular dependencies are detected and reported with the resolution chain.
12. Invalid or unsupported definitions and parameters fail with explicit exceptions.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance85

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

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

Total

2

Last Release

47d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a5b59537b7030cbdd84442d082f9ae993922622afae63296809ecc528d672dc?d=identicon)[doiftrue](/maintainers/doiftrue)

---

Top Contributors

[![doiftrue](https://avatars.githubusercontent.com/u/15121552?v=4)](https://github.com/doiftrue "doiftrue (55 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/doiftrue-litewire-di/health.svg)

```
[![Health](https://phpackages.com/badges/doiftrue-litewire-di/health.svg)](https://phpackages.com/packages/doiftrue-litewire-di)
```

PHPackages © 2026

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