57 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			57 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace SparkPlug\Com\Noccy\Watcher\Commands;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use Spark\Commands\Command;
							 | 
						||
| 
								 | 
							
								use SparkPlug;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\ApiClient\Api\Method;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\ApiClient\Log\RequestData;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\Watcher\Rule;
							 | 
						||
| 
								 | 
							
								use SparkPlug\Com\Noccy\Watcher\WatcherPlug;
							 | 
						||
| 
								 | 
							
								use Symfony\Component\Console\Helper\Table;
							 | 
						||
| 
								 | 
							
								use Symfony\Component\Console\Input\InputArgument;
							 | 
						||
| 
								 | 
							
								use Symfony\Component\Console\Input\InputInterface;
							 | 
						||
| 
								 | 
							
								use Symfony\Component\Console\Input\InputOption;
							 | 
						||
| 
								 | 
							
								use Symfony\Component\Console\Output\OutputInterface;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class WatchCommand extends Command
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    protected function configure()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $this->setName("watch")
							 | 
						||
| 
								 | 
							
								            ->setDescription("Watch files and take action when they are modified")
							 | 
						||
| 
								 | 
							
								            ->addOption("interval", "N", InputOption::VALUE_REQUIRED, "Interval between polls", 5)
							 | 
						||
| 
								 | 
							
								            ;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    protected function execute(InputInterface $input, OutputInterface $output)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        /** @var WatcherPlug $plugin */
							 | 
						||
| 
								 | 
							
								        $plugin = get_plugin('com.noccy.watcher');
							 | 
						||
| 
								 | 
							
								        $config = read_config('watchers.json');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $iv = max(1, (int)$input->getOption('interval'));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (!($plugin && $config)) {
							 | 
						||
| 
								 | 
							
								            $output->writeln("<error>Missing or bad config file watchers.json?</>");
							 | 
						||
| 
								 | 
							
								            return Command::FAILURE;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        $watcher = $plugin->getFileWatcher();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        foreach ($config['watchers'] as $ruleconf) {
							 | 
						||
| 
								 | 
							
								            $rule = Rule::createFromConfig($ruleconf);
							 | 
						||
| 
								 | 
							
								            $watcher->addRule($rule);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        while (true) {
							 | 
						||
| 
								 | 
							
								            $watcher->loop();
							 | 
						||
| 
								 | 
							
								            sleep($iv);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        return Command::SUCCESS;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |