PHP code example of thesis / di

1. Go to this page and download the library: Download thesis/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/ */

    

thesis / di example snippets


$logger = $dic->object(NullLogger::class); // Ref<NullLogger>

use Psr\Log\NullLogger;
use Thesis\Dic;
use Thesis\Dic\Module;

final readonly class ConsoleModule implements Module
{
    public function configure(Dic $dic)
    {
        $logger = $dic->object(NullLogger::class);
    
        $dic
            ->object(ConsoleApplication::class)
            ->arg('logger', $logger);
    }   
}

use Psr\Log\LoggerInterface;
use Thesis\Dic;
use Thesis\Dic\Module;
use Thesis\Dic\Ref;

/**
 * @implements Module<Ref<ConsoleApplication>>
 */
final readonly class ConsoleModule implements Module
{
    /**
     * @param Ref<LoggerInterface> $logger
     */
    public function __construct(
        private Ref $logger,
    ) {}

    public function configure(Dic $dic): mixed
    {
        return $dic
            ->object(ConsoleApplication::class)
            ->arg('logger', $this->logger);
    }
}

use Psr\Log\NullLogger;
use Thesis\Dic;
use Thesis\Dic\Module;
use Thesis\Dic\Ref;

/**
 * @implements Module<Ref<ConsoleApplication>>
 */
final readonly class MyApp implements Module
{
    public function configure(Dic $dic): mixed
    {
        $logger = $dic->object(NullLogger::class);

        return $dic->import(new ConsoleModule($logger));
    }
}

use Thesis\Dic;

$status = Dic::run(
    module: new MyApp(),
    main: static fn (ConsoleApplication $cli) => $cli->run(),
);

exit($status);

use Testo\Assert;
use Thesis\Dic;

$cli = Dic::build(new MyApp());

Assert::instanceOf($cli, ConsoleApplication::class);