LoadConfiguration::loadConfigurationFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
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 Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Contracts\Config\Repository;
9
use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration;
10
11
use function getenv;
12
use function array_merge;
13
use function in_array;
14
use function tap;
15
16
/** @internal */
17
class LoadConfiguration extends BaseLoadConfiguration
18
{
19
    /** Get all the configuration files for the application. */
20
    protected function getConfigurationFiles(Application $app): array
21
    {
22
        return (array) tap(parent::getConfigurationFiles($app), /** @param array<string, string> $files */ function (array &$files) use ($app): void {
23
            // Inject our custom config file which is stored in `app/config.php`.
24
            $files['app'] ??= $app->basePath().DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'config.php';
25
        });
26
    }
27
28
    /** Load the configuration items from all the files. */
29
    protected function loadConfigurationFiles(Application $app, Repository $repository): void
30
    {
31
        parent::loadConfigurationFiles($app, $repository);
32
33
        $this->mergeConfigurationFiles($repository);
34
35
        $this->loadRuntimeConfiguration($app, $repository);
36
    }
37
38
    private function mergeConfigurationFiles(Repository $repository): void
39
    {
40
        // These files do commonly not need to be customized by the user, so to get them out of the way,
41
        // we don't include them in the default project install.
42
43
        foreach (['view', 'cache', 'commands', 'torchlight'] as $file) {
44
            $this->mergeConfigurationFile($repository, $file);
45
        }
46
    }
47
48
    private function mergeConfigurationFile(Repository $repository, string $file): void
49
    {
50
        // We of course want the user to be able to customize the config files,
51
        // if they're present, so we'll merge their changes here.
52
53
        $repository->set($file, array_merge(
54
            (array) require __DIR__."/../../../config/$file.php",
55
            (array) $repository->get($file, [])
56
        ));
57
    }
58
59
    private function loadRuntimeConfiguration(Application $app, Repository $repository): void
60
    {
61
        if ($app->runningInConsole()) {
62
            if ($this->getArgv() !== null) {
63
                $this->mergeCommandLineArguments($repository, '--pretty-urls', 'hyde.pretty_urls', true);
64
                $this->mergeCommandLineArguments($repository, '--no-api', 'hyde.api_calls', false);
65
            }
66
67
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_SAVE_PREVIEW', 'hyde.server.save_preview');
68
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled');
69
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PRETTY_URLS', 'hyde.pretty_urls');
70
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PLAY_CDN', 'hyde.use_play_cdn');
71
        }
72
    }
73
74
    private function mergeCommandLineArguments(Repository $repository, string $argumentName, string $configKey, bool $value): void
75
    {
76
        if (in_array($argumentName, $this->getArgv(), true)) {
0 ignored issues
show
Bug introduced by
It seems like $this->getArgv() can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        if (in_array($argumentName, /** @scrutinizer ignore-type */ $this->getArgv(), true)) {
Loading history...
77
            $repository->set($configKey, $value);
78
        }
79
    }
80
81
    private function mergeRealtimeCompilerEnvironment(Repository $repository, string $environmentKey, string $configKey): void
82
    {
83
        if ($this->getEnv($environmentKey) !== false) {
84
            $repository->set($configKey, $this->getEnv($environmentKey) === 'enabled');
85
        }
86
    }
87
88
    protected function getArgv(): ?array
89
    {
90
        return $_SERVER['argv'] ?? null;
91
    }
92
93
    protected function getEnv(string $name): string|false|null
94
    {
95
        return getenv($name);
96
    }
97
}
98