PHP code example of decodelabs / pandora

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

    

decodelabs / pandora example snippets


use DecodeLabs\Pandora\Container;

$container = new Container();

use My\Library\CoolInterface;
use My\Library\CoolImplementation; // Implements CoolInterface

// Instance
$container->bind(CoolInterface::class, new CoolImplementation());
$imp = $container->get(CoolInterface::class);

// Reused class
$container->bind(CoolInterface::class, CoolImplementation::class);
// Creates a new instace every call
$imp = $container->get(CoolInterface::class);

// Shared class
$container->bindShared(CoolInterface::class, CoolImplementation::class);
// Stores created instance and returns that each call
$imp = $container->get(CoolInterface::class);

// Locked instance - will only bind if CoolInterface has not been bound before
$container->bindLocked(CoolInterface::class, new CoolImplementation());
$imp = $container->get(CoolInterface::class);

// Bind a factory
$container->bind(CoolInterface::class, function($container) {
    return new CoolImplementation();
})

// Group multiple bindings
$container->bindToGroup(CoolInterface::class, new CoolImplementation(1));
$container->bindToGroup(CoolInterface::class, new CoolImplementation(2));
$container->bindToGroup(CoolInterface::class, new CoolImplementation(3));
$group = $container->getGroup(CoolInterface::class); // Contains 3 Implementations

$container->each(CoolInterface::class, function($instance, $container) {
    // Do something with each instance
});

// Aliased instance
$container->bind(CoolInterface::class, new CoolImplementation())
    ->alias('cool.thing');
$imp = $container->get('cool.thing');

// Or
$container->registerAlias(CoolInterface::class, 'cool.thing');
$container->bind(CoolInterface::class, CoolImplementation::class);
$imp = $container->get('cool.thing');

$imp = $container->getWith(CoolInterface::class, ['list', 'of', 'params']);

// Or inject parameters for later:
$container->inject(CoolInterface::class, 'paramName', 'paramValue');
$imp = $container->get(CoolInterface::class);

$imp = $container[CoolInterface::class];

if(isset($container[CoolInterface::class])) {
    unset($container[CoolInterface::class]);
}

$binding = $container->{CoolInterface::class};

// Or
$binding = $container->getBinding(CoolInterface::class);

$container->afterResolving(CoolInterface::class, function($instance, $container) {
    // Prepare instance
});

$container->afterRebinding(CoolInterface::class, function($instance, $container) {
    // Prepare instance again
});

// Triggers resolve callback once
$imp = $container->get(CoolInterface::class);

// Triggers rebinding callback
$container->bind(CoolInterface::class, new AnotherImplementation());

use DecodeLabs\Pandora\Provider;
use DecodeLabs\Pandora\Container;

class CoolService implements Provider {

    public static function getProvidedServices(): array {
        return [
            CoolInterface::class,
            OtherInterface::class
        ];
    }

    public function registerServices(Container $container): void {
        $container->bindShared(CoolInterface::class, CoolImplementation::class)
            ->alias('cool.thing');

        // Create factory closure
        $container->bind(OtherInterface::class, function($container) {
            return new class implements OtherInterface {

                public function hello(): string {
                    return 'world';
                }
            };
        });
    }
}


$conainer->registerProvider(CoolService::class);