Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* WIP WIP WIP WIP

 * BaseDumper for utilities
 * interface to enforce 'dump()'
 * feature:
   * supports git
   * supports zip output
   * basic test to cover feature
  • Loading branch information...
commit 6f209c3611044152b6c44bb5388388ff39d62dad 1 parent a2dcf30
@till authored
View
102 src/Composer/Package/Dumper/BaseDumper.php
@@ -0,0 +1,102 @@
+<?php
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ * Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Package\Dumper;
+
+use Composer\Package\PackageInterface;
+
+/**
+ * @author Till Klampaeckel <till@php.net>
+ */
+class BaseDumper
+{
+ /**
+ * Format: zip or tarball.
+ * @var string
+ */
+ protected $format = '';
+
+ /**
+ * @var array
+ */
+ static $keys = array(
+ 'binaries' => 'bin',
+ 'scripts',
+ 'type',
+ 'extra',
+ 'installationSource' => 'installation-source',
+ 'license',
+ 'authors',
+ 'description',
+ 'homepage',
+ 'keywords',
+ 'autoload',
+ 'repositories',
+ 'includePaths' => 'include-path',
+ 'support',
+ );
+
+ /**
+ * Path to where to dump the export to.
+ * @var mixed|null
+ */
+ protected $path;
+
+ /**
+ * Working directory.
+ * @var string
+ */
+ protected $temp;
+
+ /**
+ * @param mixed $path
+ *
+ * @return $this
+ * @throws \InvalidArgumentException
+ */
+ public function __construct($path = null)
+ {
+ if (!empty($path)) {
+ if (!is_writable($path)) {
+ throw new \InvalidArgumentException("Not authorized to write to '{$path}'");
+ }
+ $this->path = $path;
+ $this->temp = sys_get_temp_dir();
+ }
+ }
+
+ /**
+ * @param \Composer\Package\PackageInterface $package
+ *
+ * @return string
+ * @throws \InvalidArgumentException When unknown 'format' is encountered.
+ */
+ public function getFilename(PackageInterface $package)
+ {
+ switch ($this->format) {
+ case 'tarball':
+ $ext = 'tar';
+ break;
+ case 'zip':
+ $ext = 'zip';
+ break;
+ default:
+ throw new \InvalidArgumentException("Format '{$this->format}' is not supported.");
+ }
+
+ $fileName = sprintf('%s-%s.%s',
+ $package->getPrettyName(),
+ $package->getVersion(),
+ $ext
+ );
+ return $fileName;
+ }
+}
View
21 src/Composer/Package/Dumper/DumperInterface.php
@@ -0,0 +1,21 @@
+<?php
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ * Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace Composer\Package\Dumper;
+
+use Composer\Package\PackageInterface;
+
+/**
+ * @author Till Klampaeckel <till@php.net>
+ */
+interface DumperInterface
+{
+ public function dump(PackageInterface $package);
+}
View
64 src/Composer/Package/Dumper/ZipDumper.php
@@ -0,0 +1,64 @@
+<?php
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ * Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Package\Dumper;
+
+use Composer\Package\Dumper\BaseDumper;
+use Composer\Package\Dumper\DumperInterface;
+use Composer\Package\PackageInterface;
+use Composer\Util\ProcessExecutor;
+use Composer\Downloader\GitDownloader;
+use Composer\IO\NullIO;
+
+/**
+ * @author Till Klampaeckel <till@php.net>
+ */
+class ZipDumper extends BaseDumper implements DumperInterface
+{
+ protected $format = 'zip';
+
+ public function dump(PackageInterface $package)
+ {
+ $workDir = sprintf('%s/zip/%s', $this->temp, $package->getName());
+ if (!file_exists($workDir)) {
+ mkdir($workDir, 0777, true);
+ }
+
+ $cwd = getcwd();
+ $fileName = $this->getFilename($package);
+ $process = new ProcessExecutor;
+ $sourceType = $package->getSourceType();
+ $sourceRef = $package->getSourceReference();
+
+ switch ($sourceType) {
+ case 'git':
+ $downloader = new GitDownloader(
+ new NullIO(),
+ $process
+ );
+ $downloader->download($package, $workDir);
+
+ $command = sprintf(
+ 'git archive --format %s --output %s %s',
+ $this->format,
+ sprintf('%s/%s', $cwd, $fileName),
+ $sourceRef
+ );
+
+ $process->execute($command, $output, $workDir);
+
+ break;
+
+ default:
+ throw new \InvalidArgumentException("Unable to handle repos of type {$sourceType} currently");
+ }
+ }
+}
View
41 tests/Composer/Test/Package/Dumper/ZipDumperTest.php
@@ -0,0 +1,41 @@
+<?php
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ * Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Test\Package\Dumper;
+
+use Composer\Package\Dumper\ZipDumper;
+use Composer\Package\MemoryPackage;
+
+class ZipDumperTest extends \PHPUnit_Framework_TestCase
+{
+ public function testThis()
+ {
+ $package = new MemoryPackage('Lagged_Session_SaveHandler_Memcache', '0.5.0', '0.5.0');
+ $package->setSourceUrl('git://github.com/lagged/Lagged_Session_SaveHandler_Memcache.git');
+ $package->setSourceReference('0.5.0');
+ $package->setSourceType('git');
+
+ $zip = new ZipDumper('/tmp');
+ $zip->dump($package);
+
+ $dist = sprintf('/%s-%s.zip', $package->getName(), $package->getVersion());
+
+ $this->assertFileExists(getcwd() . $dist);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testException()
+ {
+ new ZipDumper("/totally-random-" . time());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.