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\console; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
use yii\helpers\Console; |
16
|
|
|
use yii\helpers\FileHelper; |
17
|
|
|
use yii\helpers\Json; |
18
|
|
|
|
19
|
|
|
class BowerPackageController extends \yii\console\Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Fetches TOP-$count components from Bower ans saves to `config/bower.list`. |
23
|
|
|
* |
24
|
|
|
* @param int $count |
25
|
|
|
* @param bool $skipCache |
26
|
|
|
*/ |
27
|
|
|
public function actionFetchTop($count = 1000, $skipCache = false) |
28
|
|
|
{ |
29
|
|
|
$result = []; |
30
|
|
|
$components = $this->getComponents($skipCache); |
31
|
|
|
ArrayHelper::multisort($components, 'stars', SORT_DESC, SORT_NUMERIC); |
32
|
|
|
|
33
|
|
|
foreach (array_slice($components, 0, $count) as $component) { |
34
|
|
|
$result[] = 'bower-asset/' . $component['name']; |
35
|
|
|
echo Console::renderColoredString("%R{$component['stars']}%N - %g{$component['name']}%N"); |
36
|
|
|
Console::moveCursorTo(0); |
37
|
|
|
Console::clearLine(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$componentsListPath = Yii::getAlias('@hiqdev/assetpackagist/config/bower.list'); |
41
|
|
|
file_put_contents($componentsListPath, implode("\n", $result)); |
42
|
|
|
|
43
|
|
|
echo Console::renderColoredString('Fetched %YBower%N components list. Found %G' . count($components) . "%N components.\n"); |
44
|
|
|
echo Console::renderColoredString('Only %bTOP-' . $count . "%N components were added to the packages list.\n"); |
45
|
|
|
echo Console::renderColoredString('See %G' . $componentsListPath . "%N\n"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets components array from bower API. |
50
|
|
|
* |
51
|
|
|
* @param bool $skipCache whether to skip using local cache |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
private function getComponents($skipCache = false) |
55
|
|
|
{ |
56
|
|
|
$url = 'http://bower-component-list.herokuapp.com'; |
57
|
|
|
$componentsFilePath = Yii::getAlias('@runtime/bower-cache/components.list'); |
58
|
|
|
|
59
|
|
|
if (!$skipCache && is_file($componentsFilePath) && (time() - filemtime($componentsFilePath) < 60 * 60 * 6)) { // 6 hours |
60
|
|
|
$raw = file_get_contents($componentsFilePath); |
61
|
|
|
} else { |
62
|
|
|
$result = (new \GuzzleHttp\Client())->request('GET', $url); |
63
|
|
|
$raw = $result->getBody(); |
64
|
|
|
FileHelper::createDirectory(dirname($componentsFilePath)); |
65
|
|
|
file_put_contents($componentsFilePath, $raw); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return Json::decode($raw); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|