/
Provider.php
167 lines (140 loc) 路 3.45 KB
/
Provider.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
/*
* This file is part of the Packagist Mirror.
*
* For the full license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Webs\Mirror;
use stdClass;
use Exception;
use Generator;
/**
* Middleware to provider operations.
*
* @author Webysther Nunes <webysther@gmail.com>
*/
class Provider
{
use Console;
/**
* @var Http
*/
protected $http;
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var array
*/
protected $providersDownloaded = [];
/**
* @var bool
*/
protected $initialized = false;
/**
* Add a http.
*
* @param Http $http
*
* @return Provider
*/
public function setHttp(Http $http):Provider
{
$this->http = $http;
return $this;
}
/**
* Add a fileSystem.
*
* @param Filesystem $fileSystem
*
* @return Provider
*/
public function setFilesystem(Filesystem $filesystem):Provider
{
$this->filesystem = $filesystem;
return $this;
}
/**
* @param string $path
*/
public function setDownloaded(string $path):Provider
{
$this->providersDownloaded[] = $path;
return $this;
}
/**
* @return array
*/
public function getDownloaded():array
{
return $this->providersDownloaded;
}
/**
* @param bool $value
*/
public function setInitialized(bool $value):Provider
{
$this->initialized = $value;
return $this;
}
/**
* Add base url of packagist.org to services on packages.json of
* mirror don't support.
*
* @param stdClass $providers List of providers from packages.json
*/
public function addFullPath(stdClass $providers):stdClass
{
// Add full path for services of mirror don't provide only packagist.org
foreach (['notify', 'notify-batch', 'search'] as $key) {
// Just in case packagist.org add full path in future
$path = parse_url($providers->$key){'path'};
$providers->$key = $this->http->getBaseUri().$path;
}
return $providers;
}
/**
* Load provider includes.
*
* @param stdClass $providers
*
* @return array
*/
public function normalize(stdClass $providers):array
{
if (!property_exists($providers, 'provider-includes')) {
throw new Exception('Not found providers information');
}
$providerIncludes = $providers->{'provider-includes'};
$includes = [];
foreach ($providerIncludes as $name => $hash) {
$uri = str_replace('%hash%', $hash->sha256, $name);
$includes[$uri] = $hash->sha256;
}
return $includes;
}
/**
* Download packages.json & provider-xxx$xxx.json.
*
* @param array $providerIncludes Provider Includes
*
* @return Generator Providers downloaded
*/
public function getGenerator(array $providerIncludes):Generator
{
$providerIncludes = array_keys($providerIncludes);
$updated = true;
foreach ($providerIncludes as $uri) {
if ($this->filesystem->has($uri) && !$this->initialized) {
continue;
}
$updated = false;
yield $uri => $this->http->getRequest($uri);
}
return $updated;
}
}