Watcher.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\File;
  36. use Hoa\Event;
  37. /**
  38. * Class \Hoa\File\Watcher.
  39. *
  40. * A naive file system watcher that fires three events: new, move and modify.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Watcher extends Finder implements Event\Listenable
  46. {
  47. use Event\Listens;
  48. /**
  49. * Latency.
  50. *
  51. * @var int
  52. */
  53. protected $_latency = 1;
  54. /**
  55. * Constructor.
  56. *
  57. * @param int $latency Latency (in seconds).
  58. */
  59. public function __construct($latency = null)
  60. {
  61. parent::__construct();
  62. $this->setListener(
  63. new Event\Listener(
  64. $this,
  65. [
  66. 'new',
  67. 'modify',
  68. 'move'
  69. ]
  70. )
  71. );
  72. if (null !== $latency) {
  73. $this->setLatency($latency);
  74. }
  75. return;
  76. }
  77. /**
  78. * Run the watcher.
  79. *
  80. * Listenable events:
  81. * • new, when a file is new, i.e. found by the finder;
  82. * • modify, when a file has been modified;
  83. * • move, when a file has moved, i.e. no longer found by the finder.
  84. *
  85. * @return void
  86. */
  87. public function run()
  88. {
  89. $iterator = $this->getIterator();
  90. $previous = iterator_to_array($iterator);
  91. $current = $previous;
  92. while (true) {
  93. foreach ($current as $name => $c) {
  94. if (!isset($previous[$name])) {
  95. $this->getListener()->fire(
  96. 'new',
  97. new Event\Bucket([
  98. 'file' => $c
  99. ])
  100. );
  101. continue;
  102. }
  103. if (null === $c->getHash()) {
  104. unset($current[$name]);
  105. continue;
  106. }
  107. if ($previous[$name]->getHash() != $c->getHash()) {
  108. $this->getListener()->fire(
  109. 'modify',
  110. new Event\Bucket([
  111. 'file' => $c
  112. ])
  113. );
  114. }
  115. unset($previous[$name]);
  116. }
  117. foreach ($previous as $p) {
  118. $this->getListener()->fire(
  119. 'move',
  120. new Event\Bucket([
  121. 'file' => $p
  122. ])
  123. );
  124. }
  125. usleep($this->getLatency() * 1000000);
  126. $previous = $current;
  127. $current = iterator_to_array($iterator);
  128. }
  129. return;
  130. }
  131. /**
  132. * Set latency.
  133. *
  134. * @param int $latency Latency (in seconds).
  135. * @return int
  136. */
  137. public function setLatency($latency)
  138. {
  139. $old = $this->_latency;
  140. $this->_latency = $latency;
  141. return $old;
  142. }
  143. /**
  144. * Get latency.
  145. *
  146. * @return int
  147. */
  148. public function getLatency()
  149. {
  150. return $this->_latency;
  151. }
  152. }