Config::getDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <https://github.com/baleen/migrations>.
19
 */
20
21
namespace Baleen\Cli\Config;
22
23
use Baleen\Cli\Provider\ApplicationProvider;
24
use Baleen\Cli\Provider\CommandsProvider;
25
use Baleen\Cli\Provider\HelperSetProvider;
26
use Baleen\Cli\Provider\RepositoryProvider;
27
use Baleen\Cli\Provider\StorageProvider;
28
use Baleen\Cli\Provider\TimelineProvider;
29
use Symfony\Component\Config\Definition\Processor;
30
31
/**
32
 * Class Config.
33
 *
34
 * @author Gabriel Somoza <[email protected]>
35
 */
36
class Config implements ConfigInterface
37
{
38
    const CONFIG_FILE_NAME = '.baleen.yml';
39
    const VERSIONS_FILE_NAME = '.baleen_versions';
40
41
    /** @var array */
42
    private $config;
43
44
    /**
45
     * @param array $config
46
     * @param bool  $defaults
47
     */
48
    public function __construct(array $config = [], $defaults = true)
49
    {
50
        $configs = [$config];
51
        if ($defaults) {
52
            // insert at the beginning of the array
53
            array_unshift($configs, $this->getDefaults());
54
        }
55
        $processor = new Processor();
56
        $config = $processor->processConfiguration(
57
            $this->getDefinition(),
58
            $configs
59
        );
60
        $this->config = $config;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getDefaults()
67
    {
68
        return [
69
            'providers' => $this->getProviderDefaults(),
70
            'migrations' => $this->getMigrationDefaults(),
71
            'storage' => $this->getStorageDefaults(),
72
            'plugins' => $this->getPluginDefaults(),
73
        ];
74
    }
75
76
    /**
77
     * getPluginDefaults
78
     * @return array
79
     */
80
    public function getPluginDefaults()
81
    {
82
        return []; // default to no plugins
83
    }
84
85
    /**
86
     * Default values for the migrations section.
87
     *
88
     * @return array
89
     */
90
    protected function getMigrationDefaults()
91
    {
92
        return [
93
            'directory' => 'migrations',
94
            'namespace' => 'Migrations',
95
        ];
96
    }
97
98
    /**
99
     * Default values for the storage section.
100
     *
101
     * @return array
102
     */
103
    protected function getStorageDefaults()
104
    {
105
        return [
106
            'file' => self::VERSIONS_FILE_NAME,
107
        ];
108
    }
109
110
    /**
111
     * Default values for the providers section.
112
     *
113
     * @return array
114
     */
115
    protected function getProviderDefaults()
116
    {
117
        return [
118
            'application' => ApplicationProvider::class,
119
            'storage' => StorageProvider::class,
120
            'repository' => RepositoryProvider::class,
121
            'timeline' => TimelineProvider::class,
122
            'helperSet' => HelperSetProvider::class,
123
            'commands' => CommandsProvider::class,
124
        ];
125
    }
126
127
    /**
128
     * getProviders.
129
     *
130
     * @return array
131
     */
132
    public function getProviders()
133
    {
134
        return $this->config['providers'];
135
    }
136
137
    /**
138
     * Returns a sorted list of plugins
139
     *
140
     * @return array
141
     */
142
    public function getPlugins()
143
    {
144
        $plugins = $this->config['plugins'];
145
        ksort($plugins, SORT_NUMERIC);
146
        return $plugins;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getMigrationsDirectoryPath()
153
    {
154
        return getcwd().DIRECTORY_SEPARATOR.$this->getMigrationsDirectory();
155
    }
156
157
    /**
158
     * @return mixed
159
     */
160
    public function getMigrationsDirectory()
161
    {
162
        return $this->config['migrations']['directory'];
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getMigrationsNamespace()
169
    {
170
        return $this->config['migrations']['namespace'];
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getStorageFilePath()
177
    {
178
        return getcwd().DIRECTORY_SEPARATOR.$this->getStorageFile();
179
    }
180
181
    /**
182
     * @return mixed
183
     */
184
    public function getStorageFile()
185
    {
186
        return $this->config['storage']['file'];
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function getConfigFilePath()
193
    {
194
        return getcwd().DIRECTORY_SEPARATOR.$this->getFileName();
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getFileName()
201
    {
202
        return self::CONFIG_FILE_NAME;
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    public function toArray()
209
    {
210
        return $this->config;
211
    }
212
213
    /**
214
     * getDefinition.
215
     *
216
     * @return Definition
217
     */
218
    public function getDefinition()
219
    {
220
        return new Definition();
221
    }
222
223
    /**
224
     * Returns a clone of itself but only with settings that can be configured by the end-user.
225
     *
226
     * @return array
227
     */
228
    public function getCleanArray()
229
    {
230
        $config = $this->config;
231
        unset($config['providers']);
232
233
        return $config;
234
    }
235
}
236