diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 94fccc2e5..722bc94cc 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -12,6 +12,7 @@ namespace Composer\Command; +use Composer\Util\Platform; use Composer\Util\Silencer; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; @@ -184,7 +185,7 @@ EOT if ($input->getOption('editor')) { $editor = escapeshellcmd(getenv('EDITOR')); if (!$editor) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $editor = 'notepad'; } else { foreach (array('vim', 'vi', 'nano', 'pico', 'ed') as $candidate) { @@ -197,7 +198,7 @@ EOT } $file = $input->getOption('auth') ? $this->authConfigFile->getPath() : $this->configFile->getPath(); - system($editor . ' ' . $file . (defined('PHP_WINDOWS_VERSION_BUILD') ? '' : ' > `tty`')); + system($editor . ' ' . $file . (Platform::isWindows() ? '' : ' > `tty`')); return 0; } diff --git a/src/Composer/Command/HomeCommand.php b/src/Composer/Command/HomeCommand.php index fff1f86eb..150b8c71b 100644 --- a/src/Composer/Command/HomeCommand.php +++ b/src/Composer/Command/HomeCommand.php @@ -16,6 +16,7 @@ use Composer\Factory; use Composer\Package\CompletePackageInterface; use Composer\Repository\RepositoryInterface; use Composer\Repository\ArrayRepository; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -117,7 +118,7 @@ EOT { $url = ProcessExecutor::escape($url); - if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + if (Platform::isWindows()) { return passthru('start "web" explorer "' . $url . '"'); } diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index e3e1ffd71..5ddce2954 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -20,6 +20,7 @@ use Composer\Semver\VersionParser; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; use Composer\Package\PackageInterface; +use Composer\Util\Platform; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; @@ -232,7 +233,7 @@ EOT // outside of a real terminal, use space without a limit $width = PHP_INT_MAX; } - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $width--; } diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index f77876100..07d517eae 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -12,6 +12,7 @@ namespace Composer\Console; +use Composer\Util\Platform; use Composer\Util\Silencer; use Symfony\Component\Console\Application as BaseApplication; use Symfony\Component\Console\Input\InputInterface; @@ -219,7 +220,7 @@ class Application extends BaseApplication } Silencer::restore(); - if (defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) { + if (Platform::isWindows() && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) { $io->writeError('The following exception may be caused by a stale entry in your cmd.exe AutoRun', true, IOInterface::QUIET); $io->writeError('Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details', true, IOInterface::QUIET); } diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 7db561889..48e0d4b39 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -14,6 +14,7 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; use Composer\Util\Git as GitUtil; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\IO\IOInterface; use Composer\Util\Filesystem; @@ -43,7 +44,7 @@ class GitDownloader extends VcsDownloader $path = $this->normalizePath($path); $ref = $package->getSourceReference(); - $flag = defined('PHP_WINDOWS_VERSION_MAJOR') ? '/D ' : ''; + $flag = Platform::isWindows() ? '/D ' : ''; $command = 'git clone --no-checkout %s %s && cd '.$flag.'%2$s && git remote add composer %1$s && git fetch composer'; $this->io->writeError(" Cloning ".$ref); @@ -353,7 +354,7 @@ class GitDownloader extends VcsDownloader protected function normalizePath($path) { - if (defined('PHP_WINDOWS_VERSION_MAJOR') && strlen($path) > 0) { + if (Platform::isWindows() && strlen($path) > 0) { $basePath = $path; $removed = array(); diff --git a/src/Composer/Downloader/GzipDownloader.php b/src/Composer/Downloader/GzipDownloader.php index ae7d7f17a..819af79f0 100644 --- a/src/Composer/Downloader/GzipDownloader.php +++ b/src/Composer/Downloader/GzipDownloader.php @@ -16,6 +16,7 @@ use Composer\Config; use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; use Composer\Package\PackageInterface; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; @@ -40,7 +41,7 @@ class GzipDownloader extends ArchiveDownloader $targetFilepath = $path . DIRECTORY_SEPARATOR . basename(substr($file, 0, -3)); // Try to use gunzip on *nix - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if (!Platform::isWindows()) { $command = 'gzip -cd ' . ProcessExecutor::escape($file) . ' > ' . ProcessExecutor::escape($targetFilepath); if (0 === $this->process->execute($command, $ignoredOutput)) { diff --git a/src/Composer/Downloader/RarDownloader.php b/src/Composer/Downloader/RarDownloader.php index 81e11785e..2a0c98cf9 100644 --- a/src/Composer/Downloader/RarDownloader.php +++ b/src/Composer/Downloader/RarDownloader.php @@ -15,6 +15,7 @@ namespace Composer\Downloader; use Composer\Config; use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; @@ -42,7 +43,7 @@ class RarDownloader extends ArchiveDownloader $processError = null; // Try to use unrar on *nix - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if (!Platform::isWindows()) { $command = 'unrar x ' . ProcessExecutor::escape($file) . ' ' . ProcessExecutor::escape($path) . ' && chmod -R u+w ' . ProcessExecutor::escape($path); if (0 === $this->process->execute($command, $ignoredOutput)) { @@ -65,7 +66,7 @@ class RarDownloader extends ArchiveDownloader $error = "Could not decompress the archive, enable the PHP rar extension or install unrar.\n" . $iniMessage . "\n" . $processError; - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if (!Platform::isWindows()) { $error = "Could not decompress the archive, enable the PHP rar extension.\n" . $iniMessage; } diff --git a/src/Composer/Downloader/ZipDownloader.php b/src/Composer/Downloader/ZipDownloader.php index 6faaaaa4f..5f483975c 100644 --- a/src/Composer/Downloader/ZipDownloader.php +++ b/src/Composer/Downloader/ZipDownloader.php @@ -15,6 +15,7 @@ namespace Composer\Downloader; use Composer\Config; use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; @@ -38,7 +39,7 @@ class ZipDownloader extends ArchiveDownloader $processError = null; // try to use unzip on *nix - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if (!Platform::isWindows()) { $command = 'unzip '.ProcessExecutor::escape($file).' -d '.ProcessExecutor::escape($path) . ' && chmod -R u+w ' . ProcessExecutor::escape($path); try { if (0 === $this->process->execute($command, $ignoredOutput)) { @@ -64,7 +65,7 @@ class ZipDownloader extends ArchiveDownloader $error = "Could not decompress the archive, enable the PHP zip extension or install unzip.\n" . $iniMessage . "\n" . $processError; - if (!defined('PHP_WINDOWS_VERSION_BUILD')) { + if (!Platform::isWindows()) { $error = "Could not decompress the archive, enable the PHP zip extension.\n" . $iniMessage; } diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 058709275..2f3491fd4 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -20,6 +20,7 @@ use Composer\Package\Version\VersionGuesser; use Composer\Repository\RepositoryManager; use Composer\Repository\WritableRepositoryInterface; use Composer\Util\Filesystem; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Composer\Util\Silencer; @@ -51,7 +52,7 @@ class Factory return $home; } - if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + if (Platform::isWindows()) { if (!getenv('APPDATA')) { throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly'); } @@ -90,7 +91,7 @@ class Factory return $homeEnv . '/cache'; } - if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + if (Platform::isWindows()) { if ($cacheDir = getenv('LOCALAPPDATA')) { $cacheDir .= '/Composer'; } else { @@ -125,7 +126,7 @@ class Factory return $homeEnv; } - if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + if (Platform::isWindows()) { return strtr($home, '\\', '/'); } diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index b14659f7b..31090de00 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -17,6 +17,7 @@ use Composer\IO\IOInterface; use Composer\Repository\InstalledRepositoryInterface; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; use Composer\Util\Silencer; @@ -241,7 +242,7 @@ class LibraryInstaller implements InstallerInterface } if ($this->binCompat === "auto") { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $this->installFullBinaries($binPath, $link, $bin, $package); } else { $this->installSymlinkBinaries($binPath, $link); diff --git a/src/Composer/Installer/PearInstaller.php b/src/Composer/Installer/PearInstaller.php index 35f7855de..0e16fcb32 100644 --- a/src/Composer/Installer/PearInstaller.php +++ b/src/Composer/Installer/PearInstaller.php @@ -17,6 +17,7 @@ use Composer\Composer; use Composer\Downloader\PearPackageExtractor; use Composer\Repository\InstalledRepositoryInterface; use Composer\Package\PackageInterface; +use Composer\Util\Platform; use Composer\Util\ProcessExecutor; /** @@ -53,7 +54,7 @@ class PearInstaller extends LibraryInstaller parent::installCode($package); parent::initializeBinDir(); - $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); + $isWindows = Platform::isWindows(); $php_bin = $this->binDir . ($isWindows ? '/composer-php.bat' : '/composer-php'); if (!$isWindows) { diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index f7d812de8..e29a60673 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -110,7 +110,7 @@ class Filesystem return $this->removeDirectoryPhp($directory); } - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $cmd = sprintf('rmdir /S /Q %s', ProcessExecutor::escape(realpath($directory))); } else { $cmd = sprintf('rm -rf %s', ProcessExecutor::escape($directory)); @@ -181,10 +181,10 @@ class Filesystem { if (!@$this->unlinkImplementation($path)) { // retry after a bit on windows since it tends to be touchy with mass removals - if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(350000) && !@$this->unlinkImplementation($path))) { + if (!Platform::isWindows() || (usleep(350000) && !@$this->unlinkImplementation($path))) { $error = error_get_last(); $message = 'Could not delete '.$path.': ' . @$error['message']; - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $message .= "\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed"; } @@ -206,10 +206,10 @@ class Filesystem { if (!@rmdir($path)) { // retry after a bit on windows since it tends to be touchy with mass removals - if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(350000) && !@rmdir($path))) { + if (!Platform::isWindows() || (usleep(350000) && !@rmdir($path))) { $error = error_get_last(); $message = 'Could not delete '.$path.': ' . @$error['message']; - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $message .= "\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed"; } @@ -264,7 +264,7 @@ class Filesystem return $this->copyThenRemove($source, $target); } - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { // Try to copy & delete - this is a workaround for random "Access denied" errors. $command = sprintf('xcopy %s %s /E /I /Q /Y', ProcessExecutor::escape($source), ProcessExecutor::escape($target)); $result = $this->processExecutor->execute($command, $output); @@ -460,7 +460,7 @@ class Filesystem public static function getPlatformPath($path) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $path = preg_replace('{^(?:file:///([a-z])/)}i', 'file://$1:/', $path); } @@ -498,7 +498,7 @@ class Filesystem */ private function unlinkImplementation($path) { - if (defined('PHP_WINDOWS_VERSION_BUILD') && is_dir($path) && is_link($path)) { + if (Platform::isWindows() && is_dir($path) && is_link($path)) { return rmdir($path); } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index c1eaeebe9..b57a64a1f 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -51,10 +51,7 @@ class Perforce public static function create($repoConfig, $port, $path, ProcessExecutor $process, IOInterface $io) { - $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); - $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows, $io); - - return $perforce; + return new Perforce($repoConfig, $port, $path, $process, Platform::isWindows(), $io); } public static function checkServerExists($url, ProcessExecutor $processExecutor) diff --git a/src/Composer/Util/Platform.php b/src/Composer/Util/Platform.php new file mode 100644 index 000000000..eafb88b7a --- /dev/null +++ b/src/Composer/Util/Platform.php @@ -0,0 +1,28 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +/** + * Platform helper for uniform platform-specific tests. + * + * @author Niels Keurentjes + */ +class Platform +{ + /** + * @return bool Whether the host machine is running a Windows OS + */ + public static function isWindows() + { + return defined('PHP_WINDOWS_VERSION_BUILD'); + } +} diff --git a/src/Composer/Util/ProcessExecutor.php b/src/Composer/Util/ProcessExecutor.php index f9499f09f..6b778e5eb 100644 --- a/src/Composer/Util/ProcessExecutor.php +++ b/src/Composer/Util/ProcessExecutor.php @@ -50,7 +50,7 @@ class ProcessExecutor // make sure that null translate to the proper directory in case the dir is a symlink // and we call a git command, because msysgit does not handle symlinks properly - if (null === $cwd && defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($command, 'git') && getcwd()) { + if (null === $cwd && Platform::isWindows() && false !== strpos($command, 'git') && getcwd()) { $cwd = realpath(getcwd()); } diff --git a/src/Composer/Util/TlsHelper.php b/src/Composer/Util/TlsHelper.php index 6ea5cf591..721e93825 100644 --- a/src/Composer/Util/TlsHelper.php +++ b/src/Composer/Util/TlsHelper.php @@ -175,7 +175,7 @@ final class TlsHelper return self::$useOpensslParse = true; } - if ('\\' === DIRECTORY_SEPARATOR) { + if (Platform::isWindows()) { // Windows is probably insecure in this case. return self::$useOpensslParse = false; } diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index 26437ada5..9c851fe9a 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -16,6 +16,7 @@ use Composer\Downloader\GitDownloader; use Composer\Config; use Composer\TestCase; use Composer\Util\Filesystem; +use Composer\Util\Platform; class GitDownloaderTest extends TestCase { @@ -353,7 +354,7 @@ class GitDownloaderTest extends TestCase private function winCompat($cmd) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $cmd = str_replace('cd ', 'cd /D ', $cmd); $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd); diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index 75dfa84aa..6b660e383 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -15,6 +15,7 @@ namespace Composer\Test\Downloader; use Composer\Downloader\HgDownloader; use Composer\TestCase; use Composer\Util\Filesystem; +use Composer\Util\Platform; class HgDownloaderTest extends TestCase { @@ -156,10 +157,6 @@ class HgDownloaderTest extends TestCase private function getCmd($cmd) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - return strtr($cmd, "'", '"'); - } - - return $cmd; + return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd; } } diff --git a/tests/Composer/Test/Downloader/XzDownloaderTest.php b/tests/Composer/Test/Downloader/XzDownloaderTest.php index 418776d75..d8e77a2cb 100644 --- a/tests/Composer/Test/Downloader/XzDownloaderTest.php +++ b/tests/Composer/Test/Downloader/XzDownloaderTest.php @@ -15,6 +15,7 @@ namespace Composer\Test\Downloader; use Composer\Downloader\XzDownloader; use Composer\TestCase; use Composer\Util\Filesystem; +use Composer\Util\Platform; use Composer\Util\RemoteFilesystem; class XzDownloaderTest extends TestCase @@ -31,7 +32,7 @@ class XzDownloaderTest extends TestCase public function setUp() { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { $this->markTestSkipped('Skip test on Windows'); } $this->testDir = $this->getUniqueTmpDirectory(); diff --git a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php index c2ae497ca..881b86ea2 100644 --- a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php @@ -16,6 +16,7 @@ use Composer\Repository\Vcs\SvnDriver; use Composer\Config; use Composer\TestCase; use Composer\Util\Filesystem; +use Composer\Util\Platform; class SvnDriverTest extends TestCase { @@ -71,7 +72,7 @@ class SvnDriverTest extends TestCase private function getCmd($cmd) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { + if (Platform::isWindows()) { return strtr($cmd, "'", '"'); } diff --git a/tests/Composer/Test/Util/PlatformTest.php b/tests/Composer/Test/Util/PlatformTest.php new file mode 100644 index 000000000..3d82fb96f --- /dev/null +++ b/tests/Composer/Test/Util/PlatformTest.php @@ -0,0 +1,29 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Util; + +use Composer\Util\Platform; + +/** + * PlatformTest + * + * @author Niels Keurentjes + */ +class PlatformTest extends \PHPUnit_Framework_TestCase +{ + public function testWindows() + { + // Compare 2 common tests for Windows to the built-in Windows test + $this->assertEquals(('\\' === DIRECTORY_SEPARATOR), Platform::isWindows()); + $this->assertEquals(defined('PHP_WINDOWS_VERSION_MAJOR'), Platform::isWindows()); + } +} diff --git a/tests/Composer/Test/Util/SvnTest.php b/tests/Composer/Test/Util/SvnTest.php index 55a116376..c16b0e6ce 100644 --- a/tests/Composer/Test/Util/SvnTest.php +++ b/tests/Composer/Test/Util/SvnTest.php @@ -14,6 +14,7 @@ namespace Composer\Test\Util; use Composer\Config; use Composer\IO\NullIO; +use Composer\Util\Platform; use Composer\Util\Svn; class SvnTest extends \PHPUnit_Framework_TestCase @@ -131,10 +132,6 @@ class SvnTest extends \PHPUnit_Framework_TestCase private function getCmd($cmd) { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - return strtr($cmd, "'", '"'); - } - - return $cmd; + return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd; } }