PHP code example of sotvokun / container

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

    

sotvokun / container example snippets


use App\Contract\Filesystem;
use App\Controller\PhotoController;
use App\Service\OrderService;
use App\Storage\LocalFilesystem;
use Sotvokun\Container\Container;

$container = new Container();

$container->singleton(Filesystem::class, LocalFilesystem::class);

$container->when(PhotoController::class)
    ->needs(Filesystem::class)
    ->give(LocalFilesystem::class);

$orderService = $container->make(OrderService::class);

namespace App\Aop;

use Attribute;
use Sotvokun\Container\Aop\InterceptorProvider;
use Sotvokun\Container\Aop\MethodInterceptor;
use Sotvokun\Container\Aop\MethodInvocation;

#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class Trace implements InterceptorProvider
{
    public static function interceptors(): array
    {
        return [TraceInterceptor::class];
    }
}

final class TraceInterceptor implements MethodInterceptor
{
    public function invoke(MethodInvocation $invocation): mixed
    {
        error_log('Entering ' . $invocation->getMethod()->getName());

        try {
            return $invocation->proceed();
        } finally {
            error_log('Leaving ' . $invocation->getMethod()->getName());
        }
    }
}

namespace App\Service;

use App\Aop\Trace;
use Sotvokun\Container\Container;

final class GreetingService
{
    #[Trace]
    public function greet(string $name): string
    {
        return "Hello, {$name}";
    }
}

$container = new Container();

$sourceDirectory = __DIR__ . '/src';
$generatedDirectory = __DIR__ . '/var/aop';

if (! is_dir($generatedDirectory)) {
    mkdir($generatedDirectory, 0775, true);
}

$container->enableAop([$sourceDirectory], $generatedDirectory);

echo $container->make(GreetingService::class)->greet('World');

use Illuminate\Contracts\Container\Container as ContainerContract;

interface ReportGeneratorFactory
{
    public function make(): ReportGenerator;
}

final class ContainerReportGeneratorFactory implements ReportGeneratorFactory
{
    public function __construct(private ContainerContract $container) {}

    public function make(): ReportGenerator
    {
        return $this->container->make(ReportGenerator::class);
    }
}

final class ExportController
{
    public function __construct(private ReportGeneratorFactory $reports) {}

    public function export(): string
    {
        return $this->reports->make()->export();
    }
}

$container->bind(
    ReportGeneratorFactory::class,
    ContainerReportGeneratorFactory::class,
);

use Sotvokun\Container\Attributes\Lazy;

$container->enableLazyInjection();

final class ExportController
{
    public function __construct(#[Lazy] private ReportGenerator $reports) {}
}