Passed
Push — master ( dfdc1d...0594dd )
by Caen
03:44 queued 13s
created

LoadConfiguration::mergeConfigurationFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
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 Phar;
8
use Illuminate\Contracts\Foundation\Application;
9
use Illuminate\Contracts\Config\Repository;
10
use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration;
11
12
use function getenv;
13
use function array_merge;
14
use function dirname;
15
use function in_array;
16
use function is_dir;
17
use function tap;
18
19
/** @internal */
20
class LoadConfiguration extends BaseLoadConfiguration
21
{
22
    /** Get all the configuration files for the application. */
23
    protected function getConfigurationFiles(Application $app): array
24
    {
25
        return (array) tap(parent::getConfigurationFiles($app), function (array &$files) use ($app): void {
26
            // Inject our custom config file which is stored in `app/config.php`.
27
            $files['app'] = $app->basePath().DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'config.php';
28
29
            $this->providePharSupportIfNeeded($files);
30
        });
31
    }
32
33
    /** Load the configuration items from all the files. */
34
    protected function loadConfigurationFiles(Application $app, Repository $repository): void
35
    {
36
        parent::loadConfigurationFiles($app, $repository);
37
38
        $this->mergeConfigurationFiles($repository);
39
40
        $this->loadRuntimeConfiguration($app, $repository);
41
    }
42
43
    private function mergeConfigurationFiles(Repository $repository): void
44
    {
45
        // These files do commonly not need to be customized by the user, so to get them out of the way,
46
        // we don't include them in the default project install.
47
48
        foreach (['view', 'cache', 'commands', 'torchlight'] as $file) {
49
            $this->mergeConfigurationFile($repository, $file);
50
        }
51
    }
52
53
    private function mergeConfigurationFile(Repository $repository, string $file): void
54
    {
55
        // We of course want the user to be able to customize the config files,
56
        // if they're present, so we'll merge their changes here.
57
58
        $repository->set($file, array_merge(
59
            (array) require __DIR__."/../../../config/$file.php",
60
            (array) $repository->get($file, [])
61
        ));
62
    }
63
64
    /**
65
     * Provide support for running Hyde in a Phar archive.
66
     *
67
     * @experimental
68
     *
69
     * @codeCoverageIgnore
70
     */
71
    private static function providePharSupportIfNeeded(array &$files): void
72
    {
73
        // If we're running in a Phar and no project config directory exists,
74
        // we need to adjust the path to use the bundled static Phar config file.
75
76
        /** @var array{app: string} $files */
77
        if (Phar::running() && (! is_dir($files['app']))) {
78
            $files['app'] = dirname(__DIR__, 6).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php';
79
        }
80
    }
81
82
    private function loadRuntimeConfiguration(Application $app, Repository $repository): void
83
    {
84
        if ($app->runningInConsole()) {
85
            if ($this->getArgv() !== null) {
86
                $this->mergeCommandLineArguments($repository, '--pretty-urls', 'hyde.pretty_urls', true);
87
                $this->mergeCommandLineArguments($repository, '--no-api', 'hyde.api_calls', false);
88
            }
89
90
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_SAVE_PREVIEW', 'hyde.server.save_preview');
91
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_SERVER_DASHBOARD', 'hyde.server.dashboard.enabled');
92
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PRETTY_URLS', 'hyde.pretty_urls');
93
            $this->mergeRealtimeCompilerEnvironment($repository, 'HYDE_PLAY_CDN', 'hyde.use_play_cdn');
94
        }
95
    }
96
97
    private function mergeCommandLineArguments(Repository $repository, string $argumentName, string $configKey, bool $value): void
98
    {
99
        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

99
        if (in_array($argumentName, /** @scrutinizer ignore-type */ $this->getArgv(), true)) {
Loading history...
100
            $repository->set($configKey, $value);
101
        }
102
    }
103
104
    private function mergeRealtimeCompilerEnvironment(Repository $repository, string $environmentKey, string $configKey): void
105
    {
106
        if ($this->getEnv($environmentKey) !== false) {
107
            $repository->set($configKey, $this->getEnv($environmentKey) === 'enabled');
108
        }
109
    }
110
111
    protected function getArgv(): ?array
112
    {
113
        return $_SERVER['argv'] ?? null;
114
    }
115
116
    protected function getEnv(string $name): string|false|null
117
    {
118
        return getenv($name);
119
    }
120
}
121