Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
LoadConfiguration
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
9 / 9
14
100.00% covered (success)
100.00%
1 / 1
 getConfigurationFiles
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 loadConfigurationFiles
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 mergeConfigurationFiles
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 mergeConfigurationFile
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 loadRuntimeConfiguration
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 mergeCommandLineArguments
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 mergeRealtimeCompilerEnvironment
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getArgv
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEnv
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation\Internal;
6
7use Illuminate\Contracts\Foundation\Application;
8use Illuminate\Contracts\Config\Repository;
9use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration;
10
11use function getenv;
12use function array_merge;
13use function in_array;
14use function tap;
15
16/** @internal */
17class 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)) {
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}