From d1d77dd13d9b7755d6e192c8ff8b608f1a95c391 Mon Sep 17 00:00:00 2001 From: Matthieu Moquet Date: Tue, 28 Aug 2012 23:37:50 +0200 Subject: [PATCH] Fixed several typos - break at first archiver supports - use standard directory separator - change exception message - remove the BaseArchiver since tar & zip archivers have been merged - plus coding style --- .../Package/Archiver/ArchiveManager.php | 13 +++--- .../Package/Archiver/BaseArchiver.php | 46 ------------------- src/Composer/Package/Archiver/GitArchiver.php | 10 ++-- .../Package/Archiver/MercurialArchiver.php | 10 +--- .../Package/Archiver/PharArchiver.php | 30 ++++++++++-- 5 files changed, 37 insertions(+), 72 deletions(-) delete mode 100644 src/Composer/Package/Archiver/BaseArchiver.php diff --git a/src/Composer/Package/Archiver/ArchiveManager.php b/src/Composer/Package/Archiver/ArchiveManager.php index f5123f7ef..ddeb0dd4f 100644 --- a/src/Composer/Package/Archiver/ArchiveManager.php +++ b/src/Composer/Package/Archiver/ArchiveManager.php @@ -69,11 +69,10 @@ class ArchiveManager } $usableArchiver = null; - $sourceType = $package->getSourceType(); - foreach ($this->archivers as $archiver) { if ($archiver->supports($format, $package->getSourceType())) { $usableArchiver = $archiver; + break; } } @@ -82,20 +81,20 @@ class ArchiveManager throw new \RuntimeException(sprintf('No archiver found to support %s format', $format)); } - $filesystem = new Filesystem(); - $packageName = str_replace('/', DIRECTORY_SEPARATOR, $package->getUniqueName()); - // Directory used to download the sources - $sources = sys_get_temp_dir().DIRECTORY_SEPARATOR.$packageName; + $filesystem = new Filesystem(); + $packageName = $package->getUniqueName(); + $sources = sys_get_temp_dir().'/'.$packageName; $filesystem->ensureDirectoryExists($sources); // Archive filename - $target = $this->buildDir.DIRECTORY_SEPARATOR.$packageName.'.'.$format; + $target = $this->buildDir.'/'.$packageName.'.'.$format; $filesystem->ensureDirectoryExists(dirname($this->buildDir.$target)); // Download sources $this->downloadManager->download($package, $sources, true); + // Create the archive $sourceRef = $package->getSourceReference(); $usableArchiver->archive($sources, $target, $format, $sourceRef); } diff --git a/src/Composer/Package/Archiver/BaseArchiver.php b/src/Composer/Package/Archiver/BaseArchiver.php deleted file mode 100644 index 69fb892a8..000000000 --- a/src/Composer/Package/Archiver/BaseArchiver.php +++ /dev/null @@ -1,46 +0,0 @@ - - * Jordi Boggiano - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Composer\Package\Archiver; - -use Composer\Package\BasePackage; -use Composer\Package\PackageInterface; - -/** - * @author Till Klampaeckel - * @author Matthieu Moquet - */ -abstract class BaseArchiver implements ArchiverInterface -{ - /** - * Create a PHAR archive. - * - * @param string $sources Path of the directory to archive - * @param string $target Path of the file archive to create - * @param int $format Format of the archive - */ - protected function createPharArchive($sources, $target, $format) - { - try { - $phar = new \PharData($target, null, null, $format); - $phar->buildFromDirectory($sources); - } catch (\UnexpectedValueException $e) { - $message = sprintf("Could not create archive '%s' from '%s': %s", - $target, - $sources, - $e->getMessage() - ); - - throw new \RuntimeException($message, $e->getCode(), $e); - } - } -} diff --git a/src/Composer/Package/Archiver/GitArchiver.php b/src/Composer/Package/Archiver/GitArchiver.php index 0a33ab756..922d9af19 100644 --- a/src/Composer/Package/Archiver/GitArchiver.php +++ b/src/Composer/Package/Archiver/GitArchiver.php @@ -34,7 +34,7 @@ class GitArchiver implements ArchiverInterface { // Since git-archive no longer works with a commit ID in git 1.7.10, // use by default the HEAD reference instead of the commit sha1 - if (null === $sourceRef || preg_match('/^[0-9a-f]{40}$/i',$sourceRef)) { + if (null === $sourceRef || preg_match('/^[0-9a-f]{40}$/i', $sourceRef)) { $sourceRef = 'HEAD'; } @@ -49,7 +49,7 @@ class GitArchiver implements ArchiverInterface if (0 !== $exitCode) { throw new \RuntimeException( - sprintf('The command `%s` returned %s', $command, $exitCode) + sprintf('Impossible to build the archive: `%s` returned %s', $command, $exitCode) ); } } @@ -59,10 +59,6 @@ class GitArchiver implements ArchiverInterface */ public function supports($format, $sourceType) { - return 'git' === $sourceType && in_array($format, array( - 'zip', - 'tar', - 'tgz', - )); + return 'git' === $sourceType && in_array($format, array('zip', 'tar', 'tgz')); } } \ No newline at end of file diff --git a/src/Composer/Package/Archiver/MercurialArchiver.php b/src/Composer/Package/Archiver/MercurialArchiver.php index 7de0430e1..9a67694df 100644 --- a/src/Composer/Package/Archiver/MercurialArchiver.php +++ b/src/Composer/Package/Archiver/MercurialArchiver.php @@ -47,7 +47,7 @@ class MercurialArchiver implements ArchiverInterface if (0 !== $exitCode) { throw new \RuntimeException( - sprintf('The command `%s` returned %s', $command, $exitCode) + sprintf('Impossible to build the archive: `%s` returned %s', $command, $exitCode) ); } } @@ -57,12 +57,6 @@ class MercurialArchiver implements ArchiverInterface */ public function supports($format, $sourceType) { - return 'hg' === $sourceType && in_array($format, array( - 'tar', - 'tbz2', - 'tgz', - 'uzip', - 'zip', - )); + return 'hg' === $sourceType && in_array($format, array('tar', 'tbz2', 'tgz', 'uzip', 'zip')); } } \ No newline at end of file diff --git a/src/Composer/Package/Archiver/PharArchiver.php b/src/Composer/Package/Archiver/PharArchiver.php index f8a369b52..02aaa0ab3 100644 --- a/src/Composer/Package/Archiver/PharArchiver.php +++ b/src/Composer/Package/Archiver/PharArchiver.php @@ -19,9 +19,9 @@ use Composer\Package\PackageInterface; * @author Till Klampaeckel * @author Matthieu Moquet */ -class PharArchiver extends BaseArchiver +class PharArchiver implements ArchiverInterface { - static public $formats = array( + static protected $formats = array( 'zip' => \Phar::ZIP, 'tar' => \Phar::TAR, ); @@ -31,7 +31,6 @@ class PharArchiver extends BaseArchiver */ public function archive($sources, $target, $format, $sourceRef = null) { - // source reference is useless for this archiver $this->createPharArchive($sources, $target, static::$formats[$format]); } @@ -40,6 +39,29 @@ class PharArchiver extends BaseArchiver */ public function supports($format, $sourceType) { - return in_array($format, array_keys(static::$formats)); + return isset(static::$formats[$format]); + } + + /** + * Create a PHAR archive. + * + * @param string $sources Path of the directory to archive + * @param string $target Path of the file archive to create + * @param int $format Format of the archive + */ + protected function createPharArchive($sources, $target, $format) + { + try { + $phar = new \PharData($target, null, null, $format); + $phar->buildFromDirectory($sources); + } catch (\UnexpectedValueException $e) { + $message = sprintf("Could not create archive '%s' from '%s': %s", + $target, + $sources, + $e->getMessage() + ); + + throw new \RuntimeException($message, $e->getCode(), $e); + } } }