PHP code example of paysera / lib-dependency-injection

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

    

paysera / lib-dependency-injection example snippets


class SomeBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(new AddTaggedCompilerPass(
            'some_bundle.registry', // ID of service to modify
            'my_provider',          // name of tag to search for
            'addProvider',          // method to call on modified service
            [           // this parameter is optional and defines attributes to pass from tag
                'key',
                'theme' => 'default',   // attribute with default value
                'optional',
            ]
        ));
    }
}

class Registry
{
    // first - tagged service. Others (optional) in the order as they come in the attributes array
    public function addProvider(ProviderInterface $provider, $key, $theme, $optional = null)
    {
        $this->providers[$key] = $provider;    // or whatever
    }
}

class SomeBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass((new AddTaggedCompilerPass(
            'some_bundle.registry', // ID of service to modify
            'my_provider',          // name of tag to search for
            'addProvider',          // method to call on modified service
            ['key', 'theme' => 'default', 'optional'],
        ))->enablePriority()); // attribute name can be passed here, defaults to `priority`
    }
}

// priority -1 - smallest:
$registry->addProvider($niceProvider, 'nice', 'dark');
// priority defaults to 0, called in the order as registered:
$registry->addProvider($awesomeProvider, 'awesome', 'default');  
$registry->addProvider($anotherProvider, 'another', 'default');
// priority is over 9000:
$registry->addProvider($awesomeProvider, 'fallback', 'default', 'optional param');

class SomeBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass((new AddTaggedCompilerPass(
            'some_bundle.registry', // ID of service to modify
            'my_provider',          // name of tag to search for
            'addProvider',          // method to call on modified service
            ['key', 'theme' => 'default', 'optional'],
        ))->setCallMode(AddTaggedCompilerPass::CALL_MODE_ID));
    }
}

class Registry
{
    private $container;

    public function addProvider(string $providerId, $key, $theme, $optional = null)
    {
        $this->providers[$key] = $providerId;
    }
    
    private function getProvider(string $key)
    {
        return $this->container->get($this->providers[$key]);
    }
    
    // ...
}