Symfony Environments

Symfony has the concept of Environments which for the most part handle configuration differences

https://symfony.com/doc/current/configuration.html

Debug

Along with environments, there is also a global concept of "debug" - in any environemt, the application can be set to be in debug mode.

By default, this is the case in test and dev enviroments

Kernel

The place to get current environment and debug information is the Kernel

1
2
3
<?php
$kernel->getEnvironment();
$kernel->isDebug();

Environment Helper

Generally though, passing around the full Kernel object is overkill. Instead, we suggest you create an EnvironmentHelper which provides read only access to some Kernel properties

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

declare(strict_types=1);

namespace MyProject\Helper;

use Symfony\Component\HttpKernel\KernelInterface;

/**
 * A simple service to provide read only access to some Kernel parameters.
 */
final class EnvironmentHelper
{
    /**
     * @var string
     */
    private $environment;
    /**
     * @var bool
     */
    private $isDebug;
    /**
     * @var string
     */
    private $projectDir;

    public function __construct(KernelInterface $kernel)
    {
        $this->environment = $kernel->getEnvironment();
        $this->isDebug     = $kernel->isDebug();
        $this->projectDir  = $kernel->getProjectDir();
    }

    /**
     * The current environment, eg dev, prod, test
     *
     * @return string
     */
    public function getEnvironment(): string
    {
        return $this->environment;
    }

    /**
     * Are we currently in debug mode?
     *
     * @return bool
     */
    public function isDebug(): bool
    {
        return $this->isDebug;
    }

    /**
     * The project root directory, eg where the composer.json file is
     *
     * @return string
     */
    public function getProjectDir(): string
    {
        return $this->projectDir;
    }
}