taujor/cally
最新稳定版本:1.0.0
Composer 安装命令:
composer require taujor/cally
包简介
Cally is a lightweight, immutable registry for storing and retrieving data or services in PHP. It provides a simple yet powerful way to manage configurations, dependencies, or any key-value pairs with support for immutability and PSR-11 compliance. Cally is ideal for any project where a full-fledged
README 文档
README
Cally is a minimal, elegant, and strict PSR-11–compatible service locator for PHP 8+.
It provides a simple API for defining services, factories, singletons, lazy-loaded objects, and values.
Cally is designed to be:
- Small – no dependencies
- Predictable – explicit, no magic
- Strict – throws meaningful exceptions
- PSR-11 compliant – works with existing standards
- Fast – closures only, no reflection
Features
- PSR-11
ContainerInterfacecompatible - Register lazy-loaded services
- Register singletons
- Register factories
- Register simple values
- Freeze the container to prevent modification
- Meaningful exception hierarchy:
FrozenRegistryExceptionKeyAlreadyExistsExceptionKeyNotFoundException
Missing Essentials
- No circular dependency detection - Lazy services could create infinite loops if A depends on B depends on A
- No error context - Exceptions don't capture which dependency failed during complex resolution chains
- No type safety - Can't specify/validate what type a key should return
- No autowiring - Must manually register everything (fine for simple apps, tedious for large ones)
- No container awareness - Factories can't receive the container itself to resolve their own dependencies
- No unfreeze/clear - Once frozen, you're stuck (problematic for testing)
Debatable Missing Features
- Aliases - Point multiple keys to same service
- Tags/groups - Retrieve all services of a certain type
- Service decoration/extension - Wrap existing services
- Performance - No caching of resolution paths for complex dependency graphs
Production Readiness
For a small-to-medium application with straightforward dependencies, it's probably fine. For production at scale, you'd likely want at least circular dependency detection and better error context. The rest depends on your specific needs.
Future
I plan to implement all missing features (and possibly some debatable ones) in future, while also staying commited to Cally's minimalistic nature.
Installation
Install via Composer:
composer require taujor/cally
Quick Start
Create the container
use Taujor\Cally\Cally; $container = new Cally();
Registering Services
Singleton
A single shared instance:
$pdo = new PDO('sqlite::memory:'); $container->singleton('db', $pdo);
Usage:
$db = $container->get('db'); // always returns the same instance
Lazy Service
Instantiated only once on first use:
$container->lazy('config', function () { return parse_ini_file('app.ini'); });
Factory
Produces a new instance every time:
$container->factory('uuid', fn() => bin2hex(random_bytes(16))); $id1 = $container->get('uuid'); $id2 = $container->get('uuid'); // always different
Value
Stores a simple immutable value:
$container->value('version', '1.0.0'); echo $container->get('version'); // "1.0.0"
Retrieving Services
$service = $container->get('key');
If the key does not exist:
KeyNotFoundException
Checking Keys
if ($container->has('cache')) { // ... }
Freezing the Container
After freezing the container, no new services can be registered.
$container->freeze(); $container->set('foo', fn() => 'bar'); // Throws FrozenRegistryException
Error Handling
Cally throws clear, meaningful exceptions:
| Exception | Trigger |
|---|---|
FrozenRegistryException |
Attempt to modify a frozen container |
KeyAlreadyExistsException |
Attempt to overwrite a key |
KeyNotFoundException |
Attempt to get a missing key |
All exceptions implement PSR-11 interfaces where appropriate.
Why Cally?
- No unnecessary abstraction layers
- A clean alternative to overly complex DI containers
- Explicit over magic
License
MIT license
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-17