|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Console-Kit library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/console-kit |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\ConsoleKit; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand; |
|
15
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Application extends BaseApplication |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Running command. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Command |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_runningCommand; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Dependency injection container. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Container |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $dic; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates application. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Container $container Container. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Container $container) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->dic = $container; |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct($this->dic['app_name'], $this->dic['app_version']); |
|
47
|
|
|
|
|
48
|
|
|
$helper_set = $this->getHelperSet(); |
|
49
|
|
|
$helper_set->set($this->dic['container_helper']); |
|
50
|
|
|
|
|
51
|
|
|
$that = $this; |
|
52
|
|
|
$this->dic['helper_set'] = function () use ($that) { |
|
53
|
|
|
return $that->getHelperSet(); |
|
54
|
|
|
}; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getDefaultCommands() |
|
61
|
|
|
{ |
|
62
|
|
|
$default_commands = parent::getDefaultCommands(); |
|
63
|
|
|
$default_commands[] = new CompletionCommand(); |
|
64
|
|
|
|
|
65
|
|
|
return $default_commands; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritDoc |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
|
|
$this->_runningCommand = $command; |
|
75
|
|
|
|
|
76
|
|
|
return parent::doRunCommand($command, $input, $output); |
|
77
|
|
|
} |
|
78
|
|
|
finally { |
|
79
|
|
|
$this->_runningCommand = null; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Determines if this is topmost command. |
|
85
|
|
|
* |
|
86
|
|
|
* @param Command $command Command. |
|
87
|
|
|
* |
|
88
|
|
|
* @return boolean |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isTopmostCommand(Command $command) |
|
91
|
|
|
{ |
|
92
|
|
|
return is_object($this->_runningCommand) && $command->getName() === $this->_runningCommand->getName(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
99
|
|
|
{ |
|
100
|
|
|
if ( !isset($input) ) { |
|
101
|
|
|
$input = $this->dic['input']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ( !isset($output) ) { |
|
105
|
|
|
$output = $this->dic['output']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return parent::run($input, $output); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Detects, when we're inside PHAR file. |
|
113
|
|
|
* |
|
114
|
|
|
* @return boolean |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function isPharFile() |
|
117
|
|
|
{ |
|
118
|
|
|
return strpos(__DIR__, 'phar://') === 0; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|