Skip to content

Commit 5fad211

Browse files
committed
Implemented package details page
1 parent 2195919 commit 5fad211

File tree

12 files changed

+261
-63
lines changed

12 files changed

+261
-63
lines changed

Diff for: src/assets/AppAsset.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class AppAsset extends AssetBundle
1818
public $sourcePath = '@hiqdev/assetpackagist/assets';
1919
public $css = [
2020
'css/site.css',
21+
'//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css',
2122
];
2223
public $js = [
2324
];
2425
public $depends = [
2526
'yii\web\YiiAsset',
2627
'yii\bootstrap\BootstrapAsset',
28+
'yii\web\JqueryAsset',
2729
];
2830
}

Diff for: src/models/Storage.php renamed to src/components/Storage.php

+18-19
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,24 @@
99
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
1010
*/
1111

12-
namespace hiqdev\assetpackagist\models;
12+
namespace hiqdev\assetpackagist\components;
1313

14-
use hiqdev\assetpackagist\helpers\Locker;
14+
use hiqdev\assetpackagist\models\AssetPackage;
1515
use Yii;
16+
use yii\base\Component;
1617
use yii\helpers\Json;
18+
use hiqdev\assetpackagist\helpers\Locker;
1719

18-
class Storage
20+
class Storage extends Component
1921
{
20-
protected static $_instance;
21-
2222
protected $_path;
2323
protected $_locker;
2424

25-
protected function __construct()
25+
public function init()
2626
{
2727
$this->_path = Yii::getAlias('@storage', false);
2828
}
2929

30-
/**
31-
* @return static
32-
*/
33-
public static function getInstance()
34-
{
35-
if (static::$_instance === null) {
36-
static::$_instance = new static;
37-
}
38-
39-
return static::$_instance;
40-
}
41-
4230
protected function getLocker()
4331
{
4432
if ($this->_locker === null) {
@@ -102,6 +90,16 @@ public function writePackage(AssetPackage $package)
10290
return $hash;
10391
}
10492

93+
/**
94+
* Reads the $package information from the storage
95+
*
96+
* @param AssetPackage $package
97+
* @return array|null array of two elements:
98+
* 0 - string sha256 hash of the package
99+
* 1 - array[] releases
100+
*
101+
* Returns null, when package does not exist.
102+
*/
105103
public function readPackage(AssetPackage $package)
106104
{
107105
$name = $package->getFullName();
@@ -110,11 +108,12 @@ public function readPackage(AssetPackage $package)
110108
return [];
111109
}
112110
$json = file_get_contents($path);
111+
$updateTime = filemtime($path);
113112
$hash = hash('sha256', $json);
114113
$data = Json::decode($json);
115114
$releases = isset($data['packages'][$name]) ? $data['packages'][$name] : [];
116115

117-
return compact('hash', 'releases');
116+
return compact('hash', 'releases', 'updateTime');
118117
}
119118

120119
public function buildPath()

Diff for: src/config/hisite.php

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
'enablePrettyUrl' => true,
3535
'showScriptName' => false,
3636
],
37+
'packageStorage' => [
38+
'class' => \hiqdev\assetpackagist\components\Storage::class
39+
]
3740
],
3841
'modules' => [],
3942
'aliases' => require __DIR__ . '/aliases.php',

Diff for: src/console/AssetPackageController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace hiqdev\assetpackagist\console;
1313

14-
use hiqdev\assetpackagist\models\AssetPackage;
15-
use hiqdev\assetpackagist\models\Storage;
14+
use hiqdev\assetpackagist\components\AssetPackage;
15+
use hiqdev\assetpackagist\components\Storage;
1616
use Yii;
1717
use yii\base\Exception;
1818
use yii\helpers\Console;

Diff for: src/console/BowerPackageController.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace hiqdev\assetpackagist\console;
44

5+
use Fxp\Composer\AssetPlugin\Repository\BowerRepository;
56
use Yii;
67
use yii\helpers\ArrayHelper;
78
use yii\helpers\Console;

Diff for: src/controllers/SiteController.php

+43-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@
1313

1414
use hiqdev\assetpackagist\models\AssetPackage;
1515
use Yii;
16-
use yii\web\ServerErrorHttpException;
16+
use yii\filters\VerbFilter;
1717

1818
class SiteController extends \yii\web\Controller
1919
{
20+
public function behaviors()
21+
{
22+
return [
23+
'access' => [
24+
'class' => VerbFilter::class,
25+
'actions' => [
26+
'update' => ['post']
27+
]
28+
]
29+
];
30+
}
31+
2032
public function actionIndex()
2133
{
2234
return $this->render('index');
@@ -32,20 +44,42 @@ public function actionContact()
3244
return $this->render('contact');
3345
}
3446

35-
public function actionSearch()
47+
public function actionSearch($query)
3648
{
37-
$query = Yii::$app->request->get('query') ?: Yii::$app->request->post('query');
49+
$package = $this->getAssetPackage($query);
50+
$params = ['package' => $package, 'query' => $query, 'forceUpdate' => false];
3851

52+
if ($package->canAutoUpdate()) {
53+
$params['forceUpdate'] = true;
54+
}
55+
56+
return $this->render('search', $params);
57+
}
58+
59+
/**
60+
* @param $query
61+
* @return AssetPackage
62+
*/
63+
private static function getAssetPackage($query)
64+
{
3965
list($type, $name) = AssetPackage::splitFullName($query);
66+
$package = new AssetPackage($type, $name);
67+
$package->load();
68+
return $package;
69+
}
70+
71+
public function actionUpdate()
72+
{
73+
$query = Yii::$app->request->post('query');
4074

41-
try {
42-
$package = new AssetPackage($type, $name);
75+
$package = $this->getAssetPackage($query);
76+
if ($package->canBeUpdated()) {
4377
$package->update();
44-
$package->load();
45-
} catch (\Exception $e) {
46-
throw new ServerErrorHttpException($e->getMessage());
78+
} else {
79+
Yii::$app->session->addFlash('update-impossible', true);
4780
}
81+
$package->load();
4882

49-
return $this->render('search', ['package' => $package, 'query' => $query]);
83+
return $this->renderPartial('package-details', ['package' => $package]);
5084
}
5185
}

Diff for: src/models/AssetPackage.php

+62-15
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,29 @@
1616
use Composer\IO\NullIO;
1717
use Exception;
1818
use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
19+
use hiqdev\assetpackagist\components\Storage;
20+
use hiqdev\assetpackagist\registry\BowerRegistry;
21+
use hiqdev\assetpackagist\registry\NpmRegistry;
1922
use hiqdev\assetpackagist\registry\RegistryFactory;
2023
use Yii;
24+
use yii\base\Object;
2125

22-
class AssetPackage
26+
class AssetPackage extends Object
2327
{
2428
protected $_type;
2529
protected $_name;
2630
protected $_hash;
2731
protected $_releases = [];
2832
protected $_saved;
33+
/**
34+
* @var AssetVcsRepository|BowerRegistry|NpmRegistry
35+
*/
36+
protected $_registry;
37+
38+
/**
39+
* @var integer UNIX Epoch timestamp of the latest package update
40+
*/
41+
protected $_updateTime;
2942

3043
/**
3144
* @var NullIO
@@ -35,17 +48,15 @@ class AssetPackage
3548
* @var Composer
3649
*/
3750
protected $_composer;
38-
/**
39-
* @var Storage
40-
*/
41-
protected $_storage;
4251
/**
4352
* @var Composer
4453
*/
4554
protected static $_commonComposer;
4655

47-
public function __construct($type, $name)
56+
public function __construct($type, $name, $config = [])
4857
{
58+
parent::__construct($config);
59+
4960
if (!$this->checkType($type)) {
5061
throw new Exception('wrong type');
5162
}
@@ -56,6 +67,14 @@ public function __construct($type, $name)
5667
$this->_name = $name;
5768
}
5869

70+
public function getRegistry() {
71+
if ($this->_registry === null) {
72+
$this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager());
73+
}
74+
75+
return $this->_registry;
76+
}
77+
5978
public function checkType($type)
6079
{
6180
return $type === 'bower' || $type === 'npm';
@@ -79,7 +98,7 @@ public static function buildFullName($type, $name)
7998
public static function splitFullName($full)
8099
{
81100
list($temp, $name) = explode('/', $full);
82-
list($type, $temp) = explode('-', $temp);
101+
list($type,) = explode('-', $temp);
83102

84103
return [$type, $name];
85104
}
@@ -160,14 +179,16 @@ public static function findOne($type, $name)
160179
public function load()
161180
{
162181
$data = $this->getStorage()->readPackage($this);
163-
$this->_hash = $data['hash'];
164-
$this->_releases = $data['releases'];
182+
if ($data !== null) {
183+
$this->_hash = $data['hash'];
184+
$this->_releases = $data['releases'];
185+
$this->_updateTime = $data['updateTime'];
186+
}
165187
}
166188

167189
public function update()
168190
{
169-
$registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager());
170-
$repo = $registry->buildVcsRepository($this->getName());
191+
$repo = $this->getRegistry()->buildVcsRepository($this->getName());
171192
$this->_releases = $this->prepareReleases($repo);
172193
$this->_hash = $this->getStorage()->writePackage($this);
173194
}
@@ -234,12 +255,38 @@ public function getSaved()
234255
return $this->_saved;
235256
}
236257

258+
/**
259+
* @return Storage
260+
*/
237261
public function getStorage()
238262
{
239-
if ($this->_storage === null) {
240-
$this->_storage = Storage::getInstance();
241-
}
263+
return Yii::$app->get('packageStorage');
264+
}
265+
266+
/**
267+
* Returns the latest update time (UNIX Epoch)
268+
* @return int|null
269+
*/
270+
public function getUpdateTime()
271+
{
272+
return $this->_updateTime;
273+
}
242274

243-
return $this->_storage;
275+
/**
276+
* Package can be updated not more often than once in 10 min
277+
* @return bool
278+
*/
279+
public function canBeUpdated()
280+
{
281+
return time() - $this->getUpdateTime() > 60*10; // 10 min
282+
}
283+
284+
/**
285+
* Whether tha package should be auth-updated (if it is older than 1 day)
286+
* @return bool
287+
*/
288+
public function canAutoUpdate()
289+
{
290+
return time() - $this->getUpdateTime() > 60*60*24; // 1 day
244291
}
245292
}

Diff for: src/registry/BowerRegistry.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
class BowerRegistry extends \Fxp\Composer\AssetPlugin\Repository\BowerRepository
1515
{
1616
use RegistryTrait;
17+
18+
public $siteUrl = 'http://bower.io';
1719
}

Diff for: src/registry/RegistryTrait.php

+9
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ public function fetchPackageData($name)
2828
return $this->fetchFile($packageUrl, $cacheName);
2929
}
3030

31+
/**
32+
* @param $name
33+
* @return \Composer\Repository\RepositoryInterface
34+
*/
3135
public function buildVcsRepository($name)
3236
{
3337
$data = $this->fetchPackageData($name);
3438
$conf = $this->createVcsRepositoryConfig($data, $name);
3539

3640
return $this->rm->createRepository($conf['type'], $conf);
3741
}
42+
43+
public function getPackageSearchUrl($name)
44+
{
45+
return $this->siteUrl . '/search/?q=' . $name;
46+
}
3847
}

0 commit comments

Comments
 (0)