Skip to content

Commit 08fbde6

Browse files
committed
Merge pull request #55 from mdgbg/bin-auto-discovery
Added auto bin dir discovery in cases when we have set config / bin-d…
2 parents 060b9bc + ed3d0c2 commit 08fbde6

File tree

10 files changed

+81
-11
lines changed

10 files changed

+81
-11
lines changed

src/PhpGitHooks/Application/PhpUnit/PhpUnitHandler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class PhpUnitHandler extends ToolHandler
1616
{
1717
/** @var PhpUnitProcessBuilder */
18-
private $phpUnitProcessBuilder;
18+
protected $phpUnitProcessBuilder;
1919

2020
/**
2121
* @param OutputHandlerInterface $outputHandler
@@ -37,7 +37,7 @@ public function run(array $messages)
3737
{
3838
$this->setTitle();
3939

40-
$processBuilder = $this->phpUnitProcessBuilder->getProcessBuilder();
40+
$processBuilder = $this->processBuilder();
4141
$processBuilder->setTimeout(3600);
4242
$phpunit = $processBuilder->getProcess();
4343
$this->phpUnitProcessBuilder
@@ -49,6 +49,14 @@ public function run(array $messages)
4949
}
5050
}
5151

52+
/**
53+
* @return \Symfony\Component\Process\ProcessBuilder
54+
*/
55+
protected function processBuilder()
56+
{
57+
return $this->phpUnitProcessBuilder->getProcessBuilder($this->getBinPath('phpunit'));
58+
}
59+
5260
private function setTitle()
5361
{
5462
$this->outputHandler->setTitle('Running unit tests');

src/PhpGitHooks/Application/PhpUnit/PhpUnitRandomizerHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44

55
class PhpUnitRandomizerHandler extends PhpUnitHandler
66
{
7+
/**
8+
* @return \Symfony\Component\Process\ProcessBuilder
9+
*/
10+
protected function processBuilder()
11+
{
12+
return $this->phpUnitProcessBuilder->getProcessBuilder($this->getBinPath('phpunit-randomizer'));
13+
}
714
}

src/PhpGitHooks/Infrastructure/CodeSniffer/CodeSnifferHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ public function run(array $messages)
5454
}
5555

5656
if (false === $this->ignoreFiles->isIgnored($file)) {
57-
$processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.$this->standard, $file));
57+
$processBuilder = new ProcessBuilder(
58+
array(
59+
'php',
60+
$this->getBinPath('phpcs'),
61+
'--standard='.$this->standard,
62+
$file,
63+
)
64+
);
5865
/** @var Process $phpCs */
5966
$phpCs = $processBuilder->getProcess();
6067
$phpCs->run();

src/PhpGitHooks/Infrastructure/Common/ProcessBuilderInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
interface ProcessBuilderInterface
1313
{
1414
/**
15+
* @param string $bin
16+
*
1517
* @return ProcessBuilder
1618
*/
17-
public function getProcessBuilder();
19+
public function getProcessBuilder($bin);
1820

1921
/**
2022
* @param Process $process

src/PhpGitHooks/Infrastructure/Common/ToolHandler.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,40 @@
1010
*/
1111
abstract class ToolHandler
1212
{
13+
const COMPOSER_VENDOR_DIR = '/../../../../../../';
14+
const COMPOSER_INSTALLED_FILE = 'composer/installed.json';
15+
1316
/** @var OutputHandlerInterface */
1417
protected $outputHandler;
1518
/** @var OutputInterface */
1619
protected $output;
1720

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+
1833
/**
1934
* @param OutputHandlerInterface $outputHandler
2035
*/
2136
public function __construct(OutputHandlerInterface $outputHandler)
2237
{
2338
$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+
}
2447
}
2548

2649
/**
@@ -40,4 +63,23 @@ protected function writeOutputError(\Exception $exceptionClass, $errorText)
4063
ErrorOutput::write($errorText);
4164
throw new $exceptionClass();
4265
}
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+
}
4385
}

src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function run(array $messages)
3434
$processBuilder = new ProcessBuilder(
3535
array(
3636
'php',
37-
'bin/jsonlint',
37+
$this->getBinPath('jsonlint'),
3838
$file,
3939
)
4040
);

src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function run(array $messages)
5858
$processBuilder = new ProcessBuilder(
5959
array(
6060
'php',
61-
'bin/php-cs-fixer',
61+
$this->getBinPath('php-cs-fixer'),
6262
'--dry-run',
6363
'fix',
6464
$file,

src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function run(array $messages)
5656
$processBuilder = new ProcessBuilder(
5757
array(
5858
'php',
59-
'bin/phpmd',
59+
$this->getBinPath('phpmd'),
6060
$file,
6161
'text',
6262
'PmdRules.xml',

src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitProcessBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
class PhpUnitProcessBuilder implements ProcessBuilderInterface
1414
{
1515
/**
16+
* @param string $bin
17+
*
1618
* @return ProcessBuilder
1719
*/
18-
public function getProcessBuilder()
20+
public function getProcessBuilder($bin)
1921
{
20-
return new ProcessBuilder(array('php', 'bin/phpunit'));
22+
return new ProcessBuilder(array('php', $bin));
2123
}
2224

2325
/**

src/PhpGitHooks/Infrastructure/PhpUnit/PhpUnitRandomizerProcessBuilder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
final class PhpUnitRandomizerProcessBuilder extends PhpUnitProcessBuilder
88
{
99
/**
10+
* @param string $bin
11+
*
1012
* @return ProcessBuilder
1113
*/
12-
public function getProcessBuilder()
14+
public function getProcessBuilder($bin)
1315
{
14-
return new ProcessBuilder(array('php', 'bin/phpunit-randomizer', '--order', 'rand'));
16+
return new ProcessBuilder(array('php', $bin, '--order', 'rand'));
1517
}
1618
}

0 commit comments

Comments
 (0)