| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Darkilliant\ProcessBundle\Step; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Darkilliant\ProcessBundle\State\ProcessState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class FilesystemEventStreamStep extends AbstractConfigurableStep implements IterableStepInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var Process */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $events = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 1 |  |     public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |         $resolver->setRequired(['folder', 'event_name', 'recursive']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |         $resolver->setDefault('recursive', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         $resolver->setAllowedValues('event_name', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |             'access', 'modify', 'attrib', 'close_write', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'close_nowrite', 'close', 'open', 'moved_to', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'moved_from', 'move', 'create', 'delete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'delete_self', 'unmount', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         return parent::configureOptionResolver($resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |     public function execute(ProcessState $state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |         $command = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |             'inotifywait %s --csv -q --monitor -e %s %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |             $state->getOptions()['recursive'] ? '-r' : '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |             $state->getOptions()['event_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |             $state->getOptions()['folder'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |         $this->process = $this->getProcess($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |         $this->process->setTimeout(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     public function next(ProcessState $state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         $current = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         while (!$current) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             $currentOutput = $this->process->getIncrementalOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             $lines = explode(PHP_EOL, $currentOutput); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $lines = array_filter($lines, function ($item) { return !empty($item); }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             $state->debug('{count} lines', ['count' => count($lines)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             foreach ($lines as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                 list($folder, $events, $file) = str_getcsv($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |                 $absoluteFile = $folder.$file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |                 $events = explode(',', $events); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |                 $this->events[] = $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |                     'events' => $events, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                     'absolute_file' => $absoluteFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                     'folder' => $folder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |                     'file' => $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                 $state->debug('event {event} on {file}', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |                     'event' => $data['events'][0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                     'file' => $data['absolute_file'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             $current = array_shift($this->events); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             if (!$current) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                 $state->info('Waiting FileSystem Event...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |                 sleep(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         $state->setData($current); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     public function valid(ProcessState $state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         if (!$this->process->isStarted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |             $this->process->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         return $this->process->isRunning(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     public function getProgress(ProcessState $state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 1 |  |     public function count(ProcessState $state) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 1 |  |         return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     protected function getProcess(string $commandLine): Process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return new Process($commandLine); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |