Skip to content

Commit c1979e9

Browse files
committed
minor #122 Support Symfony 4.0 (UrGuardian4ngel)
This PR was squashed before being merged into the 1.0-dev branch (closes #122). Discussion ---------- Support Symfony 4.0 Allow usage of the latest version (currently in beta) of the [symfony/process](https://github.com/symfony/process) component. #SymfonyConHackday2017 Commits ------- 50cae49 Support Symfony 4.0
2 parents 9ff6537 + 50cae49 commit c1979e9

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ language: php
33
sudo: false
44

55
php:
6-
- 5.3
76
- 5.4
87
- 5.5
98
- 5.6
109
- 7.0
1110
- hhvm
1211

12+
matrix:
13+
include:
14+
- php: 5.3
15+
dist: precise
16+
1317
install:
1418
- composer install --prefer-source
1519

16-
script: phpunit
20+
script: vendor/bin/phpunit

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
}
2727
},
2828
"require": {
29-
"symfony/process": "^2.3|^3.0"
29+
"symfony/process": "^2.3|^3.0|^4.0"
3030
},
3131
"require-dev": {
32+
"phpunit/phpunit": "^4.8.35|^5.7",
3233
"psr/log": "^1.0"
3334
},
3435
"suggest": {

src/Gitonomy/Git/Admin.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Gitonomy\Git;
1313

1414
use Gitonomy\Git\Exception\RuntimeException;
15-
use Symfony\Component\Process\ProcessBuilder;
15+
use Symfony\Component\Process\Process;
1616

1717
/**
1818
* Administration class for Git repositories.
@@ -153,10 +153,17 @@ private static function getProcess($command, array $args = array(), array $optio
153153
'process_timeout' => 3600,
154154
), $options);
155155

156-
$builder = ProcessBuilder::create(array_merge(array($options['command'], $command), $args));
157-
$builder->inheritEnvironmentVariables(false);
156+
$commandline = array_merge(array($options['command'], $command), $args);
158157

159-
$process = $builder->getProcess();
158+
// Backward compatible layer for Symfony Process < 4.0.
159+
if (class_exists('Symfony\Component\Process\ProcessBuilder')) {
160+
$commandline = implode(' ', array_map(
161+
'Symfony\Component\Process\ProcessUtils::escapeArgument',
162+
$commandline
163+
));
164+
}
165+
166+
$process = new Process($commandline);
160167
$process->setEnv($options['environment_variables']);
161168
$process->setTimeout($options['process_timeout']);
162169
$process->setIdleTimeout($options['process_timeout']);

src/Gitonomy/Git/Repository.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Gitonomy\Git\Exception\RuntimeException;
1818
use Psr\Log\LoggerInterface;
1919
use Symfony\Component\Process\Process;
20-
use Symfony\Component\Process\ProcessBuilder;
20+
use Symfony\Component\Process\ProcessUtils;
2121

2222
/**
2323
* Git repository object.
@@ -417,8 +417,9 @@ public function getDiff($revisions)
417417
*/
418418
public function getSize()
419419
{
420-
$process = ProcessBuilder::create(array('du', '-skc', $this->gitDir))->getProcess();
421-
420+
$commandlineArguments = array('du', '-skc', $this->gitDir);
421+
$commandline = $this->normalizeCommandlineArguments($commandlineArguments);
422+
$process = new Process($commandline);
422423
$process->run();
423424

424425
if (!preg_match('/(\d+)\s+total$/', trim($process->getOutput()), $vars)) {
@@ -620,13 +621,47 @@ private function getProcess($command, $args = array())
620621

621622
$base[] = $command;
622623

623-
$builder = new ProcessBuilder(array_merge($base, $args));
624-
$builder->inheritEnvironmentVariables(false);
625-
$process = $builder->getProcess();
624+
$commandlineArguments = array_merge($base, $args);
625+
$commandline = $this->normalizeCommandlineArguments($commandlineArguments);
626+
627+
$process = new Process($commandline);
626628
$process->setEnv($this->environmentVariables);
627629
$process->setTimeout($this->processTimeout);
628630
$process->setIdleTimeout($this->processTimeout);
629631

630632
return $process;
631633
}
634+
635+
/**
636+
* This internal helper method is used to convert an array of commandline
637+
* arguments to an escaped commandline string for older versions of the
638+
* Symfony Process component.
639+
*
640+
* It acts as a backward compatible layer for Symfony Process < 3.3.
641+
*
642+
* @param array $arguments a list of command line arguments
643+
*
644+
* @return string|array a single escaped string (< 4.0) or a raw array of
645+
* the arguments passed in (4.0+)
646+
*
647+
* @see Process::escapeArgument()
648+
* @see ProcessUtils::escapeArgument()
649+
*/
650+
private function normalizeCommandlineArguments(array $arguments)
651+
{
652+
// From version 4.0 and onwards, the Process accepts an array of
653+
// arguments, and escaping is taken care of automatically.
654+
if (!class_exists('Symfony\Component\Process\ProcessBuilder')) {
655+
return $arguments;
656+
}
657+
658+
// For version < 3.3, the Process only accepts a simple string
659+
// as commandline, and escaping has to happen manually.
660+
$commandline = implode(' ', array_map(
661+
'Symfony\Component\Process\ProcessUtils::escapeArgument',
662+
$arguments
663+
));
664+
665+
return $commandline;
666+
}
632667
}

tests/Gitonomy/Git/Tests/AbstractTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
use Gitonomy\Git\Admin;
1515
use Gitonomy\Git\Repository;
16+
use PHPUnit\Framework\TestCase;
1617

17-
abstract class AbstractTest extends \PHPUnit_Framework_TestCase
18+
abstract class AbstractTest extends TestCase
1819
{
1920
const REPOSITORY_URL = 'http://github.com/gitonomy/foobar.git';
2021

0 commit comments

Comments
 (0)