PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/php/io/SpoolDirectory.class.php

http://github.com/xp-framework/xp-framework
PHP | 157 lines | 80 code | 19 blank | 58 comment | 6 complexity | 89cb873a5c15618576abaa51618a2c4d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses(
  7. 'io.File',
  8. 'io.Folder'
  9. );
  10. /**
  11. * This class handles all neccessary file and directory operations
  12. * for doing reliable spool operations.
  13. *
  14. * @purpose Reliable spool directory class
  15. */
  16. class SpoolDirectory extends Object {
  17. public
  18. $root;
  19. public
  20. $_hNew= NULL,
  21. $_hDone= NULL,
  22. $_hTodo= NULL,
  23. $_hError= NULL;
  24. /**
  25. * Creates a spooldirectory object
  26. *
  27. * @param string root Root of the spool hierarchy
  28. */
  29. public function __construct($root) {
  30. $this->root= $root;
  31. }
  32. /**
  33. * Opens all neccessary directorys and creates them if nonexistant
  34. *
  35. * @return bool success
  36. * @throws io.IOException if there are permission problems
  37. */
  38. public function open() {
  39. $this->_hNew= new Folder ($this->root.DIRECTORY_SEPARATOR.'new');
  40. $this->_hTodo= new Folder ($this->root.DIRECTORY_SEPARATOR.'todo');
  41. $this->_hDone= new Folder ($this->root.DIRECTORY_SEPARATOR.'done');
  42. $this->_hError= new Folder ($this->root.DIRECTORY_SEPARATOR.'error');
  43. if (!$this->_hNew->exists()) $this->_hNew->create ();
  44. if (!$this->_hTodo->exists()) $this->_hTodo->create ();
  45. if (!$this->_hDone->exists()) $this->_hDone->create ();
  46. if (!$this->_hError->exists()) $this->_hError->create ();
  47. return TRUE;
  48. }
  49. /**
  50. * Creates a new spool entry. The abstract is used to name the
  51. * file in a way that associates it with its content (e.g. a
  52. * unique id). If the abstract is omitted, a id
  53. * generator will be used.
  54. *
  55. * @param string abstract default NULL
  56. * @return io.File opened spool file
  57. * @throws io.IOException if file could not be created
  58. */
  59. public function createSpoolEntry($abstract= NULL) {
  60. if (NULL === $abstract)
  61. $abstract= date ('Y-m-d-H-i-s').md5 (microtime());
  62. else
  63. $abstract= date ('Y-m-d-H-i-s').'_'.$abstract;
  64. try {
  65. $f= new File ($this->_hNew->getURI().DIRECTORY_SEPARATOR.$abstract.'.spool');
  66. $f->open (FILE_MODE_WRITE);
  67. } catch (IOException $e) {
  68. throw ($e);
  69. }
  70. return $f;
  71. }
  72. /**
  73. * Enqueues the spoolentry into the todo-queue.
  74. *
  75. * @param io.File Spoolfile
  76. * @return bool success
  77. * @throws io.IOException if file could not be closed and moved.
  78. */
  79. public function enqueueSpoolEntry($f) {
  80. try {
  81. $f->close();
  82. $f->move ($this->_hTodo->getURI().DIRECTORY_SEPARATOR.$f->getFileName());
  83. } catch (IOException $e) {
  84. throw ($e);
  85. }
  86. return TRUE;
  87. }
  88. /**
  89. * Retrieves the next spool entry.
  90. *
  91. * @return io.File spoolfile next spoolfile. Its opened in read/write mode.
  92. * @throws io.IOException if an error occurs
  93. */
  94. public function getNextSpoolEntry() {
  95. try {
  96. if (FALSE !== ($entry= $this->_hTodo->getEntry())) {
  97. $f= new File ($this->_hTodo->getURI().DIRECTORY_SEPARATOR.$entry);
  98. $f->open (FILE_MODE_READWRITE);
  99. }
  100. } catch (IOException $e) {
  101. throw ($e);
  102. }
  103. return $f;
  104. }
  105. /**
  106. * Mark the given spool entry as done.
  107. *
  108. * @param io.File spoolfile
  109. * @return bool success
  110. * @throws io.IOException if file could not be closed and moved.
  111. */
  112. public function finishSpoolEntry($f) {
  113. try {
  114. $f->close();
  115. $f->move ($this->_hDone->getURI().DIRECTORY_SEPARATOR.$f->getFileName());
  116. } catch (IOException $e) {
  117. throw ($e);
  118. }
  119. return TRUE;
  120. }
  121. /**
  122. * Mark the given spool entry as failed.
  123. *
  124. * @param io.File spoolfile
  125. * @return bool success
  126. * @throws io.IOException if file could not be closed and moved.
  127. */
  128. public function failSpoolEntry($f) {
  129. try {
  130. $f->close();
  131. $f->move ($this->_hError->getURI().DIRECTORY_SEPARATOR.$f->getFileName());
  132. } catch (IOException $e) {
  133. throw ($e);
  134. }
  135. return TRUE;
  136. }
  137. }
  138. ?>