Completed
Push — master ( 2329f6...21d34e )
by Lucas
13:38 queued 03:35
created

EnvParametersCompilerPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 16 6
1
<?php
2
/** A compiler pass that passes $_SERVER/$_ENV vars into the container parameters bag */
3
4
namespace Graviton\CoreBundle\Compiler;
5
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
11
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
12
 * @link     http://swisscom.ch
13
 */
14
class EnvParametersCompilerPass implements CompilerPassInterface
15
{
16
17
    /**
18
     * this is a workaround for a new symfony feature:
19
     * https://github.com/symfony/symfony/issues/7555
20
     *
21
     * we *need* to be able to override any param with our env variables..
22
     * so we do again, what the kernel did already here.. ;-)
23
     *
24
     * Since fabpot seems to have said bye to this feature we are
25
     * re-implementing it here. We are also adding some fancy json
26
     * parsing for hashes and arrays while at it.
27
     *
28
     * @todo add proper documentation on this "feature" to a README
29
     *
30
     * @param ContainerBuilder $container Container
31
     *
32
     * @return void
33
     */
34
    public function process(ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
process uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
process uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
    {
36
        foreach (array_merge($_SERVER, $_ENV) as $key => $value) {
37
            if (0 === strpos($key, 'SYMFONY__')) {
38
                if (substr($value, 0, 1) == '[' || substr($value, 0, 1) == '{') {
39
                    $value = json_decode($value, true);
40
                    if (JSON_ERROR_NONE !== json_last_error()) {
41
                        throw new \RuntimeException(
42
                            sprintf('error "%s" in env variable "%s"', json_last_error_msg(), $key)
43
                        );
44
                    }
45
                }
46
                $container->setParameter(strtolower(str_replace('__', '.', substr($key, 9))), $value);
47
            }
48
        }
49
    }
50
}
51