Skip to content

Commit 3511c98

Browse files
committed
Improved Search using libraries.io (Close #36)
1 parent c314be6 commit 3511c98

12 files changed

+664
-158
lines changed

Diff for: src/config/common.php

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
'registryFactory' => [
4343
'class' => hiqdev\assetpackagist\registry\RegistryFactory::class,
4444
],
45+
'librariesio' => [
46+
'class' => \hiqdev\assetpackagist\librariesio\LibrariesioRepository::class,
47+
'apiKey' => $params['librariesio.api_key'],
48+
],
4549
],
4650
'container' => [
4751
'singletons' => [

Diff for: src/config/params.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010

1111
return [
12-
'favicon.ico' => '@hiqdev/assetpackagist/assets/images/favicon.ico',
12+
'favicon.ico' => '@hiqdev/assetpackagist/assets/images/favicon.ico',
1313

14-
'db.name' => 'asset_packagist',
15-
'db.username' => 'asset-packagist',
16-
'db.password' => '',
14+
'db.name' => 'asset_packagist',
15+
'db.username' => 'asset-packagist',
16+
'db.password' => '',
17+
18+
'librariesio.api_key' => '',
1719
];

Diff for: src/config/web.php

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
return [
1212
'controllerNamespace' => 'hiqdev\assetpackagist\controllers',
1313
'components' => [
14+
'urlManager' => [
15+
'rules' => [
16+
'/package/<fullname:\w+\-asset\/[\w-]+>' => '/package/detail',
17+
],
18+
],
1419
'themeManager' => [
1520
'pathMap' => [
1621
'$themedViewPaths' => ['@hiqdev/assetpackagist/views'],

Diff for: src/controllers/PackageController.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use hiqdev\assetpackagist\exceptions\CorruptedPackageException;
1616
use hiqdev\assetpackagist\exceptions\PackageNotExistsException;
1717
use hiqdev\assetpackagist\exceptions\UpdateRateLimitException;
18+
use hiqdev\assetpackagist\librariesio\ProjectDataProvider;
1819
use hiqdev\assetpackagist\models\AssetPackage;
1920
use Yii;
2021
use yii\filters\VerbFilter;
@@ -63,14 +64,32 @@ public function actionUpdate()
6364
}
6465
$package->load();
6566

66-
return $this->renderPartial('details', ['package' => $package]);
67+
return $this->renderPartial('versions', ['package' => $package]);
6768
}
6869

69-
public function actionSearch($query)
70+
public function actionSearch($query, $platform = null)
71+
{
72+
$dataProvider = new ProjectDataProvider();
73+
$dataProvider->query = $query;
74+
$dataProvider->platform = $platform;
75+
76+
$params = [
77+
'query' => $query,
78+
'platform' => $platform,
79+
'dataProvider' => $dataProvider,
80+
];
81+
82+
return $this->render('search', $params);
83+
}
84+
85+
public function actionDetail($fullname)
7086
{
7187
try {
72-
$package = $this->getAssetPackage($query);
73-
$params = ['package' => $package, 'query' => $query, 'forceUpdate' => false];
88+
$package = $this->getAssetPackage($fullname);
89+
$params = [
90+
'package' => $package,
91+
'forceUpdate' => false,
92+
];
7493

7594
if ($package->canAutoUpdate()) {
7695
$params['forceUpdate'] = true;
@@ -79,7 +98,7 @@ public function actionSearch($query)
7998
return $this->render('wrong-name', compact('query'));
8099
}
81100

82-
return $this->render('search', $params);
101+
return $this->render('details', $params);
83102
}
84103

85104
/**

Diff for: src/librariesio/LibrariesioRepository.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Asset Packagist.
4+
*
5+
* @link https://github.com/hiqdev/asset-packagist
6+
* @package asset-packagist
7+
* @license BSD-3-Clause
8+
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9+
*/
10+
11+
namespace hiqdev\assetpackagist\librariesio;
12+
13+
use GuzzleHttp\Client;
14+
use yii\base\Component;
15+
16+
class LibrariesioRepository extends Component
17+
{
18+
/**
19+
* @var string Base URI for https://libraries.io
20+
*/
21+
public $baseUri = 'https://libraries.io/api/';
22+
23+
/**
24+
* Without api key, has 30/request/minute hate limit
25+
* With api key, has 60/request/minute hate limit.
26+
* @var string The user API Key
27+
*/
28+
public $apiKey;
29+
30+
/**
31+
* Options for Guzzle client
32+
* Example:
33+
* [
34+
* 'timeout' => 0,
35+
* 'allow_redirects' => false,
36+
* 'proxy' => '192.168.16.1:10'
37+
* ].
38+
*
39+
* @var array
40+
*/
41+
public $clientOptions = [];
42+
43+
/**
44+
* The Guzzle client.
45+
* @var Client
46+
*/
47+
protected $client;
48+
49+
public function init()
50+
{
51+
parent::init();
52+
53+
$this->clientOptions['base_uri'] = $this->baseUri;
54+
55+
$this->client = new Client($this->clientOptions);
56+
}
57+
58+
/**
59+
* Send request to server with api_key and return a Response.
60+
* @param string $method
61+
* @param string $uri
62+
* @param array $options
63+
* @throws \GuzzleHttp\Exception\BadResponseException
64+
* @return \GuzzleHttp\Psr7\Response
65+
*/
66+
public function request($method, $uri = '', array $options = [])
67+
{
68+
if (!isset($options['query'])) {
69+
$options['query'] = [];
70+
}
71+
if ($this->apiKey && !isset($options['query']['api_key'])) {
72+
$options['query']['api_key'] = $this->apiKey;
73+
}
74+
75+
try {
76+
return $this->client->request($method, $uri, $options);
77+
} catch (\GuzzleHttp\Exception\BadResponseException $ex) {
78+
if ($ex->hasResponse()) {
79+
return $ex->getResponse();
80+
}
81+
throw $ex;
82+
}
83+
}
84+
85+
/**
86+
* Search package in https://libraries.io.
87+
* @see https://libraries.io/api/#project-search
88+
* @param array $query
89+
* @return \GuzzleHttp\Psr7\Response
90+
*/
91+
public function search($query = [])
92+
{
93+
return $this->request('GET', 'search', ['query' => $query]);
94+
}
95+
96+
/**
97+
* Return the package info from https://libraries.io.
98+
* @see https://libraries.io/api/#project
99+
* @param string $platform
100+
* @param string $name
101+
* @return \GuzzleHttp\Psr7\Response
102+
*/
103+
public function getProject($platform, $name)
104+
{
105+
return $this->request('GET', $platform . '/' . $name);
106+
}
107+
}

Diff for: src/librariesio/Project.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Asset Packagist.
4+
*
5+
* @link https://github.com/hiqdev/asset-packagist
6+
* @package asset-packagist
7+
* @license BSD-3-Clause
8+
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9+
*/
10+
11+
namespace hiqdev\assetpackagist\librariesio;
12+
13+
use hiqdev\assetpackagist\models\AssetPackage;
14+
use hiqdev\assetpackagist\repositories\PackageRepository;
15+
use Yii;
16+
use yii\base\Model;
17+
18+
/**
19+
* @property string $fullName Full name of package
20+
*/
21+
class Project extends Model
22+
{
23+
public $name;
24+
public $platform;
25+
public $description;
26+
public $homepage;
27+
public $repository_url;
28+
public $normalized_licenses;
29+
public $rank;
30+
public $latest_release_published_at;
31+
public $latest_release_number;
32+
public $language;
33+
public $status;
34+
public $package_manager_url;
35+
public $stars;
36+
public $forks;
37+
public $keywords;
38+
39+
public function rules()
40+
{
41+
return [
42+
['name', 'string'],
43+
['platform', 'string'],
44+
['description', 'string'],
45+
['homepage', 'url'],
46+
['repository_url', 'url'],
47+
['normalized_licenses', 'each', 'rule' => ['string']],
48+
['rank', 'integer'],
49+
['latest_release_published_at', 'datetime', 'timestampAttribute' => 'latest_release_published_at'],
50+
['latest_release_number', 'string'],
51+
['language', 'string'],
52+
['status', 'string'],
53+
['package_manager_url', 'url'],
54+
['stars', 'integer'],
55+
['forks', 'integer'],
56+
['keywords', 'each', 'rule' => ['string']],
57+
];
58+
}
59+
60+
public function getFullName()
61+
{
62+
$fullname = AssetPackage::buildFullName($this->platform, $this->name);
63+
return AssetPackage::normalizeName($fullname);
64+
}
65+
66+
/**
67+
* Check the package exists in Asset-Packagist.
68+
* @return boolean
69+
*/
70+
public function isAvailable()
71+
{
72+
$package = AssetPackage::fromFullName($this->getFullName());
73+
74+
$repository = Yii::createObject(PackageRepository::class, []);
75+
76+
return $repository->exists($package);
77+
}
78+
}

0 commit comments

Comments
 (0)