Passed
Pull Request — develop (#9)
by Brent
05:16 queued 02:06
created

Config::getDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Finder\SplFileInfo;
7
use Symfony\Component\Yaml\Yaml;
8
9
/**
10
 * Config helper class
11
 *
12
 * @package Brendt\Stitcher
13
 */
14
class Config
15
{
16
    const ASYNC = 'async';
17
    const CACHE_CDN = 'cache.cdn';
18
    const CACHE_IMAGES = 'cache.images';
19
    const CDN = 'cdn';
20
    const DIRECTORIES_CACHE = 'directories.cache';
21
    const DIRECTORIES_PUBLIC = 'directories.public';
22
    const DIRECTORIES_SRC = 'directories.src';
23
    const ENGINES_ASYNC = 'engines.async';
24
    const ENGINES_IMAGE = 'engines.image';
25
    const ENGINES_OPTIMIZER = 'engines.optimizer';
26
    const ENGINES_TEMPLATE = 'engines.template';
27
    const ENVIRONMENT = 'environment';
28
    const META = 'meta';
29
    const MINIFY = 'minify';
30
    const OPTIMIZER_OPTIONS = 'optimizer.options';
31
    const REDIRECT_HTTPS = 'redirect.https';
32
    const REDIRECT_WWW = 'redirect.www';
33
    const SITEMAP_URL = 'sitemap.url';
34
35
    public static function getDefaults(): array {
36
        return [
37
            self::ASYNC                => true,
38
            self::ENVIRONMENT        => 'development',
39
            self::DIRECTORIES_SRC    => './src',
40
            self::DIRECTORIES_PUBLIC => './public',
41
            self::DIRECTORIES_CACHE  => './.cache',
42
            self::META               => [],
43
            self::MINIFY             => false,
44
            self::ENGINES_TEMPLATE   => 'smarty',
45
            self::ENGINES_IMAGE      => 'gd',
46
            self::ENGINES_OPTIMIZER  => true,
47
            self::ENGINES_ASYNC      => true,
48
            self::CDN                => [],
49
            self::CACHE_IMAGES       => true,
50
            self::CACHE_CDN          => true,
51
            self::REDIRECT_WWW       => false,
52
            self::REDIRECT_HTTPS     => false,
53
            self::OPTIMIZER_OPTIONS  => [],
54
            self::SITEMAP_URL        => null,
55
        ];
56
    }
57
58
    public static function parseImports(array $config): array {
59
        if (!isset($config['imports'])) {
60
            return $config;
61
        }
62
63
        $mergedConfig = [];
64
65
        foreach ($config['imports'] as $import) {
66
            $importConfig = self::parseImports(Yaml::parse(self::getConfigFile($import)->getContents()));
67
68
            $mergedConfig = array_replace_recursive($mergedConfig, $importConfig);
69
        }
70
71
        $mergedConfig = array_replace_recursive($mergedConfig, $config);
72
73
        return $mergedConfig;
74
    }
75
76
    public static function getConfigFile(string $path) {
77
        $pathParts = explode('/', $path);
78
        $configFileName = array_pop($pathParts);
79
        $configPath = implode('/', $pathParts) . '/';
80
81
        $configFiles = Finder::create()->files()->in($configPath)->name($configFileName)->depth(0)->getIterator();
82
        $configFiles->rewind();
83
84
        return $configFiles->current();
85
    }
86
87
    public static function flatten(array $config, string $prefix = ''): array {
88
        $result = [];
89
90
        foreach ($config as $key => $value) {
91
            $new_key = $prefix . (empty($prefix) ? '' : '.') . $key;
92
93
            if (is_array($value) && count($value)) {
94
                $result = array_merge($result, self::flatten($value, $new_key));
95
            } else {
96
                $result[$new_key] = $value;
97
            }
98
        }
99
100
        return $result;
101
    }
102
}
103