PHP code example of fratily / container

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

    

fratily / container example snippets


use Fratily/Container/Builder/AbstractProvider;
use Fratily/Container/Builder/ContainerBuilder;
use Fratily/Container/ContainerFactory;

class Foo
{
    public function __construct(
        LoggerInterface $logger,
        SplQueue $queue,
        string $prefix
    ) {/* ... */}

    public function setCache(CacheInterface $cache): void {/* ... */}

    public function lock(): void {/* ... */}    
}

class SampleProvider extends AbstractProvider
{
    public function build(ContainerBuilder $builder): void
    {
        $builder->injection(Foo::class)
            ->addArgument("queue", new LazyNew(SplQueue::class))
            ->addArgument(3, new LazyGetParameter("parameter.prefix"))
            ->addSetter("setCache", new LazyGet(CacheInterface::class))
        ;
    
        $builder->parameter("parameter.prefix")
            ->setValue("this.is.prefix")
        ;
        
        $builder->service("service.foo")
            ->setValueByClass(Foo::class)
        ;
    
        $builder->service(LoggerInterface::class)
            ->setValueByFactory(
                function () {
                    return new Logger();
                }
            )
        ;

        $builder->service(CacheInterface::class)
            ->setValueByFactory(
                function () {
                    return new Cache();
                }
            )
        ;
    }

    public function modify(Container $container): void
    {
        $container->get("service.foo")->lock();
    }
}

$factory = new ContainerFactory();

$factory->add(new SampleProvider());

$container = $factory->create();

$foo = $container->get("service.foo");