Misc fixes and improvements
* Added request logging to com.noccy.apiclient * Added plugin com.noccy.watcher * Added pipe command and filter support * Fixes and stubs
This commit is contained in:
		
							
								
								
									
										28
									
								
								plugins/com.noccy.watcher/Monitor/MonitorInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								plugins/com.noccy.watcher/Monitor/MonitorInterface.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Watcher\Monitor;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\Watcher\Rule;
 | 
			
		||||
 | 
			
		||||
interface MonitorInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Add a rule to be watched for changes
 | 
			
		||||
     */
 | 
			
		||||
    public function add(Rule $rule);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Return a list of modified filenames
 | 
			
		||||
     */
 | 
			
		||||
    public function getModified(): array;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Return a list of watched filenames
 | 
			
		||||
     */
 | 
			
		||||
    public function getWatched(): array;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Called periodically to refresh monitors
 | 
			
		||||
     */
 | 
			
		||||
    public function loop();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										77
									
								
								plugins/com.noccy.watcher/Monitor/MtimeMonitor.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								plugins/com.noccy.watcher/Monitor/MtimeMonitor.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace SparkPlug\Com\Noccy\Watcher\Monitor;
 | 
			
		||||
 | 
			
		||||
use SparkPlug\Com\Noccy\Watcher\Rule;
 | 
			
		||||
 | 
			
		||||
class MtimeMonitor implements MonitorInterface
 | 
			
		||||
{
 | 
			
		||||
    private array $rules = [];
 | 
			
		||||
 | 
			
		||||
    private array $watched = [];
 | 
			
		||||
 | 
			
		||||
    private array $modified = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritDoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function add(Rule $rule)
 | 
			
		||||
    {
 | 
			
		||||
        $this->rules[] = $rule;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritDoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function getModified(): array
 | 
			
		||||
    {
 | 
			
		||||
        $mod = $this->modified;
 | 
			
		||||
        $this->modified = [];
 | 
			
		||||
        return $mod;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritDoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function getWatched(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [];        
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function loop()
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($this->rules as $rule) {
 | 
			
		||||
            $this->checkRule($rule);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function checkRule(Rule $rule)
 | 
			
		||||
    {
 | 
			
		||||
        clearstatcache();
 | 
			
		||||
 | 
			
		||||
        $paths = $rule->getWatchedFiles();
 | 
			
		||||
        $check = [];
 | 
			
		||||
        foreach ($paths as $path) {
 | 
			
		||||
            if (str_contains($path, '*')) {
 | 
			
		||||
                $check = array_merge($check, glob($path));
 | 
			
		||||
            } else {
 | 
			
		||||
                $check[] = $path;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($check as $path) {
 | 
			
		||||
            if (empty($this->watched[$path])) {
 | 
			
		||||
                $this->watched[$path] = filemtime($path);
 | 
			
		||||
            } else {
 | 
			
		||||
                $mtime = filemtime($path);
 | 
			
		||||
                if ($mtime > $this->watched[$path]) {
 | 
			
		||||
                    printf("* modified: %s (%s)\n", $path, $rule->getName());
 | 
			
		||||
                    $this->watched[$path] = $mtime;
 | 
			
		||||
                    if (!in_array($rule, $this->modified)) {
 | 
			
		||||
                        $this->modified[] = $rule;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user