Download the PHP package joby/smol-context without Composer
On this page you can find all versions of the php package joby/smol-context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joby/smol-context
More information about joby/smol-context
Files in joby/smol-context
Package smol-context
Short Description A lightweight DI container for PHP for global, low-config service and object registration. Use it to resolve and inject services, objects, and config values anywhere. You can execute callables with injected dependencies, instantiate objects with dependencies, and even include files using a unique DocBlock injection system.
License MIT
Informations about the package smol-context
smolContext
A lightweight dependency injection container with config integration and docblock-driven file inclusion.
Installation
About
smolContext provides a simple, static dependency injection container for PHP. Services are registered and automatically instantiated with their dependencies when retrieved.
Key features:
- Static API: Access the container globally via
Contextwithout passing it around - Automatic dependency resolution: Constructor and callable parameters are injected automatically
- Explicit registration: Services register only under specified class names for predictable behavior
- Config integration: Inject config values alongside objects using
#[ConfigValue] - Docblock file inclusion: Include PHP files with variables injected from docblock annotations
- Context stack: Push/pop container scopes for testing, sub-requests, or rollback workflows
Basic Usage
Registering and Retrieving Services
Registration Aliasing
By default, services are registered only under their exact class name. This explicit behavior prevents surprises and makes dependencies clear. However, you can optionally register a service under additional class names using the also parameter.
When to use also:
false(default): For concrete implementations you'll reference directlystring: When injecting via a specific interface or parent class (such as replacing something with a child class)array: When the same instance should satisfy multiple contractstrue: For widely-used services accessed via various type hints (use sparingly)
Creating Transient Objects
Build objects without caching them in the container:
Checking for Services
Executing Callables with Injection
Execute callables with automatic parameter injection:
Type-hinted object parameters are automatically resolved from the container.
Config Integration
Every container includes a config service (backed by joby/smol-config). Inject config values using the #[ConfigValue] attribute:
Mixing Config and Object Injection
Including Files with Docblock Injection
Include PHP files with variables injected from docblock annotations. This is useful for templates, scripts, or configuration files that need access to services.
The Include File
Create a file with dependencies declared in its opening docblock (report.php):
Including the File
Config Injection in Included Files
Docblocks don't support real PHP attributes, so config injection uses a string that looks like an attribute on the line immediately before @var. This isn't actually an attribute, and you don't even need to formally use the attribute class, it's just so that the syntax is familiar.
Type Resolution
Object types can be:
- Fully qualified:
@var \App\UserService $users - Imported via
use:@var UserService $users - Resolved relative to the file's namespace
Context Stack
The context actually maintains an internal stack of containers, allowing temporary scopes for testing, isolated operations, or rollback workflows.
Cloning the Current Container
Starting with an Empty Container
Using a Custom Container
Resetting Completely
Usage Patterns
Request-Scoped Services
Interface-Based Dependency Injection
Background Jobs
Template Rendering
Create template files that get services injected (templates/email.php):
Render templates with automatic injection:
API Reference
Static Context Methods
Context::register(string|object $classOrObject, string|array|bool $also = false): void- Register a class or instanceContext::get(string $class): object- Retrieve a service (cached)Context::new(string $class): object- Create a new instance (not cached)Context::execute(callable $callable): mixed- Execute a callable with dependency injectionContext::include(string $file): mixed- Include a PHP file with dependency injectionContext::has(string $class): bool- Check if service is registeredContext::container(): Container- Access the current container
Stack Operations
Context::openFromClone(): void- Clone current container and push itContext::openEmpty(): void- Create empty container and push itContext::openFromContainer(Container $c): void- Use custom containerContext::close(): void- Pop stack and restore previous containerContext::reset(): void- Clear stack and container
Requirements
Fully tested on PHP 8.3+, static analysis for PHP 8.1+.
License
MIT License - See LICENSE file for details.