/
AbstractCommand.php
181 lines (162 loc) · 4.29 KB
/
AbstractCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @license https://raw.githubusercontent.com/andkirby/commithook/master/LICENSE.md
*/
namespace PreCommit\Console\Command;
use PreCommit\Config;
use PreCommit\Console\Exception;
use Rikby\Console\Command\AbstractCommand as ConsoleAbstractCommand;
use Rikby\Console\Helper\GitDirHelper;
use Rikby\Console\Helper\PhpBinHelper;
use Rikby\Console\Helper\SimpleQuestionHelper;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Base command abstract class
*
* @package PreCommit\Console\Command
*/
abstract class AbstractCommand extends ConsoleAbstractCommand
{
/**
* Base commithook directory
*
* @var null|string
*/
protected $commithookDir;
/**
* Construct
*
* @param string $commithookDir
*/
public function __construct($commithookDir)
{
$this->commithookDir = $commithookDir;
parent::__construct();
}
/**
* Sets the application instance for this command.
*
* Set extra helper ProjectDir
*
* @param Application $application An Application instance
* @throws \PreCommit\Console\Exception
* @api
*/
public function setApplication(Application $application = null)
{
parent::setApplication($application);
$this->getHelperSet()->set(new GitDirHelper());
$this->getHelperSet()->set(new SimpleQuestionHelper());
$this->getHelperSet()->set(new PhpBinHelper());
}
/**
* Get config
*
* @return Config
*/
public function getConfig()
{
static $loaded;
if (null === $loaded) {
$loaded = true;
//TODO Make single load
Config::initInstance(
[
'file' => $this->commithookDir.DIRECTORY_SEPARATOR.'src'.
DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'root.xml',
]
);
Config::setProjectDir($this->askProjectDir());
if (!Config::loadCache()) {
Config::mergeExtraConfig();
}
}
return Config::getInstance();
}
/**
* Get base config (without project files)
*
* @return Config
*/
public function getConfigBase()
{
static $config;
if (null === $config) {
//TODO Make single load
$config = Config::initInstance(
[
'file' => $this->commithookDir.DIRECTORY_SEPARATOR.'src'
.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'root.xml',
]
);
if (!Config::loadCache()) {
Config::mergeExtraConfig();
}
$config = Config::getInstance();
}
return $config;
}
/**
* Initialize configuration
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
// init configuration
$this->getConfig();
return 0;
}
/**
* Init input definitions
*
* @return AbstractCommand
*/
protected function configureInput()
{
$this->addOption(
'project-dir',
'-d',
InputOption::VALUE_REQUIRED,
'Path to project (VCS) root directory.'
);
return $this;
}
/**
* Ask about GIT project root dir
*
* @return string
* @throws Exception
*/
protected function askProjectDir()
{
static $dir;
if (!$dir) {
$dir = $this->getProjectDirHelper()->getGitDirectory(
$this->input,
$this->output,
$this->input->getOption('project-dir')
);
}
return $dir;
}
/**
* Get project dir helper
*
* @return GitDirHelper
*/
protected function getProjectDirHelper()
{
if ($this->getHelperSet()) {
return $this->getHelperSet()->get(GitDirHelper::NAME);
} else {
return new GitDirHelper();
}
}
}