Skip to content

Commit

Permalink
Delegate InitProcess hook command generation to process builder
Browse files Browse the repository at this point in the history
  • Loading branch information
UrGuardian4ngel committed Nov 24, 2017
1 parent 78ff607 commit 415fd1a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ protected function getDefaultCommands()
);
$commands[] = new Command\Git\InitCommand(
$container->get('config'),
$container->get('grumphp.util.filesystem')
$container->get('grumphp.util.filesystem'),
$container->get('process_builder')
);
$commands[] = new Command\Git\PreCommitCommand(
$container->get('config'),
Expand Down
33 changes: 15 additions & 18 deletions src/Console/Command/Git/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use GrumPHP\Exception\FileNotFoundException;
use GrumPHP\Process\ProcessBuilder;
use GrumPHP\Util\Filesystem;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

/**
* This command is responsible for enabling all the configured hooks.
Expand Down Expand Up @@ -43,16 +43,23 @@ class InitCommand extends Command
*/
protected $input;

/**
* @var ProcessBuilder
*/
private $processBuilder;

/**
* @param GrumPHP $grumPHP
* @param Filesystem $filesystem
* @param ProcessBuilder $processBuilder
*/
public function __construct(GrumPHP $grumPHP, Filesystem $filesystem)
public function __construct(GrumPHP $grumPHP, Filesystem $filesystem, ProcessBuilder $processBuilder)
{
parent::__construct();

$this->grumPHP = $grumPHP;
$this->filesystem = $filesystem;
$this->processBuilder = $processBuilder;
}

/**
Expand Down Expand Up @@ -132,25 +139,15 @@ protected function parseHookBody($hook, SplFileInfo $templateFile)
*/
protected function generateHookCommand($command)
{
$executable = $this->paths()->getBinCommand('grumphp', true);
$arguments = [
$this->paths()->getRelativeProjectPath($executable),
$command,
];
$configFile = $this->useExoticConfigFile();

if ($configFile = $this->useExoticConfigFile()) {
$arguments[] = sprintf('--config=%s', $configFile);
}
$arguments = $this->processBuilder->createArgumentsForCommand('grumphp');
$arguments->add($command);
$arguments->addOptionalArgument('--config=%s', $configFile);

// Backwards compatible layer for Symfony Process up until 3.3.
if (class_exists('Symfony\Component\Process\ProcessBuilder')) {
$arguments = implode(' ', array_map(
['Symfony\Component\Process\ProcessUtils', 'escapeArgument'],
$arguments
));
}
$process = $this->processBuilder->buildProcess($arguments);

return (new Process($arguments))->getCommandLine();
return $process->getCommandLine();
}

/**
Expand Down

0 comments on commit 415fd1a

Please sign in to comment.