Passed
Push — master ( fd4a88...7449e0 )
by Caen
04:01 queued 12s
created

LoadYamlConfiguration::hasYamlConfigFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Internal;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Symfony\Component\Yaml\Yaml;
10
use function file_get_contents;
11
use function array_merge;
12
use function file_exists;
13
14
/**
15
 * @internal
16
 *
17
 * @see \Hyde\Framework\Testing\Feature\YamlConfigurationServiceTest
18
 */
19
class LoadYamlConfiguration
20
{
21
    /**
22
     * Performs a core task that needs to be performed on
23
     * early stages of the framework.
24
     */
25
    public function bootstrap(): void
26
    {
27
        if ($this->hasYamlConfigFile()) {
28
            $this->mergeParsedConfiguration();
29
        }
30
    }
31
32
    protected function hasYamlConfigFile(): bool
33
    {
34
        return file_exists(Hyde::path('hyde.yml'))
35
            || file_exists(Hyde::path('hyde.yaml'));
36
    }
37
38
    protected function mergeParsedConfiguration(): void
39
    {
40
        Config::set('hyde', array_merge(
41
            Config::getArray('hyde', []),
42
            $this->getYaml()
43
        ));
44
    }
45
46
    protected function getYaml(): array
47
    {
48
        return (array) Yaml::parse(file_get_contents($this->getFile()));
49
    }
50
51
    protected function getFile(): string
52
    {
53
        return file_exists(Hyde::path('hyde.yml'))
54
            ? Hyde::path('hyde.yml')
55
            : Hyde::path('hyde.yaml');
56
    }
57
}
58