-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.php
72 lines (58 loc) · 2.27 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Bolt\Extension\WMC\ParentTheme;
use Bolt\Application;
use Bolt\BaseExtension;
use Bolt\Helpers\Arr;
use Bolt\Config;
class Extension extends BaseExtension
{
public function initialize() {
$config = $this->app['config'];
$end = $config->getWhichEnd($config->get('general/branding/path'));
// only run on frontend
if ($end == 'frontend') {
$parent = $config->get('theme/parent');
if ($parent) {
$this->loadThemeConfig($config, $parent, $config->get('general/theme'));
$this->app['twig.path'] = $config->get('twigpath');
}
}
}
private function loadThemeConfig(Config $config, $theme, $childTheme)
{
$themePath = $this->app['resources']->getPath('themebase') . '/' . $theme;
if (file_exists($themePath)) {
$configFile = $themePath . '/config.yml';
// insert parent path right after child path.
$twigPath = $config->get('twigpath');
if ($twigPath) {
$childThemePath = $this->app['resources']->getPath('themebase') . '/' . $childTheme;
$key = array_search($childThemePath, $twigPath);
if ($key !== false) {
array_splice($twigPath, $key, 1, array($childThemePath, $themePath));
}
$config->set('twigpath', $twigPath);
}
if (file_exists($configFile)) {
$themeConfig = self::mergeConfigFile($configFile);
if ($themeConfig) {
// load parent theme config, but without overwriting, because child prevails
$config->set('theme', Arr::mergeRecursiveDistinct($themeConfig, $config->get('theme')));
// multiple levels allowed
if (!empty($themeConfig['parent'])) {
$this->loadThemeConfig($config, $themeConfig['parent'], $theme);
}
}
}
}
}
public function getName()
{
return "ParentTheme";
}
private static function mergeConfigFile($file)
{
$yamlparser = new \Symfony\Component\Yaml\Parser();
return $yamlparser->parse(file_get_contents($file) . "\n");
}
}