From 4b176f11f2e4532bca937aca9a85705446b5da91 Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Sun, 24 Mar 2013 20:27:50 +0100 Subject: [PATCH] added artifact repository initial functionality --- src/Composer/Factory.php | 1 + .../Repository/ArtifactRepository.php | 78 ++++++++++++++++++ .../Repository/ArtifactRepositoryTest.php | 36 ++++++++ .../Fixtures/artifacts/package0.zip | Bin 0 -> 234 bytes .../Fixtures/artifacts/package2.zip | Bin 0 -> 234 bytes 5 files changed, 115 insertions(+) create mode 100644 src/Composer/Repository/ArtifactRepository.php create mode 100644 tests/Composer/Test/Repository/ArtifactRepositoryTest.php create mode 100644 tests/Composer/Test/Repository/Fixtures/artifacts/package0.zip create mode 100644 tests/Composer/Test/Repository/Fixtures/artifacts/package2.zip diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 9234f46f8..72a4699a1 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -281,6 +281,7 @@ class Factory $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository'); + $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository'); return $rm; } diff --git a/src/Composer/Repository/ArtifactRepository.php b/src/Composer/Repository/ArtifactRepository.php new file mode 100644 index 000000000..4cf2b4b52 --- /dev/null +++ b/src/Composer/Repository/ArtifactRepository.php @@ -0,0 +1,78 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Repository; + +/** + * @author Serge Smertin + */ +use Composer\IO\IOInterface; +use Composer\Config; +use Composer\Json\JsonFile; +use Composer\Package\Loader\LoaderInterface; +use Composer\Package\Version\VersionParser; +use Composer\Package\Loader\ArrayLoader; + +class ArtifactRepository extends ArrayRepository +{ + protected $path; + + /** @var LoaderInterface */ + protected $loader; + + public function __construct(array $repoConfig, IOInterface $io, Config $config, array $drivers = null) + { + $this->path = $repoConfig['url']; + } + + protected function initialize() + { + parent::initialize(); + $this->versionParser = new VersionParser; + if (!$this->loader) { + $this->loader = new ArrayLoader($this->versionParser); + } + + $this->getDirectoryPackages($this->path); + } + + private function getDirectoryPackages($path) + { + foreach(new \RecursiveDirectoryIterator($path) as $file) { + /* @var $file \SplFileInfo */ + if(!$file->isFile()) { + continue; + } + + $package = $this->getComposerInformation($file); + if(!$package) { + // @todo add log + continue; + } + + $package = $this->loader->load($package); + + $this->addPackage($package); + } + } + + private function getComposerInformation(\SplFileInfo $file) + { + $config = "zip://{$file->getPathname()}#composer.json"; + $json = @file_get_contents($config); + if(!$json) { + return false; + } + + return JsonFile::parseJson($json, $config); + } +} diff --git a/tests/Composer/Test/Repository/ArtifactRepositoryTest.php b/tests/Composer/Test/Repository/ArtifactRepositoryTest.php new file mode 100644 index 000000000..af412420e --- /dev/null +++ b/tests/Composer/Test/Repository/ArtifactRepositoryTest.php @@ -0,0 +1,36 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Repository; + +use Composer\Test\TestCase; +use Composer\IO\NullIO; +use Composer\Config; +use Composer\Package\Package; + +class ArtifactRepositoryTest extends TestCase +{ + public function testExtractsConfigsFromZipArchives() { + $expectedPackages = array( + 'vendor0/package0-0.0.1', + 'vendor1/package2-4.3.2', + ); + + $coordinates = array('type' => 'artifact', 'url' => __DIR__ . '/Fixtures/artifacts'); + $repo = new ArtifactRepository($coordinates, new NullIO(), new Config()); + + $foundPackages = array_map(function(Package $package) { + return "{$package->getPrettyName()}-{$package->getPrettyVersion()}"; + }, $repo->getPackages()); + + $this->assertEquals($expectedPackages, $foundPackages); + } +} diff --git a/tests/Composer/Test/Repository/Fixtures/artifacts/package0.zip b/tests/Composer/Test/Repository/Fixtures/artifacts/package0.zip new file mode 100644 index 0000000000000000000000000000000000000000..855c6a64d211e845addf9bd7d9c02c913968b2c5 GIT binary patch literal 234 zcmWIWW@Zs#-~hs3b1R$}kN_`(07G(qZb5!=YLQ-6aeiKC1P=qhzkgtamw(__MiGY9 z&w?13mhL%nCqz}7b?K%nd#3zRV*cB7OUqYBwWjks-QyC-d)QwkaY+NCv7Bej> zWX{a_?2`;H7+)|Bd9;d^A;6oRBU7R13&`F;AP(?mWD;dYI0spdmw^Z7IEN*TASRmQ XIN**8@MdKLDPRObcOcyh;xGUJG2lL3 literal 0 HcmV?d00001 diff --git a/tests/Composer/Test/Repository/Fixtures/artifacts/package2.zip b/tests/Composer/Test/Repository/Fixtures/artifacts/package2.zip new file mode 100644 index 0000000000000000000000000000000000000000..6710580594d5faece782963255765fb4ee74bc1e GIT binary patch literal 234 zcmWIWW@Zs#-~d9|g%wT=NPw3?fFU_Qw;;bbwMZ|kI6p5mf``G=-#-w9zcPw2tbP{6 zz_fJFkvk!(+N?`AUD-3`kCOA>rXx?@9QhKWtSZ}4>So}jwOQZ4Fuu6jW`uK)<#-u*V2*QG(g