1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Asset Packagist |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/asset-packagist |
7
|
|
|
* @package asset-packagist |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\assetpackagist\controllers; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use hiqdev\assetpackagist\models\AssetPackage; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\filters\VerbFilter; |
18
|
|
|
|
19
|
|
|
class SiteController extends \yii\web\Controller |
20
|
|
|
{ |
21
|
|
|
public function actions() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'error' => [ |
25
|
|
|
'class' => \yii\web\ErrorAction::class, |
26
|
|
|
], |
27
|
|
|
'captcha' => [ |
28
|
|
|
'class' => \yii\captcha\CaptchaAction::class, |
29
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function behaviors() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
'access' => [ |
38
|
|
|
'class' => VerbFilter::class, |
39
|
|
|
'actions' => [ |
40
|
|
|
'update' => ['post'], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function actionIndex() |
47
|
|
|
{ |
48
|
|
|
return $this->render('index'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function actionAbout() |
52
|
|
|
{ |
53
|
|
|
return $this->render('about'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function actionContact() |
57
|
|
|
{ |
58
|
|
|
return $this->render('contact'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function actionSearch($query) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
$package = $this->getAssetPackage($query); |
65
|
|
|
$params = ['package' => $package, 'query' => $query, 'forceUpdate' => false]; |
66
|
|
|
|
67
|
|
|
if ($package->canAutoUpdate()) { |
68
|
|
|
$params['forceUpdate'] = true; |
69
|
|
|
} |
70
|
|
|
} catch (Exception $e) { |
71
|
|
|
$query = preg_replace('/[^a-z0-9-]/i', '', $query); |
72
|
|
|
|
73
|
|
|
return $this->render('notFound', compact('query')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->render('search', $params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $query |
81
|
|
|
* @return AssetPackage |
82
|
|
|
*/ |
83
|
|
|
private static function getAssetPackage($query) |
84
|
|
|
{ |
85
|
|
|
list($type, $name) = AssetPackage::splitFullName($query); |
86
|
|
|
$package = new AssetPackage($type, $name); |
87
|
|
|
$package->load(); |
88
|
|
|
return $package; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function actionUpdate() |
92
|
|
|
{ |
93
|
|
|
session_write_close(); |
94
|
|
|
$query = Yii::$app->request->post('query'); |
95
|
|
|
|
96
|
|
|
$package = $this->getAssetPackage($query); |
97
|
|
|
if ($package->canBeUpdated()) { |
98
|
|
|
$package->update(); |
99
|
|
|
} else { |
100
|
|
|
Yii::$app->session->addFlash('update-impossible', true); |
101
|
|
|
} |
102
|
|
|
$package->load(); |
103
|
|
|
|
104
|
|
|
return $this->renderPartial('package-details', ['package' => $package]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|