PHPackages                             flytachi/winter-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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. flytachi/winter-di

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

flytachi/winter-di
==================

Lightweight PSR-11 DI container for Winter framework (autowiring, scopes, attributes)

v1.1.2(1mo ago)086—7.7%1MITPHPPHP &gt;=8.3

Since Apr 30Pushed 1mo agoCompare

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

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

Winter DI
=========

[](#winter-di)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f1b095f2e2bff2a9dc80d7844a5fad084851e688f48547225615660b9a4cd56b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666c7974616368692f77696e7465722d64692e737667)](https://packagist.org/packages/flytachi/winter-di)[![PHP Version Require](https://camo.githubusercontent.com/9edbeb670c8676e5751bd38ecd4c72ec85fbc1f42d2592dd57ed9e97b932f98f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f666c7974616368692f77696e7465722d64692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/flytachi/winter-di)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

Lightweight PSR-11 dependency injection container for the Winter framework. Autowiring, three lifecycle scopes, attribute-based configuration, and service providers.

---

Requirements
------------

[](#requirements)

- PHP **8.3+**
- `psr/container ^2.0`
- `ext-swoole` *(optional)* — required for `request` scope coroutine isolation

---

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

[](#installation)

```
composer require flytachi/winter-di
```

---

Quick start
-----------

[](#quick-start)

```
use Flytachi\Winter\DI\Container;
use Flytachi\Winter\DI\Scanner;
use Flytachi\Winter\DI\Collector\DICollector;

// bootstrap.php — once at application start
$container = Container::init();

Scanner::run(__DIR__ . '/src', cache: __DIR__ . '/var/cache/di.php')
    ->collect(new DICollector($container))  // auto-register #[Singleton], #[Request], #[Transient]
    ->execute();

$container->register(AppServiceProvider::class); // bind interfaces and factories

// Resolve anywhere
$service = Container::getInstance()->make(UserService::class);

// Call a method with full injection
$result = Container::getInstance()->call([UserController::class, 'index']);
```

---

Scopes
------

[](#scopes)

ScopeLifetimeSafe in Swoole`singleton`One instance per process✓ if stateless`transient`New instance on every `make()`✓ always`request`One instance per request / coroutine✓ isolated via `Coroutine::getContext()`Default scope when no attribute and no manual registration: **transient**.

---

Attributes
----------

[](#attributes)

```
use Flytachi\Winter\DI\Attribute\Singleton;
use Flytachi\Winter\DI\Attribute\Transient;
use Flytachi\Winter\DI\Attribute\Request;
use Flytachi\Winter\DI\Attribute\Autowired;
use Flytachi\Winter\DI\Attribute\Inject;

// Scope on class
#[Singleton]
class UserRepository { }

#[Request]
class AuthContext { }

#[Transient]
class QueryBuilder { }

// Injection overrides on constructor parameters
class UserService
{
    public function __construct(
        private UserRepository $repo,               // autowired by type (no attribute needed)

        #[Inject(FileCache::class)]
        private CacheInterface $fallback,           // specific implementation

        #[Inject('config.timeout')]
        private int $timeout,                       // named value
    ) {}
}

// Property injection (when constructor is unavailable)
class SomeCommand
{
    #[Autowired]                                    // by declared type — idiomatic choice
    private UserService $service;

    #[Inject(FileCache::class)]                     // specific implementation override
    private CacheInterface $cache;
}
```

---

Container API
-------------

[](#container-api)

```
$c = Container::init();       // initialise (bootstrap)
$c = Container::getInstance();// get anywhere

// Binding
$c->bind(CacheInterface::class, RedisCache::class);              // transient
$c->singleton(CacheInterface::class, RedisCache::class);         // singleton
$c->transient(QueryBuilder::class);                              // transient (explicit)
$c->request(AuthContext::class);                                 // request-scoped
$c->set('config.timeout', 30);                                   // named scalar / instance

// Factory closure — receives the container
$c->bind(MailerInterface::class, fn(Container $c) =>
    new SmtpMailer(env('MAIL_HOST'), $c->make(LoggerInterface::class))
);

// Resolution
$service = $c->make(UserService::class);
$service = $c->make(UserService::class, ['timeout' => 60]); // with overrides

// Method / closure injection
$result = $c->call([UserController::class, 'index']);
$result = $c->call([$controller, 'store']);
$result = $c->call(fn(UserService $s) => $s->all());
$result = $c->call([ImportJob::class, 'run'], ['chunkSize' => 500]);

// PSR-11
$c->has(UserService::class); // bool
$c->get(UserService::class);  // mixed — alias for make()
```

---

Service providers
-----------------

[](#service-providers)

```
use Flytachi\Winter\DI\Contract\ServiceProvider;
use Flytachi\Winter\DI\Container;

class AppServiceProvider extends ServiceProvider
{
    public function register(Container $c): void
    {
        $c->singleton(CacheInterface::class, RedisCache::class);
        $c->request(AuthContext::class);
        $c->set('config.timeout', (int) env('APP_TIMEOUT', 30));
        $c->bind(MailerInterface::class, fn($c) =>
            new SmtpMailer(env('MAIL_HOST'), $c->make(LoggerInterface::class))
        );
    }
}

// bootstrap.php
Container::init()
    ->register(AppServiceProvider::class)
    ->register(DatabaseServiceProvider::class);
```

---

Scanner
-------

[](#scanner)

`Scanner` walks the project tree once and dispatches every discovered class to all registered `CollectorInterface` implementations — a single filesystem pass, multiple consumers.

```
use Flytachi\Winter\DI\Scanner;
use Flytachi\Winter\DI\Collector\DICollector;

// Without cache — always scans (dev mode, PPA, Cmd, Db collectors)
Scanner::run($rootDir)
    ->collect(new PpaCollector())
    ->collect(new CmdCollector())
    ->execute();

// With cache — skips FS walk on cache hit (production)
Scanner::run($rootDir, cache: '/var/cache/scanner.php')
    ->collect(new DICollector($container))
    ->collect(new MappingCollector($router))
    ->execute();

// Exclude additional directories (vendor/ is always excluded)
Scanner::run($rootDir)
    ->exclude(['/path/to/legacy', '/path/to/generated'])
    ->collect(new DICollector($container))
    ->execute();
```

The cache stores only the list of discovered FQCNs as a plain PHP file — fast `require`, no serialization overhead. Delete the file to force a rescan.

---

ReflectionCache
---------------

[](#reflectioncache)

Per-process cache for reflection objects. Creates each `ReflectionClass`, `ReflectionMethod`, and `ReflectionParameter[]` once and reuses it for the process lifetime — critical in Swoole where workers handle many requests.

```
use Flytachi\Winter\DI\ReflectionCache;

$ref    = ReflectionCache::classOf(UserService::class);   // ReflectionClass
$enum   = ReflectionCache::enumOf(Status::class);   // ReflectionEnum
$method = ReflectionCache::method(UserService::class, 'handle'); // ReflectionMethod
$params = ReflectionCache::parameters(UserService::class, 'handle'); // ReflectionParameter[]
```

Used internally by `ReflectionResolver` — available as a public utility for frameworks and libraries that perform their own reflection-based parameter resolution.

---

Exceptions
----------

[](#exceptions)

ExceptionWhen`ContainerException`Circular dependency, unresolvable parameter, provider error`NotFoundException`No binding and class does not existBoth implement the PSR-11 interfaces (`ContainerExceptionInterface`, `NotFoundExceptionInterface`).

---

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

[](#documentation)

Full documentation is available in [`docs/`](docs/):

FileContents[01-overview.md](docs/01-overview.md)Features, installation, quick start[02-container.md](docs/02-container.md)Complete Container API reference[03-scopes.md](docs/03-scopes.md)Scopes — singleton, transient, request; Swoole behaviour[04-attributes.md](docs/04-attributes.md)`#[Singleton]`, `#[Transient]`, `#[Request]`, `#[Autowired]`, `#[Inject]`[05-providers.md](docs/05-providers.md)ServiceProvider — grouping bindings[06-scan.md](docs/06-scan.md)Directory scan — auto-discovery[07-reflection-cache.md](docs/07-reflection-cache.md)ReflectionCache — per-process reflection object cache---

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE).

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance92

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

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

Total

5

Last Release

39d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/861b81dd97c8ddfa919522d2a4e17626120bd3e3d7464857cc03784676bc74a8?d=identicon)[Flytachi](/maintainers/Flytachi)

---

Top Contributors

[![Flytachi](https://avatars.githubusercontent.com/u/68924300?v=4)](https://github.com/Flytachi "Flytachi (7 commits)")

---

Tags

containerPSR-11Autowiringdependency-injectiondiwinter

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/flytachi-winter-di/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k53.2M1.2k](/packages/php-di-php-di)[slince/di

A flexible dependency injection container

20268.4k6](/packages/slince-di)[infocyph/intermix

A lightweight PHP DI container, invoker, serializer, and utility toolkit.

137.3k1](/packages/infocyph-intermix)[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

124.2k3](/packages/devanych-di-container)

PHPackages © 2026

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