Skip to content

Commit 45af437

Browse files
committed
Add fetchPackageReleaseXml method to get package releasei nfo
1 parent cf55bf8 commit 45af437

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

README.md

+33-26
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,40 @@ Features:
2525
Channel operations:
2626

2727
```php
28-
<?php
29-
use CacheKit\FileSystemCache;
30-
31-
$channel = new PEARX\Channel($host);
32-
$categories = $channel->getCategories();
33-
34-
foreach( $categories as $category ) {
35-
// $category->name
36-
// $category->infoUrl
37-
38-
$packages = $category->getPackages();
39-
foreach( $packages as $package ) {
40-
$package->name;
41-
$package->summary;
42-
$package->desc;
43-
$package->channel;
44-
$package->license;
45-
$package->deps;
46-
$package->releases;
47-
48-
$package->stable; // version string
49-
$package->alpha; // version string
50-
$package->latest; // version string
51-
52-
$stability = $package->getRelease('0.0.1');
53-
}
28+
use CacheKit\FileSystemCache;
29+
30+
$channel = new PEARX\Channel($host);
31+
32+
33+
// find package from the remote pear host
34+
$package = $channel->findPackage('PEAR');
35+
36+
37+
38+
// traverse pear channel categories
39+
$categories = $channel->getCategories();
40+
41+
foreach( $categories as $category ) {
42+
// $category->name
43+
// $category->infoUrl
44+
45+
$packages = $category->getPackages();
46+
foreach( $packages as $package ) {
47+
$package->name;
48+
$package->summary;
49+
$package->desc;
50+
$package->channel;
51+
$package->license;
52+
$package->deps;
53+
$package->releases;
54+
55+
$package->stable; // version string
56+
$package->alpha; // version string
57+
$package->latest; // version string
58+
59+
$stability = $package->getRelease('0.0.1');
5460
}
61+
}
5562
```
5663

5764

src/PEARX/Channel.php

+29
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ public function getRestBaseUrl($version = null)
8585
return rtrim($this->primary[ $this->rest ],'/');
8686
}
8787

88+
public function fetchPackageReleaseXml($packageName, $version = 'stable')
89+
{
90+
$baseUrl = $this->getRestBaseUrl();
91+
$url = "$baseUrl/r/" . strtolower($packageName);
92+
93+
if( $version === 'stable'
94+
|| $version === 'latest'
95+
|| $version === 'beta' )
96+
{
97+
// get version info
98+
$version = file_get_contents($url . '/' . $version . '.txt' );
99+
}
100+
$url = $url . '/' . $version . '.xml';
101+
$xmlStr = $this->core->request($url);
102+
103+
// libxml_use_internal_errors(true);
104+
$xml = Utils::create_dom();
105+
if( false === $xml->loadXml( $xmlStr ) ) {
106+
throw new Exception("Error in XMl document: $url");
107+
}
108+
return $xml;
109+
}
88110

89111
/**
90112
* fetch channel.xml from PEAR channel server.
@@ -151,6 +173,12 @@ public function getCategories()
151173
return $list;
152174
}
153175

176+
177+
/**
178+
* Get all packages from this channel.
179+
*
180+
* @return Package[]
181+
*/
154182
public function getPackages()
155183
{
156184
$packages = array();
@@ -160,6 +188,7 @@ public function getPackages()
160188
return $packages;
161189
}
162190

191+
163192
public function findPackage($name)
164193
{
165194
foreach( $this->getCategories() as $category ) {

tests/PEARX/ChannelTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
class ChannelTest extends PHPUnit_Framework_TestCase
44
{
55

6+
public function testPackageReleaseInfoFinderWithLatest()
7+
{
8+
$channel = new PEARX\Channel('pecl.php.net');
9+
ok($channel);
10+
11+
$xml = $channel->fetchPackageReleaseXml('apc','latest');
12+
ok($xml);
13+
}
614

7-
public function testPackageFindPear()
15+
public function testPackageFindPearWithFileSystemCache()
816
{
917
$cache = new CacheKit\FileSystemCache(array(
1018
'expiry' => 10,
@@ -27,7 +35,7 @@ public function testPackageFindPear()
2735
ok( $package->license );
2836
}
2937

30-
public function testPackageFind()
38+
public function testPackageFindWithFileSystemCache()
3139
{
3240
$cache = new CacheKit\FileSystemCache(array(
3341
'expiry' => 10,

0 commit comments

Comments
 (0)