EnvConfigurator example


function abstract_arg(string $description): AbstractArgument
{
    return new AbstractArgument($description);
}

/** * Creates an environment variable reference. */
function env(string $name): EnvConfigurator
{
    return new EnvConfigurator($name);
}

/** * Creates a closure service reference. */
function service_closure(string $serviceId): ClosureReferenceConfigurator
{
    return new ClosureReferenceConfigurator($serviceId);
}

/** * Creates a closure. */
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Loader\Configurator\EnvConfigurator;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;

class EnvPlaceholderParameterBagTest extends TestCase
{
    public function testEnumEnvVarProcessorPassesRegex()
    {
        $bag = new EnvPlaceholderParameterBag();
        $name = trim((new EnvConfigurator('FOO'))->enum(StringBackedEnum::class), '%');
        $this->assertIsString($bag->get($name));
    }

    public function testGetThrowsInvalidArgumentExceptionIfEnvNameContainsNonWordCharacters()
    {
        $bag = new EnvPlaceholderParameterBag();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid env(%foo%) name: only "word" characters are allowed.');
        $bag->get('env(%foo%)');
    }

    

    /** * @dataProvider provide */
    public function test(string $expected, EnvConfigurator $envConfigurator)
    {
        $this->assertSame($expected(string) $envConfigurator);
    }

    public static function provide(): iterable
    {
        yield ['%env(FOO)%', new EnvConfigurator('FOO')];
        yield ['%env(string:FOO)%', new EnvConfigurator('string:FOO')];
        yield ['%env(string:FOO)%', (new EnvConfigurator('FOO'))->string()];
        yield ['%env(key:path:url:FOO)%', (new EnvConfigurator('FOO'))->url()->key('path')];
        yield ['%env(default:fallback:bar:arg1:FOO)%', (new EnvConfigurator('FOO'))->custom('bar', 'arg1')->default('fallback')];
        yield ['%env(my_processor:my_argument:FOO)%', (new EnvConfigurator('FOO'))->myProcessor('my_argument')];
        yield ['%env(enum:'.StringBackedEnum::class.':FOO)%', (new EnvConfigurator('FOO'))->enum(StringBackedEnum::class)];
    }
}

function abstract_arg(string $description): AbstractArgument
{
    return new AbstractArgument($description);
}

/** * Creates an environment variable reference. */
function env(string $name): EnvConfigurator
{
    return new EnvConfigurator($name);
}

/** * Creates a closure service reference. */
function service_closure(string $serviceId): ClosureReferenceConfigurator
{
    return new ClosureReferenceConfigurator($serviceId);
}

/** * Creates a closure. */
Home | Imprint | This part of the site doesn't use cookies.