1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Equip\Console; |
4
|
|
|
|
5
|
|
|
use Auryn\Injector; |
6
|
|
|
use Equip\Configuration\ConfigurationSet; |
7
|
|
|
use Equip\Console\Command\CommandSet; |
8
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication; |
9
|
|
|
|
10
|
|
|
class Application |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Create a new console application |
14
|
|
|
* |
15
|
|
|
* @param Injector|null $injector |
16
|
|
|
* @param ConfigurationSet|null $configuration |
17
|
|
|
* @param CommandSet|null $commands |
18
|
|
|
* |
19
|
|
|
* @return static |
20
|
|
|
*/ |
21
|
|
|
public static function build( |
22
|
|
|
Injector $injector = null, |
23
|
|
|
ConfigurationSet $configuration = null, |
24
|
|
|
CommandSet $commands = null |
25
|
|
|
) { |
26
|
|
|
return new static( |
27
|
|
|
$injector, |
28
|
|
|
$configuration, |
29
|
|
|
$commands |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Injector |
35
|
|
|
*/ |
36
|
|
|
private $injector; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ConfigurationSet |
40
|
|
|
*/ |
41
|
|
|
private $configuration; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CommandSet |
45
|
|
|
*/ |
46
|
|
|
private $commands; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Injector|null $injector |
50
|
|
|
* @param ConfigurationSet|null $configuration |
51
|
|
|
* @param CommandSet|null $commands |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
Injector $injector = null, |
55
|
|
|
ConfigurationSet $configuration = null, |
56
|
|
|
CommandSet $commands = null |
57
|
|
|
) { |
58
|
|
|
$this->injector = $injector ?: new Injector; |
59
|
|
|
$this->configuration = $configuration ?: new ConfigurationSet; |
60
|
|
|
$this->commands = $commands ?: new CommandSet; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set configuration |
65
|
|
|
* |
66
|
|
|
* @param array $configuration |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setConfiguration(array $configuration) |
71
|
|
|
{ |
72
|
|
|
$this->configuration = $this->configuration->withValues($configuration); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set commands |
78
|
|
|
* |
79
|
|
|
* @param array $commands |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setCommands(array $commands) |
84
|
|
|
{ |
85
|
|
|
$this->commands = $this->commands->withValues($commands); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Start the console application |
91
|
|
|
* |
92
|
|
|
* @return int 0 if everything went fine, or an error code |
93
|
|
|
*/ |
94
|
|
|
public function run() |
95
|
|
|
{ |
96
|
|
|
$this->configuration->apply($this->injector); |
97
|
|
|
|
98
|
|
|
$application = $this->injector->make(ConsoleApplication::class); |
99
|
|
|
|
100
|
|
|
array_map(function ($command) use ($application) { |
101
|
|
|
$application->add($this->injector->make($command)); |
102
|
|
|
}, $this->commands->toArray()); |
103
|
|
|
|
104
|
|
|
return $application->run(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|