PHP code example of ruudk / symfony-config-code-generator

1. Go to this page and download the library: Download ruudk/symfony-config-code-generator 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/ */

    

ruudk / symfony-config-code-generator example snippets




declare(strict_types=1);

rator\SymfonyConfigCodeGenerator;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;

$container = new ContainerBuilder();

// Parameters
$container->setParameter('app.debug', true);
$container->setParameter('database.url', '%env(DATABASE_URL)%');

// Simple service with autowiring
$container->register('app.logger', 'Psr\Log\LoggerInterface')
    ->setAutowired(true);

// Service with arguments and method calls
$container->register('app.mailer', 'App\Service\MailerService')
    ->addArgument(new Reference('mailer.transport'))
    ->addArgument('%database.url%')
    ->addMethodCall('setLogger', [new Reference('app.logger')])
    ->addMethodCall('configure', [[
        'from' => '[email protected]',
    ]]);

// Event listener with tags
$container->register('app.request_listener', 'App\EventListener\RequestListener')
    ->addTag('kernel.event_listener', [
        'event' => 'kernel.request',
        'priority' => 100,
    ]);

// Service with expression
$container->register('app.feature_service', 'App\Service\FeatureService')
    ->addArgument(new Expression('parameter("app.debug") ? "debug" : "production"'));

// Service with tagged iterator
$container->register('app.handler_registry', 'App\Service\HandlerRegistry')
    ->addArgument(new TaggedIteratorArgument('app.handler'));

// Tagged services
$container->register('app.user_handler', 'App\Handler\UserHandler')
    ->addTag('app.handler');

// Generate the configuration
echo new SymfonyConfigCodeGenerator()->dumpFile($container);



declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
use function Symfony\Component\DependencyInjection\Loader\Configurator\expr;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

// This file was automatically generated and should not be edited.

return static function (ContainerConfigurator $configurator) : void {
    $parameters = $configurator->parameters();
    $parameters->set(
        'app.debug',
        true,
    );
    $parameters->set(
        'database.url',
        env('DATABASE_URL'),
    );

    $services = $configurator->services();

    $services->set(
        'app.feature_service',
        App\Service\FeatureService::class,
    )
        ->args(
            [
                expr('parameter("app.debug") ? "debug" : "production"'),
            ],
        );

    $services->set(
        'app.handler_registry',
        App\Service\HandlerRegistry::class,
    )
        ->args(
            [
                tagged_iterator('app.handler'),
            ],
        );

    $services->set(
        'app.logger',
        Psr\Log\LoggerInterface::class,
    )
        ->autowire();

    $services->set(
        'app.mailer',
        App\Service\MailerService::class,
    )
        ->args(
            [
                service('mailer.transport'),
                param('database.url'),
            ],
        )
        ->call(
            'setLogger',
            [
                service('app.logger'),
            ],
        )
        ->call(
            'configure',
            [
                [
                    'from' => '[email protected]',
                ],
            ],
        );

    $services->set(
        'app.request_listener',
        App\EventListener\RequestListener::class,
    )
        ->tag(
            'kernel.event_listener',
            [
                'event' => 'kernel.request',
                'priority' => 100,
            ],
        );

    $services->set(
        'app.user_handler',
        App\Handler\UserHandler::class,
    )
        ->tag('app.handler');
};