symplify/php-config-printer

Print Symfony services array with configuration to to plain PHP file format thanks to this simple php-parser wrapper


README

Downloads total

Print Symfony services array with configuration to to plain PHP file format thanks to this simple php-parser wrapper

Install

composer require symplify/php-config-printer --dev

Register bundle in your Kernel:

namespace App;

use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symplify\PhpConfigPrinter\Bundle\PhpConfigPrinterBundle;

final class AppKernel
{
    /**
     * @return BundleInterface[]
     */
    public function registerBundles(): array
    {
        return [new PhpConfigPrinterBundle()];
    }
}

Use

1. Only Configured Services

use Symplify\PhpConfigPrinter\Printer\SmartPhpConfigPrinter;

/** @var SmartPhpConfigPrinter $smartConfigPrinter */
$config = [
    'SomeService' => [
        'key' => 'value',
    ],
];

$smartConfigPrinter->printConfiguredServices($config);

2. Full Config

use Symplify\PhpConfigPrinter\YamlToPhpConverter;

class SomeClass
{
    /**
     * @var YamlToPhpConverter
     */
    private $yamlToPhpConverter;

    public function __construct(YamlToPhpConverter $yamlToPhpConverter)
    {
        $this->yamlToPhpConverter = $yamlToPhpConverter;
    }

    public function run(): void
    {
        $phpFileContent = $this->yamlToPhpConverter->convertYamlArray([
            'parameters' => [
                'key' => 'value',
            ],
            'services' => [
                '_defaults' => [
                    'autowire' => true,
                    'autoconfigure' => true,
                ],
            ],
        ]);

        // dump the $phpFileContent file
        // ...
    }
}