1. Go to this page and download the library: Download maduser/argon 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/ */
class Logger {}
class UserService
{
public function __construct(Logger $logger, string $env = 'prod') {}
}
$container->get(UserService::class); // Works out of the box
interface LoggerInterface {}
class UserService
{
public function __construct(LoggerInterface $logger, string $env) {}
}
$container->get(UserService::class); // 500: No interface binding, no default value for $env.
interface LoggerInterface {}
class FileLogger implements LoggerInterface {}
class DatabaseLogger implements LoggerInterface {}
class ServiceA
{
public function __construct(LoggerInterface $logger) {}
}
class ServiceB
{
public function __construct(LoggerInterface $logger) {}
}
$container->for(ServiceA::class)
->set(LoggerInterface::class, DatabaseLogger::class);
// Same Interface, different implementation
$container->for(ServiceB::class)
->set(LoggerInterface::class, FileLogger::class);
class AppServiceProvider implements ServiceProviderInterface
{
// called before compilation and should be used to declare bindings
public function register(ArgonContainer $container): void
{
$container->set(LoggerInterface::class, FileLogger::class);
$container->set(CacheInterface::class, RedisCache::class)->transient();
}
// Executed after compilation, once the container is ready to resolve services
public function boot(ArgonContainer $container): void
{
// Optional setup logic
}
}
$container->register(AppServiceProvider::class);
$container->boot();
use Maduser\Argon\Container\Contracts\PostResolutionInterceptorInterface;
interface Validatable
{
public function validate(): void;
}
class MyDTO implements Validatable
{
public function validate(): void
{
// Verify (object $instance): void
{
$instance->validate();
}
}
$container->registerInterceptor(ValidationInterceptor::class);
$dto = $container->get(MyDTO::class); // validate() is automatically called
use Maduser\Argon\Container\Contracts\PreResolutionInterceptorInterface;
class EnvOverrideInterceptor implements PreResolutionInterceptorInterface
{
public static function supports(string $id): bool
{
return $id === ApiClient::class;
}
public function intercept(string $id, array &$parameters): ?object
{
$parameters['apiKey'] = $_ENV['APP_ENV'] === 'prod'
? 'prod-key'
: 'dev-key';
return null; // let container continue
}
}
class StubInterceptor implements PreResolutionInterceptorInterface
{
public static function supports(string $id): bool
{
return $id === SomeHeavyService::class && $_ENV['TESTING'];
}
public function intercept(string $id, array &$parameters): ?object
{
return new FakeService(); // short-circuit
}
}
$container->registerInterceptor(EnvOverrideInterceptor::class);
$container->registerInterceptor(StubInterceptor::class);
// For example in a ServiceProvider
public function boot(ArgonContainer $container): void
{
$container->extend(LoggerInterface::class, function (object $logger): object {
return new BufferingLogger($logger);
});
}
// Suppose SomeLogger is optional
$container->optional(SomeLogger::class)->log('Only if logger exists');
// This won't throw, even if SomeLogger wasn't registered
// In a ServiceProvider — boot() runs at runtime, safe for closures
public function boot(ArgonContainer $container): void
{
$container->set(LoggerInterface::class, fn (Config $config) => {
return new FileLogger($config->get('log.path'));
});
}
// Exclude from compilation explicitly
$container->set(LoggerInterface::class, fn (Config $config) => {
return new FileLogger($config->get('log.path'));
})->skipCompilation();
$file = __DIR__ . '/CompiledContainer.php';
if (file_exists($file) && !$_ENV['DEV']) {
// configure $container...
$compiler = new ContainerCompiler($container);
$compiler->compileToFile($file);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.