Passed
Push — master ( bf6fe2...7be6b5 )
by Caen
03:54 queued 12s
created

LoadConfiguration   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 67
rs 10
c 3
b 0
f 0
wmc 12

6 Methods

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