|
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 |
|
$this->symfonyApplication->setAutoExit(false); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function run(InputInterface $input, OutputInterface $output) : int |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->container['phpchunkit.application.input'] = $input; |
|
51
|
1 |
|
$this->container['phpchunkit.application.output'] = $output; |
|
52
|
|
|
|
|
53
|
1 |
|
foreach (self::$commands as $serviceName) { |
|
54
|
1 |
|
$this->registerCommand($serviceName); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return $this->runSymfonyApplication($input, $output); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function registerCommand(string $serviceName) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$service = $this->container[$serviceName]; |
|
63
|
|
|
|
|
64
|
1 |
|
$symfonyCommand = $this->register($service->getName()); |
|
65
|
|
|
|
|
66
|
1 |
|
$service->configure($symfonyCommand); |
|
67
|
|
|
|
|
68
|
1 |
|
$symfonyCommand->setCode(function($input, $output) use ($service) { |
|
69
|
|
|
if (!$service instanceof Command\Setup) { |
|
70
|
|
|
$configuration = $this->container['phpchunkit.configuration']; |
|
71
|
|
|
|
|
72
|
|
|
if (!$configuration->isSetup()) { |
|
73
|
|
|
return $this->symfonyApplication |
|
74
|
|
|
->find('setup')->run($input, $output); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return call_user_func_array([$service, 'execute'], [$input, $output]); |
|
79
|
1 |
|
}); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function runSymfonyApplication( |
|
83
|
|
|
InputInterface $input, |
|
84
|
|
|
OutputInterface $output) : int |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->symfonyApplication->run($input, $output); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
private function register(string $name) : SymfonyCommand |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->symfonyApplication->register($name); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|