DataStore.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * $Id: DataStore.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/system/io/PhingFile.php';
  22. require_once 'phing/system/io/FileWriter.php';
  23. /**
  24. * An abstract representation of file and directory pathnames.
  25. *
  26. * @package phing.util
  27. * @author Michiel Rook <mrook@php.net>
  28. * @version $Revision: 905 $
  29. */
  30. class DataStore
  31. {
  32. private $data = array();
  33. private $file = null;
  34. /**
  35. * Constructs a new data store
  36. *
  37. * @param PhingFile $file object pointing to the data store on disk
  38. */
  39. function __construct(PhingFile $file)
  40. {
  41. $this->file = $file;
  42. if ($this->file->exists())
  43. {
  44. $this->read();
  45. }
  46. }
  47. /**
  48. * Destructor
  49. */
  50. function __destruct()
  51. {
  52. $this->commit();
  53. }
  54. /**
  55. * Retrieves a value from the data store
  56. *
  57. * @param string $key the key
  58. *
  59. * @return mixed the value
  60. */
  61. public function get($key)
  62. {
  63. if (!isset($this->data[$key]))
  64. {
  65. return null;
  66. }
  67. else
  68. {
  69. return $this->data[$key];
  70. }
  71. }
  72. /**
  73. * Adds a value to the data store
  74. *
  75. * @param string $key the key
  76. * @param mixed $value the value
  77. * @param boolean $autocommit whether to auto-commit (write)
  78. * the data store to disk
  79. *
  80. * @return none
  81. */
  82. public function put($key, $value, $autocommit = false)
  83. {
  84. $this->data[$key] = $value;
  85. if ($autocommit)
  86. {
  87. $this->commit();
  88. }
  89. }
  90. /**
  91. * Commits data store to disk
  92. *
  93. * @return none
  94. */
  95. public function commit()
  96. {
  97. $this->write();
  98. }
  99. /**
  100. * Internal function to read data store from file
  101. *
  102. * @return none
  103. */
  104. private function read()
  105. {
  106. if (!$this->file->canRead())
  107. {
  108. throw new BuildException("Can't read data store from '" .
  109. $file->getPath() . "'");
  110. }
  111. else
  112. {
  113. $serializedData = $this->file->contents();
  114. $this->data = unserialize($serializedData);
  115. }
  116. }
  117. /**
  118. * Internal function to write data store to file
  119. *
  120. * @return none
  121. */
  122. private function write()
  123. {
  124. if (!$this->file->canWrite())
  125. {
  126. throw new BuildException("Can't write data store to '" .
  127. $file->getPath() . "'");
  128. }
  129. else
  130. {
  131. $serializedData = serialize($this->data);
  132. $writer = new FileWriter($this->file);
  133. $writer->write($serializedData);
  134. $writer->close();
  135. }
  136. }
  137. };