PHP code example of mnapoli / assembly

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

    

mnapoli / assembly example snippets


class MyModuleDefinitionProvider extend \Assembly\ArrayDefinitionProvider
{
    public function getArrayDefinitions()
    {
        return [
            'logger.destination' => '/var/log/myapp.log',

            'logger' => \Assembly\object('MyLogger')
                ->setConstructorArguments('warning', \Assembly\get('logger.destination'))
                ->addMethodCall('setDebug', true),

            'super_mailer' => \Assembly\factory('MailerFactory', 'create'),

            'mailer' => \Assembly\get('super_mailer'),
        ];
    }
}

use function \Assembly\object;
use function \Assembly\get;

class MyModuleDefinitionProvider extend \Assembly\ArrayDefinitionProvider
{
    public function getArrayDefinitions()
    {
        return [
            'logger' => object(MyLogger::class),
            'logger_alias' => get('logger'),
        ];
    }
}

$provider = new ArrayDefinitionProvider([
    // add definitions here
]);

return [
    'db.port' => new ParameterDefinition(3306),
];

return [
    'logger' => new Reference('monolog'),
];

$definition = new ObjectDefinition('PDO');
$definition->addConstructorArgument('mysql:host=localhost;dbname=test');
$definition->addConstructorArgument('user');
$definition->addConstructorArgument('password');

$definition = new ObjectDefinition('PDO');
$definition->addConstructorArgument(new Reference('db.connection_string'));
$definition->addConstructorArgument('user');
$definition->addConstructorArgument('password');

$definition = new FactoryCallDefinition(new Reference('db.factory'), 'create');
$definition->setArguments(new Reference('db.connection_string'), 'user', 'password');

$definition = new FactoryCallDefinition('Acme\DbFactory', 'create');

// List the definition providers to load
$definitionProviders = [
    new Module1DefinitionProvider(),
    new Module2DefinitionProvider(),
];

// Define here container entries for the application
$entries = [
    'abc' => 'def',
    'router' => new Router(...),
];

$container = new Container($entries, $definitionProviders);

$resolver = new \Assembly\Container\DefinitionResolver($container);
$value = $resolver->resolve($definition);