1
1
<?php
2
+
2
3
namespace PEARX ;
3
- use PEARX \ ChannelParser ;
4
+
4
5
use DOMDocument ;
5
6
use Exception ;
6
- use PEARX \Core ;
7
7
8
8
/**
9
+ * ````php
9
10
* $channel = new PEARX\Channel( 'pear.php.net', array(
10
11
* 'cache' => ....
11
12
* ));
12
13
*
13
14
* $channel->getPackages();
14
- *
15
+ * ```
15
16
*/
16
17
class Channel
17
18
{
@@ -21,11 +22,10 @@ class Channel
21
22
public $ name ;
22
23
23
24
/**
24
- * @var string suggestedalias
25
+ * @var string suggested alias
25
26
*/
26
27
public $ alias ;
27
28
28
-
29
29
/**
30
30
* @var string summary
31
31
*/
@@ -36,13 +36,11 @@ class Channel
36
36
*/
37
37
public $ primary = array ();
38
38
39
-
40
39
/**
41
40
* @var string REST version
42
41
*/
43
42
public $ rest ; // Latest REST version
44
43
45
-
46
44
public $ cache ;
47
45
48
46
public $ downloader ;
@@ -58,43 +56,61 @@ class Channel
58
56
59
57
public $ core ;
60
58
59
+ private $ host ;
60
+ private $ init = false ;
61
+
62
+ /**
63
+ * @param string $host
64
+ * @param array $options
65
+ */
61
66
public function __construct ($ host , $ options = array () )
62
67
{
63
68
$ this ->core = new Core ( $ options );
64
- $ this ->channelXml = $ this ->fetchChannelXml ( $ host );
65
-
66
- $ parser = new ChannelParser ;
67
- $ info = $ parser ->parse ( $ this ->channelXml );
68
-
69
- $ this ->name = $ info ->name ;
70
- $ this ->summary = $ info ->summary ;
71
- $ this ->alias = $ info ->alias ;
72
- $ this ->primary = $ info ->primary ;
73
- $ this ->rest = $ info ->rest ;
69
+ $ this ->host = $ host ;
74
70
}
75
71
72
+ /**
73
+ * @return string
74
+ */
76
75
public function getBaseUrl ()
77
76
{
77
+ $ this ->init ();
78
+
78
79
return $ this ->scheme . ':// ' . $ this ->name ;
79
80
}
80
81
82
+ /**
83
+ * @param string $version
84
+ *
85
+ * @return string
86
+ */
81
87
public function getRestBaseUrl ($ version = null )
82
88
{
83
- if ( $ version && $ this ->primary [$ version ] )
89
+ $ this ->init ();
90
+
91
+ if ( $ version && $ this ->primary [$ version ] ) {
84
92
return rtrim ($ this ->primary [ $ version ],'/ ' );
93
+ }
94
+
85
95
return rtrim ($ this ->primary [ $ this ->rest ],'/ ' );
86
96
}
87
97
88
98
99
+ /**
100
+ * @param string $packageName
101
+ * @param string $version
102
+ *
103
+ * @return DOMDocument
104
+ */
89
105
public function fetchPackageReleaseXml ($ packageName , $ version = 'stable ' )
90
106
{
91
107
$ baseUrl = $ this ->getRestBaseUrl ();
92
108
$ url = "$ baseUrl/r/ " . strtolower ($ packageName );
93
109
94
- if ( $ version === 'stable '
110
+ if ( $ version === 'stable '
95
111
|| $ version === 'latest '
96
- || $ version === 'beta ' )
97
- {
112
+ || $ version === 'beta '
113
+ ) {
98
114
// Get version info
99
115
$ ret = file_get_contents ($ url . '/ ' . $ version . '.txt ' );
100
116
if ($ ret === false ) {
@@ -116,15 +132,19 @@ public function fetchPackageReleaseXml($packageName, $version = 'stable')
116
132
117
133
/**
118
134
* fetch channel.xml from PEAR channel server.
135
+ *
136
+ * @param string $host
137
+ *
138
+ * @return string
119
139
*/
120
140
public function fetchChannelXml ($ host )
121
141
{
122
- $ xmlstr = null ;
123
142
$ xmlstr = $ this ->core ->cache ? $ this ->core ->cache ->get ( $ host ) : null ;
124
143
125
- // cache not found.
126
- if ( null !== $ xmlstr )
144
+ // Check the cache
145
+ if ( null !== $ xmlstr ) {
127
146
return $ xmlstr ;
147
+ }
128
148
129
149
$ httpUrl = 'http:// ' . $ host . '/channel.xml ' ;
130
150
$ httpsUrl = 'https:// ' . $ host . '/channel.xml ' ;
@@ -148,19 +168,22 @@ public function fetchChannelXml($host)
148
168
throw new Exception ('channel.xml fetch failed. ' );
149
169
}
150
170
151
- // save cache
171
+ // Cache result
152
172
if ( $ this ->cache ) {
153
173
$ this ->cache ->set ($ host , $ xmlstr );
154
174
}
155
175
return $ xmlstr ;
156
176
}
157
177
178
+ /**
179
+ * @return Category[]
180
+ */
158
181
public function getCategories ()
159
182
{
160
183
$ baseUrl = $ this ->getRestBaseUrl ();
161
184
$ url = $ baseUrl . '/c/categories.xml ' ;
162
185
$ xmlStr = $ this ->core ->request ($ url );
163
-
186
+
164
187
// libxml_use_internal_errors(true);
165
188
$ xml = Utils::create_dom ();
166
189
if ( false === $ xml ->loadXml ( $ xmlStr ) ) {
@@ -195,13 +218,44 @@ public function getPackages()
195
218
}
196
219
197
220
221
+ /**
222
+ * @param string
223
+ *
224
+ * @return Package|null
225
+ */
198
226
public function findPackage ($ name )
199
227
{
200
228
foreach ( $ this ->getCategories () as $ category ) {
201
229
$ packages = $ category ->getPackages ();
202
- if ( isset ($ packages [$ name ]) )
203
- return $ packages [ $ name ];
230
+ if ( isset ($ packages [$ name ]) ) {
231
+ return $ packages [$ name ];
232
+ }
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Initialise the properties necessary to do an HTTP request. This is done lazily as opposed as during
238
+ * instantiation to avoid an HTTP request if unnecessary which would otherwise fail if no internet
239
+ * connection is available.
240
+ */
241
+ private function init ()
242
+ {
243
+ if ($ this ->init ) {
244
+ return ;
204
245
}
246
+
247
+ $ this ->channelXml = $ this ->fetchChannelXml ( $ this ->host );
248
+
249
+ $ parser = new ChannelParser ;
250
+ $ info = $ parser ->parse ( $ this ->channelXml );
251
+
252
+ $ this ->name = $ info ->name ;
253
+ $ this ->summary = $ info ->summary ;
254
+ $ this ->alias = $ info ->alias ;
255
+ $ this ->primary = $ info ->primary ;
256
+ $ this ->rest = $ info ->rest ;
257
+
258
+ $ this ->init = true ;
205
259
}
206
260
}
207
261
0 commit comments