1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * An abstract representation of file and directory pathnames.
22
 *
23
 * @package phing.util
24
 * @author  Michiel Rook <mrook@php.net>
25
 */
26
class DataStore
27
{
28
    private $data = [];
29
    private $file = null;
30

31
    /**
32
     * Constructs a new data store
33
     *
34
     * @param PhingFile $file object pointing to the data store on disk
35
     */
36 0
    public function __construct(PhingFile $file)
37
    {
38 0
        $this->file = $file;
39

40 0
        if ($this->file->exists()) {
41 0
            $this->read();
42
        }
43
    }
44

45
    /**
46
     * Destructor
47
     */
48 0
    public function __destruct()
49
    {
50 0
        $this->commit();
51
    }
52

53
    /**
54
     * Retrieves a value from the data store
55
     *
56
     * @param string $key the key
57
     *
58
     * @return mixed the value
59
     */
60 0
    public function get($key)
61
    {
62 0
        if (!isset($this->data[$key])) {
63 0
            return null;
64
        }
65

66 0
        return $this->data[$key];
67
    }
68

69
    /**
70
     * Adds a value to the data store
71
     *
72
     * @param string $key the key
73
     * @param mixed $value the value
74
     * @param boolean $autocommit whether to auto-commit (write)
75
     *                            the data store to disk
76
     *
77
     * @return void
78
     */
79 0
    public function put($key, $value, $autocommit = false)
80
    {
81 0
        $this->data[$key] = $value;
82

83 0
        if ($autocommit) {
84 0
            $this->commit();
85
        }
86
    }
87

88
    /**
89
     * Remove a value from the data store
90
     *
91
     * @param string $key the key
92
     * @param boolean $autocommit whether to auto-commit (write)
93
     *                            the data store to disk
94
     */
95 0
    public function remove($key, $autocommit = false)
96
    {
97 0
        unset($this->data[$key]);
98

99 0
        if ($autocommit) {
100 0
            $this->commit();
101
        }
102
    }
103

104
    /**
105
     * Commits data store to disk
106
     *
107
     * @return void
108
     */
109 0
    public function commit()
110
    {
111 0
        $this->write();
112
    }
113

114
    /**
115
     * Internal function to read data store from file
116
     *
117
     * @throws BuildException
118
     * @return void
119
     */
120 0
    private function read()
121
    {
122 0
        if (!$this->file->canRead()) {
123 0
            throw new BuildException(
124 0
                "Can't read data store from '" .
125 0
                $this->file->getPath() . "'"
126
            );
127
        }
128

129 0
        $serializedData = $this->file->contents();
130

131 0
        $this->data = unserialize($serializedData);
132
    }
133

134
    /**
135
     * Internal function to write data store to file
136
     *
137
     * @throws BuildException
138
     * @return void
139
     */
140 0
    private function write()
141
    {
142 0
        if (!$this->file->canWrite()) {
143 0
            throw new BuildException(
144 0
                "Can't write data store to '" .
145 0
                $this->file->getPath() . "'"
146
            );
147
        }
148

149 0
        $serializedData = serialize($this->data);
150

151 0
        $writer = new FileWriter($this->file);
152 0
        $writer->write($serializedData);
153 0
        $writer->close();
154
    }
155
}

Read our documentation on viewing source code .

Loading