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\librariesio\ProjectDataProvider; |
19
|
|
|
use hiqdev\assetpackagist\models\AssetPackage; |
20
|
|
|
use Yii; |
21
|
|
|
use yii\filters\VerbFilter; |
22
|
|
|
use yii\web\Controller; |
23
|
|
|
|
24
|
|
|
class PackageController extends Controller |
25
|
|
|
{ |
26
|
|
|
public function behaviors() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'access' => [ |
30
|
|
|
'class' => VerbFilter::class, |
31
|
|
|
'actions' => [ |
32
|
|
|
'update' => ['post'], |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function actionUpdate() |
39
|
|
|
{ |
40
|
|
|
session_write_close(); |
41
|
|
|
$query = Yii::$app->request->post('query'); |
42
|
|
|
$package = $this->getAssetPackage($query); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
if (!$package->canBeUpdated()) { |
46
|
|
|
throw new UpdateRateLimitException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
Yii::createObject(PackageUpdateCommand::class, [$package])->execute(Yii::$app->queue); |
50
|
|
|
} catch (UpdateRateLimitException $exception) { |
51
|
|
|
Yii::$app->session->addFlash('rate-limited', true); |
52
|
|
|
} catch (PackageNotExistsException $e) { |
53
|
|
|
return $this->renderPartial('not-found', ['package' => $package]); |
54
|
|
|
} catch (CorruptedPackageException $e) { |
55
|
|
|
return $this->renderPartial('fetch-error', ['package' => $package, 'exception' => $e]); |
56
|
|
|
} catch (Exception $e) { |
57
|
|
|
Yii::error([ |
58
|
|
|
'UNKNOWN error during ' . $package->getFullName() . ' update', |
59
|
|
|
get_class($e), |
60
|
|
|
$e->getMessage(), |
61
|
|
|
], __METHOD__); |
62
|
|
|
|
63
|
|
|
return $this->renderPartial('fetch-error', ['package' => $package]); |
64
|
|
|
} |
65
|
|
|
$package->load(); |
66
|
|
|
|
67
|
|
|
return $this->renderPartial('versions', ['package' => $package]); |
68
|
|
|
} |
69
|
|
|
|
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) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
$package = $this->getAssetPackage($fullname); |
89
|
|
|
$params = [ |
90
|
|
|
'package' => $package, |
91
|
|
|
'forceUpdate' => false, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if ($package->canAutoUpdate()) { |
95
|
|
|
$params['forceUpdate'] = true; |
96
|
|
|
} |
97
|
|
|
} catch (Exception $e) { |
98
|
|
|
return $this->render('wrong-name', compact('query')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->render('details', $params); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $query |
106
|
|
|
* @return AssetPackage |
107
|
|
|
*/ |
108
|
|
|
private static function getAssetPackage($query) |
109
|
|
|
{ |
110
|
|
|
$filtredQuery = trim($query); |
111
|
|
|
$package = AssetPackage::fromFullName($filtredQuery); |
112
|
|
|
$package->load(); |
113
|
|
|
|
114
|
|
|
return $package; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|