Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\File\Watcher Class Reference
Inheritance diagram for Hoa\File\Watcher:

Public Member Functions

 __construct ($latency=null)
 
 on ($listenerId, $callable)
 
 run ()
 
 setLatency ($latency)
 
 getLatency ()
 
- Public Member Functions inherited from Hoa\File\Finder
 __construct ()
 
 in ($path)
 
 maxDepth ($depth)
 
 files ()
 
 directories ()
 
 links ()
 
 followSymlinks ($flag=true)
 
 name ($regex)
 
 notIn ($regex)
 
 size ($size)
 
 dots ($flag=true)
 
 owner ($owner)
 
 changed ($date)
 
 modified ($date)
 
 filter ($callback)
 
 sortByName ($locale= 'root')
 
 sortBySize ()
 
 sort ($callable)
 
 childFirst ()
 
 getIterator ()
 
 setSplFileInfo ($splFileInfo)
 
 getSplFileInfo ()
 
 getMaxDepth ()
 
 getTypes ()
 
 getName ()
 
 getFlags ()
 
 getFirst ()
 

Protected Attributes

 $_on = null
 
 $_latency = 1
 
- Protected Attributes inherited from Hoa\File\Finder
 $_splFileInfo = 'Hoa\File\SplFileInfo'
 
 $_paths = []
 
 $_maxDepth = -1
 
 $_filters = []
 
 $_flags = -1
 
 $_types = []
 
 $_first = -1
 
 $_sorts = []
 

Additional Inherited Members

- Protected Member Functions inherited from Hoa\File\Finder
 formatDate ($date, &$operator)
 
 getPaths ()
 
 getFilters ()
 
 getSorts ()
 

Detailed Description

Class .

A naive file system watcher that fires three events: new, move and modify.

Definition at line 49 of file Watcher.php.

Constructor & Destructor Documentation

Hoa\File\Watcher::__construct (   $latency = null)

Constructor.

Parameters
int$latencyLatency (in seconds).
Returns
void

Definition at line 73 of file Watcher.php.

74  {
75  parent::__construct();
76 
77  $this->_on = new Core\Event\Listener(
78  $this,
79  [
80  'new',
81  'modify',
82  'move'
83  ]
84  );
85 
86  if (null !== $latency) {
87  $this->setLatency($latency);
88  }
89 
90  return;
91  }
setLatency($latency)
Definition: Watcher.php:179

Here is the call graph for this function:

Member Function Documentation

Hoa\File\Watcher::getLatency ( )

Get latency.

Returns
int

Definition at line 192 of file Watcher.php.

193  {
194  return $this->_latency;
195  }

Here is the caller graph for this function:

Hoa\File\Watcher::on (   $listenerId,
  $callable 
)

Attach a callable to this listenable object.

Parameters
string$listenerIdListener ID.
mixed$callableCallable.
Returns

Implements Hoa\Core\Event\Listenable.

Definition at line 101 of file Watcher.php.

102  {
103  $this->_on->attach($listenerId, $callable);
104 
105  return $this;
106  }
Hoa\File\Watcher::run ( )

Run the watcher.

Listenable events: • new, when a file is new, i.e. found by the finder; • modify, when a file has been modified; • move, when a file has moved, i.e. no longer found by the finder.

Returns
void

Definition at line 118 of file Watcher.php.

119  {
120  $iterator = $this->getIterator();
121  $previous = iterator_to_array($iterator);
122  $current = $previous;
123 
124  while (true) {
125  foreach ($current as $name => $c) {
126  if (!isset($previous[$name])) {
127  $this->_on->fire(
128  'new',
129  new Core\Event\Bucket([
130  'file' => $c
131  ])
132  );
133 
134  continue;
135  }
136 
137  if (null === $c->getHash()) {
138  unset($current[$name]);
139 
140  continue;
141  }
142 
143  if ($previous[$name]->getHash() != $c->getHash()) {
144  $this->_on->fire(
145  'modify',
146  new Core\Event\Bucket([
147  'file' => $c
148  ])
149  );
150  }
151 
152  unset($previous[$name]);
153  }
154 
155  foreach ($previous as $p) {
156  $this->_on->fire(
157  'move',
158  new Core\Event\Bucket([
159  'file' => $p
160  ])
161  );
162  }
163 
164  usleep($this->getLatency() * 1000000);
165 
166  $previous = $current;
167  $current = iterator_to_array($iterator);
168  }
169 
170  return;
171  }

Here is the call graph for this function:

Hoa\File\Watcher::setLatency (   $latency)

Set latency.

Parameters
int$latencyLatency (in seconds).
Returns
int

Definition at line 179 of file Watcher.php.

180  {
181  $old = $this->_latency;
182  $this->_latency = $latency;
183 
184  return $old;
185  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\File\Watcher::$_latency = 1
protected

Definition at line 63 of file Watcher.php.

Hoa\File\Watcher::$_on = null
protected

Definition at line 56 of file Watcher.php.


The documentation for this class was generated from the following file: