forked from composer/composer
/
ComposerRepository.php
83 lines (71 loc) · 2.45 KB
/
ComposerRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?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\Repository;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface;
use Composer\Util\RemoteFilesystem;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ComposerRepository extends ArrayRepository
{
protected $url;
protected $io;
protected $packages;
public function __construct(array $config, IOInterface $io)
{
if (!preg_match('{^\w+://}', $config['url'])) {
// assume http as the default protocol
$config['url'] = 'http://'.$config['url'];
}
$config['url'] = rtrim($config['url'], '/');
if (function_exists('filter_var') && !filter_var($config['url'], FILTER_VALIDATE_URL)) {
throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$config['url']);
}
$this->url = $config['url'];
$this->io = $io;
}
protected function initialize()
{
parent::initialize();
$json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io));
$data = $json->read();
$loader = new ArrayLoader();
$this->loadRepository($loader, $data);
}
protected function loadRepository(ArrayLoader $loader, $data)
{
// legacy repo handling
if (!isset($data['packages']) && !isset($data['includes'])) {
foreach ($data as $pkg) {
foreach ($pkg['versions'] as $metadata) {
$this->addPackage($loader->load($metadata));
}
}
return;
}
if (isset($data['packages'])) {
foreach ($data['packages'] as $package => $versions) {
foreach ($versions as $version => $metadata) {
$this->addPackage($loader->load($metadata));
}
}
}
if (isset($data['includes'])) {
foreach ($data['includes'] as $include => $metadata) {
$json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io));
$this->loadRepository($loader, $json->read());
}
}
}
}