1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PHPChunkit; |
6
|
|
|
|
7
|
|
|
use PHPChunkit\Command; |
8
|
|
|
use Symfony\Component\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
10
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @testClass PHPChunkit\Test\PHPChunkitApplicationTest |
19
|
|
|
*/ |
20
|
|
|
class PHPChunkitApplication |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Container |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Application |
29
|
|
|
*/ |
30
|
|
|
private $symfonyApplication; |
31
|
|
|
|
32
|
|
|
private static $commands = [ |
33
|
|
|
'phpchunkit.command.setup', |
34
|
|
|
'phpchunkit.command.build_sandbox', |
35
|
|
|
'phpchunkit.command.create_databases', |
36
|
|
|
'phpchunkit.command.test_watcher', |
37
|
|
|
'phpchunkit.command.run', |
38
|
|
|
'phpchunkit.command.generate_test', |
39
|
|
|
]; |
40
|
|
|
|
41
|
1 |
|
public function __construct(Container $container) |
42
|
|
|
{ |
43
|
1 |
|
$this->container = $container; |
44
|
1 |
|
$this->symfonyApplication = $this->container['phpchunkit.symfony_application']; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function run(InputInterface $input, OutputInterface $output) : int |
48
|
|
|
{ |
49
|
1 |
|
$this->container['phpchunkit.application.input'] = $input; |
50
|
1 |
|
$this->container['phpchunkit.application.output'] = $output; |
51
|
|
|
|
52
|
1 |
|
foreach (self::$commands as $serviceName) { |
53
|
1 |
|
$this->registerCommand($serviceName); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $this->runSymfonyApplication($input, $output); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function registerCommand(string $serviceName) |
60
|
|
|
{ |
61
|
1 |
|
$service = $this->container[$serviceName]; |
62
|
|
|
|
63
|
1 |
|
$symfonyCommand = $this->register($service->getName()); |
64
|
|
|
|
65
|
1 |
|
$service->configure($symfonyCommand); |
66
|
|
|
|
67
|
1 |
|
$symfonyCommand->setCode(function($input, $output) use ($service) { |
68
|
|
|
if (!$service instanceof Command\Setup) { |
69
|
|
|
$configuration = $this->container['phpchunkit.configuration']; |
70
|
|
|
|
71
|
|
|
if (!$configuration->isSetup()) { |
72
|
|
|
return $this->symfonyApplication |
73
|
|
|
->find('setup')->run($input, $output); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
call_user_func_array([$service, 'execute'], [$input, $output]); |
78
|
1 |
|
}); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
protected function runSymfonyApplication( |
82
|
|
|
InputInterface $input, |
83
|
|
|
OutputInterface $output) : int |
84
|
|
|
{ |
85
|
|
|
return $this->symfonyApplication->run($input, $output); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
private function register(string $name) : SymfonyCommand |
89
|
|
|
{ |
90
|
1 |
|
return $this->symfonyApplication->register($name); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|