|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of LuisMulinari\Consoleful |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace LuisMulinari\Consoleful; |
|
9
|
|
|
|
|
10
|
|
|
use Lcobucci\DependencyInjection\Builders\DelegatingBuilder; |
|
11
|
|
|
use Lcobucci\DependencyInjection\ContainerBuilder; |
|
12
|
|
|
use Lcobucci\DependencyInjection\ContainerConfig; |
|
13
|
|
|
use Lcobucci\DependencyInjection\ContainerInjector; |
|
14
|
|
|
use Symfony\Component\Console\Application as SymfonyConsoleApplication; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This class extends Symfony Console Application and build a dependency injection container |
|
21
|
|
|
* |
|
22
|
|
|
* @author Luis Henrique Mulinari <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Application extends SymfonyConsoleApplication implements ContainerAwareInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ContainerInjector; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ContainerBuilder |
|
30
|
|
|
*/ |
|
31
|
|
|
private $builder; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ContainerConfig |
|
35
|
|
|
*/ |
|
36
|
|
|
private $containerConfig; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class constructor |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param string $version |
|
43
|
|
|
* @param ContainerConfig $containerConfig |
|
44
|
|
|
* @param ContainerBuilder $builder |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function __construct( |
|
47
|
|
|
$name = 'UNKNOWN', |
|
48
|
|
|
$version = 'UNKNOWN', |
|
49
|
|
|
ContainerConfig $containerConfig = null, |
|
50
|
|
|
ContainerBuilder $builder = null |
|
51
|
|
|
) { |
|
52
|
5 |
|
parent::__construct($name, $version); |
|
53
|
|
|
|
|
54
|
5 |
|
$this->containerConfig = $containerConfig; |
|
55
|
5 |
|
$this->builder = $builder ?: new DelegatingBuilder(); |
|
56
|
5 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param InputInterface $input |
|
60
|
|
|
* @param OutputInterface $output |
|
61
|
|
|
* @return int|void |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$this->injectContainer(); |
|
66
|
|
|
|
|
67
|
3 |
|
parent::doRun($input, $output); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
private function injectContainer() |
|
71
|
|
|
{ |
|
72
|
3 |
|
if ($this->container === null && $this->containerConfig === null) { |
|
73
|
1 |
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
if ($this->container === null) { |
|
77
|
1 |
|
$this->setContainer($this->builder->getContainer($this->containerConfig)); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
foreach ($this->all() as $command) { |
|
81
|
2 |
|
if ($command instanceof ContainerAwareInterface) { |
|
82
|
1 |
|
$command->setContainer($this->container); |
|
83
|
1 |
|
} |
|
84
|
2 |
|
} |
|
85
|
2 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|