PHP code example of clementdecou / qube

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

    

clementdecou / qube example snippets


use Qube\Container;

$container = new Container();

use Amorfx\Qube\qube;

$container = qube();

$container->set('my_service', new MyService());

use Amorfx\Qube\DependencyInjection\ContainerInterface;

$container->set('my_service', function (ContainerInterface $container) {
    return new MyService($container->get('service1'), $container->get('service2'));
});

$container->set('my_service', new MyService(), false);

$container->set('my_service', new MyService(), tags: ['tag1', 'tag2']);

$service = $container->get('my_service');

$services = $container->getByTag('tag1');

$container->setParameter('my_param', 'my_value');

$param = $container->getParameter('my_param'); // return my_value

use Amorfx\Qube\DependencyInjection\ServiceProviderInterface;
use Amorfx\Qube\DependencyInjection\Container;
class ServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container): void
    {
        // register services and parameters or other providers
    }
}

$container->registerProviders([new ServiceProvider()]);
// OR
$container->registerProvider(new ServiceProvider());

use Amorfx\Qube\DependencyInjection\ContainerBuilder;
use Amorfx\Qube\quBuilder;

$builder = new ContainerBuilder(configFilePath: __DIR__ . '/config.php');
$container = $builder->get();

// OR use helper function
$container = quBuilder()
->fromConfig(__DIR__ . '/config.php')
->get();

// Or directly from an array
$container = (new ContainerBuilder(config: [...]))->get();

return [
    'parameters' => [
        //list of parameter identifier with their value
        'param1' => 'value1',
        // ...
    ],
    
    'services' => [ // list of you services
    
        'service1' => [ // the service identifier
            'object' => // an instance of your service or use factory
            'factory' => function (ContainerInterface $container) {
                // the closure to return your service instance
            },
            'tags' => [], // a list of tags
            'share' => false, // if the service is shareable, default is true
        ],
        // ...
    ],
    
    'providers' => [
        // the list of your providers class name
    ],
];