1: <?php
2:
3: namespace PHPixie\Config\Storages\Type;
4:
5: class File extends \PHPixie\Slice\Data\Implementation
6: implements \PHPixie\Config\Storages\Storage\Editable\Persistable
7: {
8: protected $formats;
9: protected $file;
10:
11: protected $arrayData;
12: protected $format;
13:
14: protected $isLoaded = false;
15: protected $isModified = false;
16:
17: public function __construct($sliceBuilder, $formats, $file)
18: {
19: $this->formats = $formats;
20: $this->file = $file;
21: parent::__construct($sliceBuilder);
22: }
23:
24: public function getData($path = null, $isRequired = false, $default = null)
25: {
26: return $this->arrayData()->getData($path, $isRequired, $default);
27: }
28:
29: public function set($path, $value)
30: {
31: $this->arrayData()->set($path, $value);
32: $this->isModified = true;
33: }
34:
35: public function remove($path = null)
36: {
37: $this->arrayData()->remove($path);
38: $this->isModified = true;
39: }
40:
41: public function keys($path = null, $isRequired = false)
42: {
43: return $this->arrayData()->keys($path, $isRequired);
44: }
45:
46: public function slice($path = null)
47: {
48: return $this->arrayData()->slice($path);
49: }
50:
51: public function arraySlice($path = null)
52: {
53: return $this->arrayData()->arraySlice($path);
54: }
55:
56: public function getIterator()
57: {
58: return $this->arrayData()->getIterator();
59: }
60:
61: public function persist($removeIfEmpty = false)
62: {
63: if (!$this->isModified)
64: return;
65:
66: $data = $this->get(null, array());
67:
68: if(empty($data) && $removeIfEmpty && file_exists($this->file)) {
69: unlink($this->file);
70:
71: }else{
72: $this->format()->write($this->file, $data);
73:
74: }
75:
76: $this->isModified = false;
77: }
78:
79: protected function arrayData()
80: {
81: if($this->arrayData === null) {
82:
83: if(file_exists($this->file)) {
84: $data = $this->format()->read($this->file);
85: }else{
86: $data = array();
87: }
88:
89: $this->arrayData = $this->sliceBuilder->editableArrayData($data);
90: }
91:
92: return $this->arrayData;
93: }
94:
95: protected function format()
96: {
97: if($this->format === null) {
98: $this->format = $this->formats->getByFilename($this->file);
99: }
100: return $this->format;
101: }
102: }
103: