/
YamlPersistence.php
74 lines (54 loc) · 2.07 KB
/
YamlPersistence.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
<?php namespace Comodojo\Installer\Persistence;
use \Symfony\Component\Yaml\Yaml;
use \Comodojo\Exception\InstallerException;
/**
* Dump configuration statements to a yaml file
*
* @package Comodojo Framework
* @author Marco Giovinazzi <marco.giovinazzi@comodojo.org>
* @license MIT
*
* LICENSE:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class YamlPersistence extends AbstractPersistence {
/**
* Default depth of yaml file
*
* This will be the default if no "depth" parameter is specified
*
* @const int
*/
const DEFAULT_DEPTH = 4;
/**
* {@inheritDoc}
*/
public function load() {
$name = $this->getParameters()->get('config-file');
if ( $name === null ) throw new InstallerException("No config-file name specified");
if ( file_exists($name) && is_readable($name) ) return Yaml::parse(file_get_contents($name));
return [];
}
/**
* {@inheritDoc}
*/
public function dump(array $data) {
$name = $this->getParameters()->get('config-file');
if ( $name === null ) throw new InstallerException("No config-file name specified");
$config_depth = $this->getParameters()->get('depth');
$depth = $config_depth === null ? self::DEFAULT_DEPTH : intval($config_depth);
$filedata = Yaml::dump($data, $depth);
if (
( file_exists($name) && is_writable($name) ) ||
is_writeable(pathinfo($name, PATHINFO_DIRNAME))
) return file_put_contents($name, $filedata, LOCK_EX);
throw new InstallerException("Cannot write to configuration file $name");
}
}