Skip to content

Instantly share code, notes, and snippets.

@taiar
Last active August 29, 2015 14:07
Show Gist options
  • Save taiar/32dbb79ca97671f2698e to your computer and use it in GitHub Desktop.
Save taiar/32dbb79ca97671f2698e to your computer and use it in GitHub Desktop.
Dir watcher prototype
<?php
class dir_watcher {
private $dir;
private $files;
public function __construct($dir = '.') {
$this->dir = realpath($dir) . DIRECTORY_SEPARATOR;
$this->get_file_timestamps();
echo "Monitoring dir: " . $this->dir . PHP_EOL;
$this->run();
}
private function get_file_timestamps() {
$this->files = array();
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->dir),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $path) {
if (!$path->isDir()) {
$this->files[$path->__toString()] = filemtime($path->__toString());
}
}
}
private function check() {
foreach ($this->files as $file => $mtime) {
$ntime = filemtime($file);
if($ntime != $mtime) {
echo "[".date("H:i:s d-m-Y")."] File modification found." . PHP_EOL;
return true;
}
}
return false;
}
private function action() {
system("php vendor/taghouse/installer/Installer.php");
$this->get_file_timestamps();
}
private function run(){
while(true) {
if($this->check())
$this->action();
sleep(3);
}
}
}
new dir_watcher();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment