Passed
Push — master ( 7d250d...d2f71d )
by Caen
03:49 queued 11s
created

LoadConfiguration::providePharSupportIfNeeded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Internal;
6
7
use Illuminate\Contracts\Config\Repository as RepositoryContract;
8
use Illuminate\Contracts\Foundation\Application;
9
use Illuminate\Foundation\Bootstrap\LoadConfiguration as BaseLoadConfiguration;
10
11
/** @internal */
12
class LoadConfiguration extends BaseLoadConfiguration
13
{
14
    /** Get all the configuration files for the application. */
15
    protected function getConfigurationFiles(Application $app): array
16
    {
17
        return tap(parent::getConfigurationFiles($app), function (array &$files) use ($app): void {
18
            // Inject our custom config file which is stored in `app/config.php`.
19
            $files['app'] = $app->basePath().DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'config.php';
20
21
            $this->providePharSupportIfNeeded($files);
22
        });
23
    }
24
25
    /** Load the configuration items from all the files. */
26
    protected function loadConfigurationFiles(Application $app, RepositoryContract $repository): void
27
    {
28
        parent::loadConfigurationFiles($app, $repository);
29
30
        $this->mergeConfigurationFiles($repository);
31
    }
32
33
    private function mergeConfigurationFiles(RepositoryContract $repository): void
34
    {
35
        // These files do commonly not need to be customized by the user, so to get them out of the way,
36
        // we don't include them in the default project install.
37
38
        foreach (['view', 'cache', 'commands', 'torchlight'] as $file) {
39
            $this->mergeConfigurationFile($repository, $file);
40
        }
41
    }
42
43
    private function mergeConfigurationFile(RepositoryContract $repository, string $file): void
44
    {
45
        // We of course want the user to be able to customize the config files,
46
        // if they're present, so we'll merge their changes here.
47
48
        $repository->set($file, array_merge(
49
            require __DIR__."/../../../config/$file.php",
50
            (array) $repository->get($file, [])
51
        ));
52
    }
53
54
    /**
55
     * Provide support for running Hyde in a Phar archive.
56
     *
57
     * @experimental
58
     * @codeCoverageIgnore
59
     */
60
    private static function providePharSupportIfNeeded(array &$files): void
61
    {
62
        // If we're running in a Phar and no project config directory exists,
63
        // we need to adjust the path to use the bundled static Phar config file.
64
65
        if (\Phar::running() && (! is_dir($files['app']))) {
66
            $files['app'] = dirname(__DIR__, 6).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'app.php';
67
        }
68
    }
69
}
70