gacela-project/container

A minimalistic container dependency resolver

Maintainers

Package info

github.com/gacela-project/container

Homepage

pkg:composer/gacela-project/container

Transparency log

Fund package maintenance!

chemaclass.com/sponsor

Statistics

Installs: 166 270

Dependents: 2

Suggesters: 0

Stars: 11

Open Issues: 3


README

GitHub Build Status Latest Version PHP Version Require Psalm Type-coverage Status MIT Software License

A minimalistic, PSR-11 compliant dependency injection container with automatic constructor injection and zero configuration.

Features

  • 🚀 Zero Configuration: Automatic constructor injection without verbose setup
  • 🔄 Circular Dependency Detection: Clear error messages when dependencies form a loop
  • 📦 PSR-11 Compliant: Standard container interface for interoperability
  • Performance Optimized: Built-in caching, warmup, and a compiled cache that skips reflection
  • 🧩 Fluent Registration: Register bindings after construction with bind() and singleton()
  • 🎁 Typed Resolution: make() returns a typed instance; getOrFail() never returns null
  • 🔍 Introspection: Debug and inspect container state easily
  • 🎯 Type Safe: Requires type hints for reliable dependency resolution
  • 🏷️ PHP 8 Attributes: Declarative configuration with #[Inject], #[Singleton], and #[Factory]

Installation

composer require gacela-project/container

Requires PHP >= 8.3.

Hello World

use Gacela\Container\Container;

class Greeter {
    public function __construct(private Clock $clock) {}

    public function greet(): string {
        return 'Hello World at ' . $this->clock->now();
    }
}

class Clock {
    public function now(): string {
        return date('H:i:s');
    }
}

// Zero configuration — dependencies are auto-wired from type hints
$container = new Container();
$greeter = $container->make(Greeter::class);

echo $greeter->greet();

Need interfaces, singletons, attributes, or a compiled cache? See the docs below.

Documentation

Guide What's inside
Getting Started Installation, basic usage, how resolution works
Bindings & Registration Constructor bindings, bind()/singleton(), contextual bindings, aliasing
Resolving Services get(), make(), getOrFail(), resolve(), transient vs. shared
PHP 8 Attributes #[Inject], #[Singleton], #[Factory]
Managing Services Factories, extending, protecting closures, introspection
Performance & Compilation warmUp(), compiled container cache
Cookbook Recipes: testing, config, plugins, decorating, debugging
Error Handling Every exception, what causes it, how to fix it
Best Practices Recommended patterns
API Reference Full method, static, and attribute reference
Backward Compatibility What semver covers here, and what it does not
Upgrade Guide Migrating from 0.10.0 to 1.0.0

Real-World Example

See how it's used in the Gacela Framework.

Testing

composer test          # Run tests
composer quality       # Run static analysis
composer test-coverage # Generate coverage report

License

MIT License. See LICENSE file for details.