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\controllers; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use hiqdev\assetpackagist\commands\PackageUpdateCommand; |
15
|
|
|
use hiqdev\assetpackagist\exceptions\CorruptedPackageException; |
16
|
|
|
use hiqdev\assetpackagist\exceptions\PackageNotExistsException; |
17
|
|
|
use hiqdev\assetpackagist\exceptions\UpdateRateLimitException; |
18
|
|
|
use hiqdev\assetpackagist\models\AssetPackage; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\filters\VerbFilter; |
21
|
|
|
use yii\web\Controller; |
22
|
|
|
|
23
|
|
|
class PackageController extends Controller |
24
|
|
|
{ |
25
|
|
|
public function behaviors() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
'access' => [ |
29
|
|
|
'class' => VerbFilter::class, |
30
|
|
|
'actions' => [ |
31
|
|
|
'update' => ['post'], |
32
|
|
|
], |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function actionUpdate() |
38
|
|
|
{ |
39
|
|
|
session_write_close(); |
40
|
|
|
$query = Yii::$app->request->post('query'); |
41
|
|
|
$package = $this->getAssetPackage($query); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
if (!$package->canBeUpdated()) { |
45
|
|
|
throw new UpdateRateLimitException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
Yii::createObject(PackageUpdateCommand::class, [$package])->execute(Yii::$app->queue); |
49
|
|
|
} catch (UpdateRateLimitException $exception) { |
50
|
|
|
Yii::$app->session->addFlash('rate-limited', true); |
51
|
|
|
} catch (PackageNotExistsException $e) { |
52
|
|
|
return $this->renderPartial('not-found', ['package' => $package]); |
53
|
|
|
} catch (CorruptedPackageException $e) { |
54
|
|
|
return $this->renderPartial('fetch-error', ['package' => $package, 'exception' => $e]); |
55
|
|
|
} catch (Exception $e) { |
56
|
|
|
Yii::error([ |
57
|
|
|
'UNKNOWN error during ' . $package->getFullName() . ' update', |
58
|
|
|
get_class($e), |
59
|
|
|
$e->getMessage(), |
60
|
|
|
], __METHOD__); |
61
|
|
|
|
62
|
|
|
return $this->renderPartial('fetch-error', ['package' => $package]); |
63
|
|
|
} |
64
|
|
|
$package->load(); |
65
|
|
|
|
66
|
|
|
return $this->renderPartial('versions', ['package' => $package]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function actionSearch($query, $platform = null) |
70
|
|
|
{ |
71
|
|
|
if (preg_match('/^(npm|bower)-asset\/[a-zA-Z0-9.-]+$/', $query)) { |
72
|
|
|
return $this->redirect("/package/$query"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$activeQuery = \hiqdev\hiart\librariesio\models\Project::find()->where([ |
76
|
|
|
'q' => $query, |
77
|
|
|
'platforms' => in_array($platform, ['npm', 'bower'], true) ? $platform : 'bower,npm', |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return $this->render('search', [ |
81
|
|
|
'query' => $query, |
82
|
|
|
'platform' => $platform, |
83
|
|
|
'dataProvider' => new \yii\data\ActiveDataProvider([ |
84
|
|
|
'query' => $activeQuery, |
85
|
|
|
]), |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function actionDetail($fullname) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$package = $this->getAssetPackage($fullname); |
93
|
|
|
$params = [ |
94
|
|
|
'package' => $package, |
95
|
|
|
'forceUpdate' => false, |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
if ($package->canAutoUpdate()) { |
99
|
|
|
$params['forceUpdate'] = true; |
100
|
|
|
} |
101
|
|
|
} catch (Exception $e) { |
102
|
|
|
return $this->render('wrong-name', ['query' => $fullname]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->render('details', $params); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $query |
110
|
|
|
* @return AssetPackage |
111
|
|
|
*/ |
112
|
|
|
private static function getAssetPackage($query) |
113
|
|
|
{ |
114
|
|
|
$package = AssetPackage::fromFullName(trim($query)); |
115
|
|
|
$package->load(); |
116
|
|
|
|
117
|
|
|
return $package; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|