Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

DefaultParameters::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\DependencyInjection;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Class DefaultParameters.
19
 */
20
class DefaultParameters implements ContainerAwareInterface
21
{
22
    /**
23
     * @var
24
     */
25
    private static $instance;
26
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
32
    /**
33
     * @param ContainerInterface $container
34
     */
35
    private function __construct(ContainerInterface $container)
36
    {
37
        $this->setContainer($container);
38
    }
39
40
    /**
41
     * defineDefaultConfiguration.
42
     *
43
     * @param array $parameters
44
     */
45
    public function defineDefaultConfiguration(array $parameters)
46
    {
47
        foreach ($parameters as $parameterKey => $change) {
48
            // retrieve current defined parameters
49
            $containerParameters = $this->container->getParameter($parameterKey);
50
51
            // replacing parameter only if defined in yml file
52
            if (array_key_exists('replace', $change)) {
53
                // default parameters in an array, so checking its content and replacing matching keys
54
                if (is_array($containerParameters) && is_array($change['default'])) {
55
                    foreach ($containerParameters as $defaultKey => $defaultValue) {
56
                        if (in_array($defaultValue, $change['default'])) {
57
                            $containerParameters[$defaultKey] = $change['replace'][$defaultKey];
58
                        }
59
                    }
60
                } elseif ($change['default'] == $containerParameters) {
61
                    // default parameters in a string, simply replacing it
62
                    $containerParameters = $change['replace'];
63
                }
64
65
                // overriding parameters with our default values
66
                $this->container->setParameter($parameterKey, $containerParameters);
67
            }
68
        }
69
    }
70
71
    /**
72
     * parameterExists.
73
     *
74
     * @param $name
75
     *
76
     * @return bool|mixed false if not defined, the parameter if exists
77
     */
78
    public function parameterExists($name)
79
    {
80
        if ($this->container->hasParameter($name)) {
81
            return $this->container->getParameter($name);
82
        } else {
83
            return false;
84
        }
85
    }
86
87
    /**
88
     * @return self
89
     */
90
    public static function getInstance(ContainerInterface $container)
91
    {
92
        if (self::$instance == null) {
93
            self::$instance = new self($container);
94
        }
95
96
        return self::$instance;
97
    }
98
99
    /**
100
     * Sets the Container.
101
     *
102
     * @param ContainerInterface|null $container A ContainerInterface instance or null
103
     *
104
     * @api
105
     */
106
    public function setContainer(ContainerInterface $container = null)
107
    {
108
        $this->container = $container;
109
    }
110
}
111