1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phppm\Console; |
4
|
|
|
|
5
|
|
|
use Phppm\ProcessManager; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
11
|
|
|
|
12
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.