Skip to content

Commit f66e733

Browse files
committed
Added BowerPackageController and actionFetchTop
1 parent b05ba4e commit f66e733

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/config/aliases.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
'@storage' => '<base-dir>/web',
1414
'@bower' => '@vendor/bower-asset',
1515
'@npm' => '@vendor/npm-asset',
16+
'@runtime' => '<base-dir>/runtime',
1617
];

src/config/goals.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
asset-package:
22
class: hiqdev\assetpackagist\console\AssetPackageController
3+
bower-package:
4+
class: hiqdev\assetpackagist\console\BowerPackageController
35

46
install:
57
after:

src/console/AssetPackageController.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use hiqdev\assetpackagist\models\AssetPackage;
1515
use hiqdev\assetpackagist\models\Storage;
1616
use Yii;
17+
use yii\helpers\Console;
1718

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

0 commit comments

Comments
 (0)