| @@ 12-70 (lines=59) @@ | ||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
|
| 10 | use Symfony\Component\Console\Style\SymfonyStyle; |
|
| 11 | ||
| 12 | class Run extends Command |
|
| 13 | { |
|
| 14 | const DEFAULT_TIME = 100; |
|
| 15 | ||
| 16 | protected function configure() |
|
| 17 | { |
|
| 18 | $this->setName('run') |
|
| 19 | ->setDescription('Run given script as demon process.') |
|
| 20 | ->setHelp(''); |
|
| 21 | ||
| 22 | $this->addArgument( |
|
| 23 | 'script', |
|
| 24 | InputArgument::REQUIRED, |
|
| 25 | 'Script that will be demonized' |
|
| 26 | ); |
|
| 27 | ||
| 28 | $this->addOption('interval', 'i', InputArgument::OPTIONAL, 'Worker interval time.'); |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @param InputInterface $input |
|
| 33 | * @param OutputInterface $output |
|
| 34 | * @throws \InvalidArgumentException |
|
| 35 | */ |
|
| 36 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 37 | { |
|
| 38 | $style = new SymfonyStyle($input, $output); |
|
| 39 | $script = $input->getArgument('script'); |
|
| 40 | ||
| 41 | $style->title('Script demonizer.'); |
|
| 42 | ||
| 43 | $time = $input->getOption('interval') ?: self::DEFAULT_TIME; |
|
| 44 | ||
| 45 | $style->comment('Execute with interval: ' . $time); |
|
| 46 | ||
| 47 | if (file_exists($script)) { |
|
| 48 | require_once $script; |
|
| 49 | $namespace = $this->getNamespace($script); |
|
| 50 | ||
| 51 | $processManager = new ProcessManager(); |
|
| 52 | $processManager->addWorker('\\' . $namespace, $output, $time); |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @param string $file |
|
| 58 | * @return string |
|
| 59 | */ |
|
| 60 | protected function getNamespace(string $file) : string |
|
| 61 | { |
|
| 62 | $source = file_get_contents($file); |
|
| 63 | ||
| 64 | if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $source, $matches)) { |
|
| 65 | return $matches[1].'\\'.$matches[2]; |
|
| 66 | } |
|
| 67 | ||
| 68 | return ''; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| @@ 11-68 (lines=58) @@ | ||
| 8 | use Symfony\Component\Console\Input\InputArgument; |
|
| 9 | use Symfony\Component\Console\Style\SymfonyStyle; |
|
| 10 | ||
| 11 | class Worker extends Command |
|
| 12 | { |
|
| 13 | protected function configure() |
|
| 14 | { |
|
| 15 | $this->setName('worker') |
|
| 16 | ->setDescription('Execute worker with given process script.') |
|
| 17 | ->setHelp(''); |
|
| 18 | ||
| 19 | $this->addArgument( |
|
| 20 | 'script', |
|
| 21 | InputArgument::REQUIRED, |
|
| 22 | 'Script that will be demonized' |
|
| 23 | ); |
|
| 24 | ||
| 25 | $this->addOption('interval', 'i', InputArgument::OPTIONAL, 'Worker interval time.'); |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param InputInterface $input |
|
| 30 | * @param OutputInterface $output |
|
| 31 | * @throws \InvalidArgumentException |
|
| 32 | */ |
|
| 33 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 34 | { |
|
| 35 | $style = new SymfonyStyle($input, $output); |
|
| 36 | $script = $input->getArgument('script'); |
|
| 37 | ||
| 38 | $style->title('Script worker.'); |
|
| 39 | ||
| 40 | $time = $input->getOption('interval') ?: Run::DEFAULT_TIME; |
|
| 41 | ||
| 42 | $style->comment('Execute with interval: ' . $time); |
|
| 43 | ||
| 44 | if (file_exists($script)) { |
|
| 45 | require_once $script; |
|
| 46 | $namespace = $this->getNamespace($script); |
|
| 47 | ||
| 48 | $worker = new \Phppm\ProcessWorker('\\' . $namespace, $output, $time); |
|
| 49 | $worker->runProcess(); |
|
| 50 | ||
| 51 | } |
|
| 52 | } |
|
| 53 | ||
| 54 | /** |
|
| 55 | * @param string $file |
|
| 56 | * @return string |
|
| 57 | */ |
|
| 58 | protected function getNamespace(string $file) : string |
|
| 59 | { |
|
| 60 | $source = file_get_contents($file); |
|
| 61 | ||
| 62 | if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $source, $matches)) { |
|
| 63 | return $matches[1].'\\'.$matches[2]; |
|
| 64 | } |
|
| 65 | ||
| 66 | return ''; |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||