1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\Common; |
4
|
|
|
|
5
|
|
|
use PhpGitHooks\Command\OutputHandlerInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ToolHandler. |
10
|
|
|
*/ |
11
|
|
|
abstract class ToolHandler |
12
|
|
|
{ |
13
|
|
|
const COMPOSER_VENDOR_DIR = '/../../../../../../'; |
14
|
|
|
const COMPOSER_INSTALLED_FILE = 'composer/installed.json'; |
15
|
|
|
|
16
|
|
|
/** @var OutputHandlerInterface */ |
17
|
|
|
protected $outputHandler; |
18
|
|
|
/** @var OutputInterface */ |
19
|
|
|
protected $output; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $tools = array( |
23
|
|
|
'phpcs' => 'squizlabs/php_codesniffer', |
24
|
|
|
'php-cs-fixer' => 'fabpot/php-cs-fixer', |
25
|
|
|
'phpmd' => 'phpmd/phpmd', |
26
|
|
|
'phpunit' => 'phpunit/phpunit', |
27
|
|
|
'phpunit-randomizer' => 'fiunchinho/phpunit-randomizer', |
28
|
|
|
'jsonlint' => 'seld/jsonlint', |
29
|
|
|
); |
30
|
|
|
/** @var array */ |
31
|
|
|
private $installedPackages = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param OutputHandlerInterface $outputHandler |
35
|
|
|
*/ |
36
|
|
|
public function __construct(OutputHandlerInterface $outputHandler) |
37
|
|
|
{ |
38
|
|
|
$this->outputHandler = $outputHandler; |
39
|
|
|
|
40
|
|
|
$installedJson = dirname(__FILE__).self::COMPOSER_VENDOR_DIR.self::COMPOSER_INSTALLED_FILE; |
41
|
|
|
if (file_exists($installedJson)) { // else not installed over composer |
42
|
|
|
$packages = json_decode(file_get_contents($installedJson), true); |
43
|
|
|
foreach ($packages as $package) { |
44
|
|
|
$this->installedPackages[$package['name']] = $package; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param OutputInterface $outputInterface |
51
|
|
|
*/ |
52
|
|
|
public function setOutput(OutputInterface $outputInterface) |
53
|
|
|
{ |
54
|
|
|
$this->output = $outputInterface; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Exception $exceptionClass |
59
|
|
|
* @param string $errorText |
60
|
|
|
*/ |
61
|
|
|
protected function writeOutputError(\Exception $exceptionClass, $errorText) |
62
|
|
|
{ |
63
|
|
|
ErrorOutput::write($errorText); |
64
|
|
|
throw new $exceptionClass(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $tool |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function getBinPath($tool) |
73
|
|
|
{ |
74
|
|
|
if (isset($this->installedPackages[$this->tools[$tool]])) { |
75
|
|
|
$package = $this->installedPackages[$this->tools[$tool]]; |
76
|
|
|
foreach ($package['bin'] as $bin) { |
77
|
|
|
if (preg_match("#${tool}$#", $bin)) { |
78
|
|
|
return dirname(__FILE__).self::COMPOSER_VENDOR_DIR.$package['name'].DIRECTORY_SEPARATOR.$bin; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return 'bin'.DIRECTORY_SEPARATOR.$tool; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|