Skip to content

Commit dc8695a

Browse files
committed
Fetch data lazily
This allows phpbrew to instantiate this class and enable/disable an extension without triggering a request to fetch the package data if unnecessary. This way enabling/disabling an extension works offline. Closes #7 (not 100% of this) Closes phpbrew/phpbrew#813 (tested locally)
1 parent 2d361b3 commit dc8695a

File tree

1 file changed

+82
-28
lines changed

1 file changed

+82
-28
lines changed

src/PEARX/Channel.php

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<?php
2+
23
namespace PEARX;
3-
use PEARX\ChannelParser;
4+
45
use DOMDocument;
56
use Exception;
6-
use PEARX\Core;
77

88
/**
9+
* ````php
910
* $channel = new PEARX\Channel( 'pear.php.net', array(
1011
* 'cache' => ....
1112
* ));
1213
*
1314
* $channel->getPackages();
14-
*
15+
* ```
1516
*/
1617
class Channel
1718
{
@@ -21,11 +22,10 @@ class Channel
2122
public $name;
2223

2324
/**
24-
* @var string suggestedalias
25+
* @var string suggested alias
2526
*/
2627
public $alias;
2728

28-
2929
/**
3030
* @var string summary
3131
*/
@@ -36,13 +36,11 @@ class Channel
3636
*/
3737
public $primary = array();
3838

39-
4039
/**
4140
* @var string REST version
4241
*/
4342
public $rest; // Latest REST version
4443

45-
4644
public $cache;
4745

4846
public $downloader;
@@ -58,43 +56,61 @@ class Channel
5856

5957
public $core;
6058

59+
private $host;
60+
private $init = false;
61+
62+
/**
63+
* @param string $host
64+
* @param array $options
65+
*/
6166
public function __construct($host, $options = array() )
6267
{
6368
$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;
7470
}
7571

72+
/**
73+
* @return string
74+
*/
7675
public function getBaseUrl()
7776
{
77+
$this->init();
78+
7879
return $this->scheme . '://' . $this->name;
7980
}
8081

82+
/**
83+
* @param string $version
84+
*
85+
* @return string
86+
*/
8187
public function getRestBaseUrl($version = null)
8288
{
83-
if( $version && $this->primary[$version] )
89+
$this->init();
90+
91+
if( $version && $this->primary[$version] ) {
8492
return rtrim($this->primary[ $version ],'/');
93+
}
94+
8595
return rtrim($this->primary[ $this->rest ],'/');
8696
}
8797

8898

99+
/**
100+
* @param string $packageName
101+
* @param string $version
102+
*
103+
* @return DOMDocument
104+
*/
89105
public function fetchPackageReleaseXml($packageName, $version = 'stable')
90106
{
91107
$baseUrl = $this->getRestBaseUrl();
92108
$url = "$baseUrl/r/" . strtolower($packageName);
93109

94-
if( $version === 'stable'
110+
if ( $version === 'stable'
95111
|| $version === 'latest'
96-
|| $version === 'beta' )
97-
{
112+
|| $version === 'beta'
113+
) {
98114
// Get version info
99115
$ret = file_get_contents($url . '/' . $version . '.txt');
100116
if ($ret === false) {
@@ -116,15 +132,19 @@ public function fetchPackageReleaseXml($packageName, $version = 'stable')
116132

117133
/**
118134
* fetch channel.xml from PEAR channel server.
135+
*
136+
* @param string $host
137+
*
138+
* @return string
119139
*/
120140
public function fetchChannelXml($host)
121141
{
122-
$xmlstr = null;
123142
$xmlstr = $this->core->cache ? $this->core->cache->get( $host ) : null;
124143

125-
// cache not found.
126-
if( null !== $xmlstr )
144+
// Check the cache
145+
if( null !== $xmlstr ) {
127146
return $xmlstr;
147+
}
128148

129149
$httpUrl = 'http://' . $host . '/channel.xml';
130150
$httpsUrl = 'https://' . $host . '/channel.xml';
@@ -148,19 +168,22 @@ public function fetchChannelXml($host)
148168
throw new Exception('channel.xml fetch failed.');
149169
}
150170

151-
// save cache
171+
// Cache result
152172
if( $this->cache ) {
153173
$this->cache->set($host, $xmlstr );
154174
}
155175
return $xmlstr;
156176
}
157177

178+
/**
179+
* @return Category[]
180+
*/
158181
public function getCategories()
159182
{
160183
$baseUrl = $this->getRestBaseUrl();
161184
$url = $baseUrl . '/c/categories.xml';
162185
$xmlStr = $this->core->request($url);
163-
186+
164187
// libxml_use_internal_errors(true);
165188
$xml = Utils::create_dom();
166189
if( false === $xml->loadXml( $xmlStr ) ) {
@@ -195,13 +218,44 @@ public function getPackages()
195218
}
196219

197220

221+
/**
222+
* @param string
223+
*
224+
* @return Package|null
225+
*/
198226
public function findPackage($name)
199227
{
200228
foreach( $this->getCategories() as $category ) {
201229
$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;
204245
}
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;
205259
}
206260
}
207261

0 commit comments

Comments
 (0)