forked from jarves/jarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JarvesConfig.php
105 lines (88 loc) · 3.04 KB
/
JarvesConfig.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <[email protected]>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
use Jarves\Configuration\Model;
use Jarves\Configuration\SystemConfig;
use Propel\Generator\Exception\BuildException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class JarvesConfig
{
/**
* @var SystemConfig
*/
protected $systemConfig;
/**
* @var string
*/
private $rootDir;
/**
* @var string
*/
private $environment;
/**
* @var ContainerInterface
*/
private $container;
/**
* @param string $rootDir
* @param string $environment
* @param ContainerInterface $container
*/
public function __construct($rootDir, $environment, ContainerInterface $container)
{
$this->rootDir = $rootDir;
$this->environment = $environment;
$this->container = $container;
}
/**
* @param bool $withCache
*
* @return SystemConfig
*/
public function getSystemConfig($withCache = true)
{
if (null === $this->systemConfig) {
$configFile = $this->rootDir . '/config/config.jarves.xml';
$configEnvFile = $this->rootDir . '/config/config.jarves_' . $this->environment . '.xml';
if (file_exists($configEnvFile)) {
$configFile = $configEnvFile;
}
$cacheFile = $configFile . '.cache.php';
$systemConfigCached = @file_get_contents($cacheFile);
$cachedSum = 0;
if ($systemConfigCached) {
$cachedSum = substr($systemConfigCached, 0, 32);
$systemConfigCached = substr($systemConfigCached, 33);
}
$systemConfigHash = file_exists($configFile) ? md5(filemtime($configFile)) : -1;
if ($withCache && $systemConfigCached && $cachedSum === $systemConfigHash) {
$this->systemConfig = @unserialize($systemConfigCached);
}
if (!$this->systemConfig) {
$configXml = file_exists($configFile) ? file_get_contents($configFile) : [];
$this->systemConfig = new SystemConfig($configXml);
file_put_contents($cacheFile, $systemConfigHash . "\n" . serialize($this->systemConfig));
}
if (!$this->systemConfig->getDatabase() || !$this->systemConfig->getDatabase()->isValid()) {
$database = $this->container->get('jarves.configuration.database');
$this->systemConfig->setDatabase($database);
if (!$database->isValid()) {
throw new BuildException('Jarves uses the database credentials from parameters.yml. ' .
'You did not configure all parameters.');
}
}
}
return $this->systemConfig;
}
}