PHP code example of monkeyscloud / monkeyslegion-di
1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-di library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
monkeyscloud / monkeyslegion-di example snippets
use MonkeysLegion\DI\Container;
$container = new Container([
LoggerInterface::class => fn() => new FileLogger('/var/log/app.log'),
]);
// Auto-wires dependencies automatically
$service = $container->get(UserService::class);
class UserService {
public function __construct(
private UserRepository $repo,
private LoggerInterface $logger,
) {}
}
// Just works — both dependencies are resolved recursively
$service = $container->get(UserService::class);
$container = new Container([
PDO::class => fn() => new PDO('mysql:host=localhost;dbname=app', 'user', 'pass'),
CacheInterface::class => fn(ContainerInterface $c) => new RedisCache(
$c->get(Redis::class),
),
]);
$container->set('mailer', fn() => new SmtpMailer($config));
$container->set(LoggerInterface::class, new NullLogger());
$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(CacheInterface::class, RedisCache::class);
// Now any class depending on LoggerInterface gets FileLogger
$service = $container->get(UserService::class);
class NotificationService {
public function __construct(
#[Inject('slack.logger')]
private LoggerInterface $logger,
) {}
}
#[Transient]
class RequestContext {
public readonly string $id;
public function __construct() {
$this->id = uniqid('req_', true);
}
}
// Each call returns a new instance
$a = $container->get(RequestContext::class);
$b = $container->get(RequestContext::class);
assert($a !== $b);
#[Singleton]
class DatabaseConnection {
// Cached after first resolution (this is the default lifecycle,
// but the attribute makes intent explicit)
}
#[Tagged('event.listener')]
class UserCreatedListener { /* ... */ }
#[Tagged('event.listener')]
class AuditLogListener { /* ... */ }
// Retrieve all tagged services
$listeners = $container->getTagged('event.listener');
use MonkeysLegion\DI\ContainerBuilder;
$builder = new ContainerBuilder();
$builder
->addDefinitions([
PDO::class => fn() => new PDO($dsn, $user, $pass),
])
->set('app.debug', fn() => true)
->bind(LoggerInterface::class, FileLogger::class)
->tag(UserCreatedListener::class, 'event.listener')
->transient(RequestContext::class);
$container = $builder->build();
use MonkeysLegion\DI\ContainerBuilder;
use MonkeysLegion\DI\ContainerDumper;
// Build and dump (deploy step)
$builder = new ContainerBuilder();
$builder->addDefinitions([/* ... */]);
$container = $builder->build();
$dumper = new ContainerDumper();
$dumper->dump($container, '/var/cache/compiled_container.php');
// Load in production (fast boot)
$builder = new ContainerBuilder();
$builder
->addDefinitions([/* same definitions */])
->enableCompilation('/var/cache');
$container = $builder->build(); // Returns CompiledContainer if cache exists
use MonkeysLegion\DI\Container;
use MonkeysLegion\DI\Traits\ContainerAware;
$container = new Container();
// ... logic to add definitions, bindings, etc.
// Set the global instance for both the Container and the Trait
Container::setInstance($container);
class LegacyService {
use ContainerAware;
public function doSomething() {
// Access dependencies without constructor injection
$db = $this->resolve(Database::class);
if ($this->has('feature_flag')) {
// ...
}
}
}
// Reset cached instances between tests
$container->reset();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.