Mizmoz / Container
- Be lightweight, lazy load all the things.
- Use PHP-FIG PSR-11 v2 interface
- Auto resolve when possible
composer require mizmoz/container
use Mizmoz\Container;
$container = new Container;
// Add an item to the container
$container->add(MyClass::class, function () {
return new MyClass('some', 'params');
});
// get the item
$myClass = $container->get(MyClass::class);
// Add a shared item
$container->addShared(MyClass::class, function () {
return new MyClass();
}, Container::SHARED);
// get the shared item
$container->get(MyClass::class) === $container->get(MyClass::class);
// check if an item exists in the container
$container->has(MyClass::class);
// add an alias for easy access to items
$container->addAlias(SomeLogger::class, 'log');
Basic constructor resolution is available.
// Pass the resolver in to the container
$container = new Container(new Resolver);
// silly example
$date = $container->get(DateTime::class);
// add entries to the container to have interfaces resolved
$container->add(SomeInterface::class, function () { return new Some(); });
// add a raw value to the container, these are always treated as shared objects.
$container->addValue('config', new Config());
// now auto resolve the class that requires the SomeInterface entry
$container->get(SomeUser::class);
Use the ManageContainerTrait
to automatically have the container set on a class when it's returned via get($id)
Entry add(string $id, callable $entry, string $type = Container::EXCLUSIVE)
Entry addShared(string $id, callable $entry)
Entry addExclusive(string $id, callable $entry)
Entry addValue(string $id, $entry)
Provider addServiceProvider(Contract\ServiceProvider $serviceProvider)
Container addAlias(string $id, string $alias)
mixed get(string $id)
bool has(string $id)
- Auto resolver improvements
- Add ability to resolve basic arguments e.g. Resolve $secret in Connect(string $secret)
- Add event notifications with Mizmoz\Event