1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phppm\Console; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
10
|
|
|
|
11
|
|
View Code Duplication |
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
|
|
|
|
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.