|
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_IMAGE = 'engines.image'; |
|
24
|
|
|
const ENGINES_OPTIMIZER = 'engines.optimizer'; |
|
25
|
|
|
const ENGINES_TEMPLATE = 'engines.template'; |
|
26
|
|
|
const ENGINES_MINIFIER = 'engines.minifier'; |
|
27
|
|
|
const ENVIRONMENT = 'environment'; |
|
28
|
|
|
const META = 'meta'; |
|
29
|
|
|
const OPTIMIZER_OPTIONS = 'optimizer.options'; |
|
30
|
|
|
const REDIRECT_HTTPS = 'redirect.https'; |
|
31
|
|
|
const REDIRECT_WWW = 'redirect.www'; |
|
32
|
|
|
const SITEMAP_URL = 'sitemap.url'; |
|
33
|
|
|
|
|
34
|
|
|
public static function getDefaults(): array { |
|
35
|
|
|
return [ |
|
36
|
|
|
self::ASYNC => true, |
|
37
|
|
|
self::ENVIRONMENT => 'development', |
|
38
|
|
|
self::DIRECTORIES_SRC => './src', |
|
39
|
|
|
self::DIRECTORIES_PUBLIC => './public', |
|
40
|
|
|
self::DIRECTORIES_CACHE => './.cache', |
|
41
|
|
|
self::META => [], |
|
42
|
|
|
self::ENGINES_MINIFIER => false, |
|
43
|
|
|
self::ENGINES_TEMPLATE => 'smarty', |
|
44
|
|
|
self::ENGINES_IMAGE => 'gd', |
|
45
|
|
|
self::ENGINES_OPTIMIZER => true, |
|
46
|
|
|
self::CDN => [], |
|
47
|
|
|
self::CACHE_IMAGES => true, |
|
48
|
|
|
self::CACHE_CDN => true, |
|
49
|
|
|
self::REDIRECT_WWW => false, |
|
50
|
|
|
self::REDIRECT_HTTPS => false, |
|
51
|
|
|
self::OPTIMIZER_OPTIONS => [], |
|
52
|
|
|
self::SITEMAP_URL => null, |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function parseImports(array $config): array { |
|
57
|
|
|
if (!isset($config['imports'])) { |
|
58
|
|
|
return $config; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$mergedConfig = []; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($config['imports'] as $import) { |
|
64
|
|
|
$importConfig = self::parseImports(Yaml::parse(self::getConfigFile($import)->getContents())); |
|
65
|
|
|
|
|
66
|
|
|
$mergedConfig = array_replace_recursive($mergedConfig, $importConfig); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$mergedConfig = array_replace_recursive($mergedConfig, $config); |
|
70
|
|
|
|
|
71
|
|
|
return $mergedConfig; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function getConfigFile(string $path) { |
|
75
|
|
|
$pathParts = explode('/', $path); |
|
76
|
|
|
$configFileName = array_pop($pathParts); |
|
77
|
|
|
$configPath = implode('/', $pathParts) . '/'; |
|
78
|
|
|
|
|
79
|
|
|
$configFiles = Finder::create()->files()->in($configPath)->name($configFileName)->depth(0)->getIterator(); |
|
80
|
|
|
$configFiles->rewind(); |
|
81
|
|
|
|
|
82
|
|
|
return $configFiles->current(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function flatten(array $config, string $prefix = ''): array { |
|
86
|
|
|
$result = []; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($config as $key => $value) { |
|
89
|
|
|
$new_key = $prefix . (empty($prefix) ? '' : '.') . $key; |
|
90
|
|
|
|
|
91
|
|
|
if (is_array($value) && count($value)) { |
|
92
|
|
|
$result = array_merge($result, self::flatten($value, $new_key)); |
|
93
|
|
|
} else { |
|
94
|
|
|
$result[$new_key] = $value; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|