-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
150 lines (141 loc) · 5.02 KB
/
Config.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of the BeloConfigBundle package.
*
* (c) Loïc Beurlet <https://www.belo.lu/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Belo\ConfigBundle\Utils;
use Doctrine\ORM\EntityManager;
/**
* @author Loïc Beurlet
*/
class Config
{
private $em;
private $cache = array();
private $objectCache = null;
/**
* Config constructor. This method should not be called manually. Use the service instead.
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
$this->reloadCache();
}
private function reloadCache($forceObjectCache = false)
{
$rep = $this->em->getRepository('BeloConfigBundle:Config');
$cdata = $rep->findAll();
foreach($cdata as $data)
{
$this->cache[$data->getConfKey()] = $data->getConfValue();
if($forceObjectCache || $this->objectCache != null) {
$this->objectCache[$data->getConfKey()] = $data;
}
}
}
/**
* This method checks if a config key exists in the current configuration.
* @param $search: The config key you are looking for.
* @return bool: true if key was found, false if not.
* @throws \BadMethodCallException: if no config key is provided.
*/
public function exists($search)
{
if (null === $search) {
throw new \BadMethodCallException("No config key provided.");
}
if(!key_exists($search, $this->cache)) { return false; }
return true;
}
/**
* This method returns the config value attached to a given config key.
* @param $search: The config key you are trying to get the config value for.
* @return mixed: The config value attached to the given config key.
* @throws \BadMethodCallException: if no config key is provided or the key is unknown.
*/
public function get($search)
{
if (null === $search) {
throw new \BadMethodCallException("No config key provided.");
}
if(!key_exists($search, $this->cache)) { throw new \BadMethodCallException("The config key is unknown."); }
return $this->cache[$search];
}
/**
* This method sets a value for given config key. If a key does not yet exist, it will be created.
* If the third method argument is not manually set to true, you need to run flush() in order to save the values.
* @param $key: The config key.
* @param $value: The value for the config.
* @param bool $flushInstantly: Set to true, if data should instantly be flushed into database. Default: false
* @return bool: returns true if succeeded.
* @throws \BadMethodCallException: if no config key is provided or the key is unknown.
*/
public function set($key, $value, $flushInstantly = false)
{
if (null === $key) {
throw new \BadMethodCallException("No config key provided.");
}
if(!is_bool($flushInstantly)) {
throw new \BadMethodCallException("Third argument must be a boolean.");
}
if(key_exists($key, $this->cache))
{
if($value === $this->cache[$key]) {
return true;
}
if($this->objectCache === null) {
$this->reloadCache(true);
}
$actualConfig = $this->objectCache[$key];
$actualConfig->setConfValue($value);
$this->em->persist($actualConfig);
if($flushInstantly) {
$this->em->flush();
$this->reloadCache();
}
return true;
}
$newConfig = new \Belo\ConfigBundle\Entity\Config();
$newConfig->setConfKey($key);
$newConfig->setConfValue($value);
$this->em->persist($newConfig);
if($flushInstantly) {
$this->em->flush();
$this->reloadCache();
}
return true;
}
/**
* Saves all data to the database.
* @return bool: returns true if suceeded.
*/
public function flush()
{
$this->em->flush();
$this->reloadCache();
return true;
}
/**
* Removes a config key and its attached value.
* @param $search: The config key you want to remove.
* @return bool: Returns true if succeeded.
* @throws \BadMethodCallException: if no config key is provided or the key is unknown.
*/
public function remove($search)
{
if (null === $search) {
throw new \BadMethodCallException("No config key provided.");
}
if(!key_exists($search, $this->cache)) { throw new \BadMethodCallException("The config key is unknown."); }
$actualConfig = $this->em->getRepository('BeloConfigBundle:Config')->findOneBy(array('confKey' => $search));
$this->em->remove($actualConfig);
$this->em->flush();
$this->reloadCache();
return true;
}
}