From 897ff9126da3ae4008b42ac516e06f73c4cb5086 Mon Sep 17 00:00:00 2001 From: digitalkaoz Date: Thu, 5 Jan 2012 11:12:54 +0100 Subject: [PATCH] seperate class for Process, using this one all over the place --- src/Composer/Compiler.php | 2 +- src/Composer/Downloader/GitDownloader.php | 8 ++-- src/Composer/Downloader/HgDownloader.php | 8 ++-- src/Composer/Downloader/SvnDownloader.php | 6 +-- src/Composer/Downloader/Util/Filesystem.php | 34 ++------------- src/Composer/Downloader/ZipDownloader.php | 2 +- src/Composer/Repository/Vcs/GitDriver.php | 16 +++---- src/Composer/Repository/Vcs/HgDriver.php | 18 ++++---- src/Composer/Repository/Vcs/SvnDriver.php | 14 +++--- src/Composer/Util/Process.php | 47 +++++++++++++++++++++ 10 files changed, 88 insertions(+), 67 deletions(-) create mode 100644 src/Composer/Util/Process.php diff --git a/src/Composer/Compiler.php b/src/Composer/Compiler.php index ebce05559..a4c1e5986 100644 --- a/src/Composer/Compiler.php +++ b/src/Composer/Compiler.php @@ -13,7 +13,7 @@ namespace Composer; use Symfony\Component\Finder\Finder; -use Symfony\Component\Process\Process; +use Composer\Util\Process; /** * The Compiler class compiles composer into a phar diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 161dfa5ce..ebb546a96 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -13,7 +13,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Jordi Boggiano @@ -39,7 +39,7 @@ class GitDownloader implements DownloaderInterface $url = escapeshellarg($package->getSourceUrl()); $ref = escapeshellarg($package->getSourceReference()); - Filesystem::runProcess(sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref)); + Process::execute(sprintf('git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s', $url, $path, $ref)); } /** @@ -52,7 +52,7 @@ class GitDownloader implements DownloaderInterface } $this->enforceCleanDirectory($path); - Filesystem::runProcess(sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $target->getSourceReference())); + Process::execute(sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $target->getSourceReference())); } /** @@ -67,7 +67,7 @@ class GitDownloader implements DownloaderInterface private function enforceCleanDirectory($path) { - Filesystem::runProcess(sprintf('cd %s && git status --porcelain', $path),$output); + Process::execute(sprintf('cd %s && git status --porcelain', $path),$output); if (implode('', $output)) { throw new \RuntimeException('Source directory has uncommitted changes'); } diff --git a/src/Composer/Downloader/HgDownloader.php b/src/Composer/Downloader/HgDownloader.php index 80a60dc8d..b7d7fbbe0 100644 --- a/src/Composer/Downloader/HgDownloader.php +++ b/src/Composer/Downloader/HgDownloader.php @@ -13,7 +13,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Per Bernhardt @@ -39,7 +39,7 @@ class HgDownloader implements DownloaderInterface $url = escapeshellarg($package->getSourceUrl()); $ref = escapeshellarg($package->getSourceReference()); - Filesystem::runProcess(sprintf('(hg clone %s %s 2> /dev/null) && cd %2$s && hg up %s', $url, $path, $ref)); + Process::execute(sprintf('(hg clone %s %s 2> /dev/null) && cd %2$s && hg up %s', $url, $path, $ref)); } /** @@ -52,7 +52,7 @@ class HgDownloader implements DownloaderInterface } $this->enforceCleanDirectory($path); - Filesystem::runProcess(sprintf('cd %s && hg pull && hg up %s', $path, escapeshellarg($target->getSourceReference()))); + Process::execute(sprintf('cd %s && hg pull && hg up %s', $path, escapeshellarg($target->getSourceReference()))); } /** @@ -67,7 +67,7 @@ class HgDownloader implements DownloaderInterface private function enforceCleanDirectory($path) { - Filesystem::runProcess(sprintf('cd %s && hg st', $path), $output); + Process::execute(sprintf('cd %s && hg st', $path), $output); if (implode('', $output)) { throw new \RuntimeException('Source directory has uncommitted changes'); } diff --git a/src/Composer/Downloader/SvnDownloader.php b/src/Composer/Downloader/SvnDownloader.php index 226a125c1..d6577a0b1 100644 --- a/src/Composer/Downloader/SvnDownloader.php +++ b/src/Composer/Downloader/SvnDownloader.php @@ -3,7 +3,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Ben Bieker @@ -29,7 +29,7 @@ class SvnDownloader implements DownloaderInterface $url = escapeshellarg($package->getSourceUrl()); $ref = escapeshellarg($package->getSourceReference()); - Filesystem::runProcess(sprintf('svn co %s/%s %s', $url, $ref, $path)); + Process::execute(sprintf('svn co %s/%s %s', $url, $ref, $path)); } /** @@ -43,7 +43,7 @@ class SvnDownloader implements DownloaderInterface $url = escapeshellarg($target->getSourceUrl()); $ref = escapeshellarg($target->getSourceReference()); - Filesystem::runProcess(sprintf('cd %s && svn switch %s/%s', $path, $url, $ref)); + Process::execute(sprintf('cd %s && svn switch %s/%s', $path, $url, $ref)); } /** diff --git a/src/Composer/Downloader/Util/Filesystem.php b/src/Composer/Downloader/Util/Filesystem.php index 4c8d80d80..6e73faa63 100644 --- a/src/Composer/Downloader/Util/Filesystem.php +++ b/src/Composer/Downloader/Util/Filesystem.php @@ -12,7 +12,7 @@ namespace Composer\Downloader\Util; -use Symfony\Component\Process\Process; +use Composer\Util\Process; /** * @author Jordi Boggiano @@ -22,9 +22,9 @@ class Filesystem public function removeDirectory($directory) { if (defined('PHP_WINDOWS_VERSION_BUILD')) { - static::runProcess(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory)))); + Process::execute(sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory)))); } else { - static::runProcess(sprintf('rm -rf %s', escapeshellarg($directory))); + Process::execute(sprintf('rm -rf %s', escapeshellarg($directory))); } } @@ -127,30 +127,4 @@ class Filesystem { return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; } - - /** - * runs a process on the commandline - * - * @static - * @param $command the command to execute - * @param null $output the output will be written into this var if passed - * @return int statuscode - */ - public static function runProcess($command, &$output = null) - { - $process = new Process($command); - $process->run(function($type, $buffer) use ($output) { - if (null === $output) { - return; - } - - echo $buffer; - }); - - if (null !== $output) { - $output = $process->getOutput(); - } - - return $process->getExitCode(); - } -} +} \ No newline at end of file diff --git a/src/Composer/Downloader/ZipDownloader.php b/src/Composer/Downloader/ZipDownloader.php index ca19366b8..d89417c4f 100644 --- a/src/Composer/Downloader/ZipDownloader.php +++ b/src/Composer/Downloader/ZipDownloader.php @@ -13,7 +13,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; -use Symfony\Component\Process\Process; +use Composer\Util\Process; /** * @author Jordi Boggiano diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index 5af9016e5..1db051313 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -3,7 +3,7 @@ namespace Composer\Repository\Vcs; use Composer\Json\JsonFile; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Jordi Boggiano @@ -30,9 +30,9 @@ class GitDriver implements VcsDriverInterface $url = escapeshellarg($this->url); $tmpDir = escapeshellarg($this->tmpDir); if (is_dir($this->tmpDir)) { - Filesystem::runProcess(sprintf('cd %s && git fetch origin', $tmpDir), $output); + Process::execute(sprintf('cd %s && git fetch origin', $tmpDir), $output); } else { - Filesystem::runProcess(sprintf('git clone %s %s', $url, $tmpDir), $output); + Process::execute(sprintf('git clone %s %s', $url, $tmpDir), $output); } $this->getTags(); @@ -46,7 +46,7 @@ class GitDriver implements VcsDriverInterface { if (null === $this->rootIdentifier) { $this->rootIdentifier = 'master'; - Filesystem::runProcess(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output); + Process::execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output); foreach ($output as $branch) { if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) { $this->rootIdentifier = $match[1]; @@ -90,7 +90,7 @@ class GitDriver implements VcsDriverInterface public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { - Filesystem::runProcess(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); + Process::execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $composer = implode("\n", $output); unset($output); @@ -101,7 +101,7 @@ class GitDriver implements VcsDriverInterface $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { - Filesystem::runProcess(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); + Process::execute(sprintf('cd %s && git log -1 --format=%%at %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $date = new \DateTime('@'.$output[0]); $composer['time'] = $date->format('Y-m-d H:i:s'); } @@ -117,7 +117,7 @@ class GitDriver implements VcsDriverInterface public function getTags() { if (null === $this->tags) { - Filesystem::runProcess(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output); + Process::execute(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output); $this->tags = $output ? array_combine($output, $output) : array(); } @@ -132,7 +132,7 @@ class GitDriver implements VcsDriverInterface if (null === $this->branches) { $branches = array(); - Filesystem::runProcess(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output); + Process::execute(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output); foreach ($output as $branch) { if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { preg_match('{^ *[^/]+/(\S+) *([a-f0-9]+) .*$}', $branch, $match); diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php index 50f2b2081..b62dfb530 100644 --- a/src/Composer/Repository/Vcs/HgDriver.php +++ b/src/Composer/Repository/Vcs/HgDriver.php @@ -13,7 +13,7 @@ namespace Composer\Repository\Vcs; use Composer\Json\JsonFile; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Per Bernhardt @@ -40,9 +40,9 @@ class HgDriver implements VcsDriverInterface $url = escapeshellarg($this->url); $tmpDir = escapeshellarg($this->tmpDir); if (is_dir($this->tmpDir)) { - Filesystem::runProcess(sprintf('cd %s && hg pull -u', $tmpDir), $output); + Process::execute(sprintf('cd %s && hg pull -u', $tmpDir), $output); } else { - Filesystem::runProcess(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output); + Process::execute(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output); } $this->getTags(); @@ -56,7 +56,7 @@ class HgDriver implements VcsDriverInterface { $tmpDir = escapeshellarg($this->tmpDir); if (null === $this->rootIdentifier) { - Filesystem::runProcess(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output); + Process::execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output); $this->rootIdentifier = $output[0]; } @@ -95,7 +95,7 @@ class HgDriver implements VcsDriverInterface public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { - Filesystem::runProcess(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); + Process::execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $composer = implode("\n", $output); unset($output); @@ -106,7 +106,7 @@ class HgDriver implements VcsDriverInterface $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { - Filesystem::runProcess(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); + Process::execute(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $date = new \DateTime($output[0]); $composer['time'] = $date->format('Y-m-d H:i:s'); } @@ -124,7 +124,7 @@ class HgDriver implements VcsDriverInterface if (null === $this->tags) { $tags = array(); - Filesystem::runProcess(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output); + Process::execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output); foreach ($output as $tag) { if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) $tags[$match[1]] = $match[2]; @@ -144,7 +144,7 @@ class HgDriver implements VcsDriverInterface if (null === $this->branches) { $branches = array(); - Filesystem::runProcess(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output); + Process::execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output); foreach ($output as $branch) { if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) $branches[$match[1]] = $match[2]; @@ -183,7 +183,7 @@ class HgDriver implements VcsDriverInterface return false; } - $exit = Filesystem::runProcess(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored); + $exit = Process::execute(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored); return $exit === 0; } } diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index 441398632..3a3e7c2ed 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -3,7 +3,7 @@ namespace Composer\Repository\Vcs; use Composer\Json\JsonFile; -use Composer\Downloader\Util\Filesystem; +use Composer\Util\Process; /** * @author Jordi Boggiano @@ -79,7 +79,7 @@ class SvnDriver implements VcsDriverInterface $rev = ''; } - Filesystem::runProcess(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output); + Process::execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output); $composer = implode("\n", $output); unset($output); @@ -90,7 +90,7 @@ class SvnDriver implements VcsDriverInterface $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { - Filesystem::runProcess(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output); + Process::execute(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output); foreach ($output as $line) { if (preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) { $date = new \DateTime($match[1]); @@ -111,7 +111,7 @@ class SvnDriver implements VcsDriverInterface public function getTags() { if (null === $this->tags) { - Filesystem::runProcess(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output); + Process::execute(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output); $this->tags = array(); foreach ($output as $tag) { $this->tags[rtrim($tag, '/')] = '/tags/'.$tag; @@ -127,7 +127,7 @@ class SvnDriver implements VcsDriverInterface public function getBranches() { if (null === $this->branches) { - Filesystem::runProcess(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output); + Process::execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output); $this->branches = array(); foreach ($output as $line) { @@ -139,7 +139,7 @@ class SvnDriver implements VcsDriverInterface } unset($output); - Filesystem::runProcess(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output); + Process::execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output); foreach ($output as $line) { preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match); if ($match[2] === './') { @@ -179,7 +179,7 @@ class SvnDriver implements VcsDriverInterface return false; } - $exit = Filesystem::runProcess(sprintf('svn info --non-interactive %s 2>/dev/null', escapeshellarg($url)), $ignored); + $exit = Process::execute(sprintf('svn info --non-interactive %s 2>/dev/null', escapeshellarg($url)), $ignored); return $exit === 0; } } diff --git a/src/Composer/Util/Process.php b/src/Composer/Util/Process.php new file mode 100644 index 000000000..1a34b0ea7 --- /dev/null +++ b/src/Composer/Util/Process.php @@ -0,0 +1,47 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +use Symfony\Component\Process\Process as BaseProcess; + +/** + * @author Robert Schönthal + */ +class Process extends BaseProcess +{ + /** + * runs a process on the commandline + * + * @static + * @param $command the command to execute + * @param null $output the output will be written into this var if passed + * @return int statuscode + */ + public static function execute($command, &$output = null) + { + $process = new static($command); + $process->run(function($type, $buffer) use ($output) { + if (null === $output) { + return; + } + + echo $buffer; + }); + + if (null !== $output) { + $output = $process->getOutput(); + } + + return $process->getExitCode(); + } +} \ No newline at end of file