Passed
Push — master ( 795a3e...0f843e )
by Alexander
14:26
created

Application::getDefaultCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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