added commands and other tweaks

This commit is contained in:
cornernote 2015-08-09 02:47:29 +09:30
parent 1e90351c16
commit 707e4513f3
12 changed files with 192 additions and 33 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace app\commands;
use app\components\Git;
use app\models\Package;
use Yii;
use yii\console\Controller;
class PackageController extends Controller
{
public function actionUpdateBower()
{
$packages = Package::find()->all();
foreach ($packages as $package) {
$package->bower = Git::get($package->url, 'bower.json');
$package->save();
}
}
}

54
components/Git.php Normal file
View File

@ -0,0 +1,54 @@
<?php
namespace app\components;
/**
* Git
* @package app\components
*/
class Git
{
/**
* @param $endpoint
* @param $file
* @return bool|string
*/
public static function get($endpoint, $file)
{
if (strpos($endpoint, 'github.com')) {
return self::getGitHub($endpoint, $file);
}
if (strpos($endpoint, 'bitbucket.org')) {
return self::getBitBucket($endpoint, $file);
}
return false;
}
/**
* @param $endpoint
* @param $file
* @return string
*/
private static function getGitHub($endpoint, $file)
{
$https = strtr($endpoint, [
'git://' => 'https://raw.',
'.git' => '/master/' . $file,
]);
return @file_get_contents($https);
}
/**
* @param $endpoint
* @param $file
* @return string
*/
private static function getBitBucket($endpoint, $file)
{
$https = strtr($endpoint, [
'git://' => 'https://',
'.git' => '/raw/master/' . $file,
]);
return @file_get_contents($https);
}
}

2
config/bootstrap.php Normal file
View File

@ -0,0 +1,2 @@
<?php

3
config/init.php Normal file
View File

@ -0,0 +1,3 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', getenv('YII_DEBUG') ? (getenv('YII_DEBUG') == 'false' ? false : getenv('YII_DEBUG')) : true);
defined('YII_ENV') or define('YII_ENV', getenv('YII_ENV') ? getenv('YII_ENV') : 'dev');

View File

@ -1,4 +1,5 @@
<?php <?php
use yii\helpers\ArrayHelper;
$config = [ $config = [
'id' => 'minetest-bower', 'id' => 'minetest-bower',
@ -6,6 +7,11 @@ $config = [
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'components' => [ 'components' => [
'db' => require(__DIR__ . '/db.php'), 'db' => require(__DIR__ . '/db.php'),
],
];
$web = [
'components' => [
'errorHandler' => [ 'errorHandler' => [
'errorAction' => 'site/error', 'errorAction' => 'site/error',
], ],
@ -25,4 +31,16 @@ $config = [
], ],
]; ];
$console = [
'controllerNamespace' => 'app\commands',
];
if (php_sapi_name() != 'cli') {
// Web application
$config = ArrayHelper::merge($config, $web);
} else {
// Console application
$config = ArrayHelper::merge($config, $console);
}
return $config; return $config;

View File

@ -2,6 +2,7 @@
namespace app\controllers; namespace app\controllers;
use app\components\Git;
use app\models\Package; use app\models\Package;
use Yii; use Yii;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
@ -31,6 +32,36 @@ class PackageController extends Controller
return parent::beforeAction($action); return parent::beforeAction($action);
} }
public function actionIndex()
{
return Package::find()
->select(['name', 'url', 'hits'])
->orderBy(['hits' => SORT_DESC])
->all();
}
public function actionView($name)
{
$package = Package::find()
->where(['name' => $name])
->one();
if ($package) {
$package->hits++;
$package->save();
return $package;
}
throw new HttpException(404, 'Package not found.');
}
public function actionSearch($name)
{
return Package::find()
->select(['name', 'url', 'hits'])
->where(['like', 'name', $name])
->orderBy(['hits' => SORT_DESC])
->all();
}
public function actionCreate() public function actionCreate()
{ {
$package = new Package(); $package = new Package();
@ -47,7 +78,7 @@ class PackageController extends Controller
} }
} }
if (isset($package->errors['name'])) { if (isset($package->errors['name'])) {
throw new HttpException(400, 'Invalid Package Name: ' . implode(', ', $package->errors['name'])); throw new HttpException(400, 'Invalid Name: ' . implode(', ', $package->errors['name']));
} }
if (isset($package->errors['url'])) { if (isset($package->errors['url'])) {
throw new HttpException(400, 'Invalid URL: ' . implode(', ', $package->errors['url'])); throw new HttpException(400, 'Invalid URL: ' . implode(', ', $package->errors['url']));
@ -61,25 +92,4 @@ class PackageController extends Controller
throw new HttpException(400, 'Error: ' . implode(', ', $errors)); throw new HttpException(400, 'Error: ' . implode(', ', $errors));
} }
public function actionIndex()
{
return Package::find()->orderBy(['hits' => SORT_DESC])->all();
}
public function actionView($name)
{
$package = Package::find()->where(['name' => $name])->one();
if ($package) {
$package->hits++;
$package->save();
return $package;
}
throw new HttpException(404);
}
public function actionSearch($name)
{
return Package::find()->where(['like', 'name', $name])->orderBy(['hits' => SORT_DESC])->all();
}
} }

View File

@ -14,6 +14,7 @@ class m150808_000001_create_package extends Migration
'name' => Schema::TYPE_STRING . '(128) NOT NULL', 'name' => Schema::TYPE_STRING . '(128) NOT NULL',
'url' => Schema::TYPE_STRING . '(255) NOT NULL', 'url' => Schema::TYPE_STRING . '(255) NOT NULL',
'hits' => Schema::TYPE_INTEGER . ' NOT NULL', 'hits' => Schema::TYPE_INTEGER . ' NOT NULL',
'bower' => Schema::TYPE_TEXT,
'created_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL', 'created_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL',
'updated_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL', 'updated_at' => Schema::TYPE_TIMESTAMP . ' NOT NULL',
], ($this->db->driverName === 'mysql' ? 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB' : null)); ], ($this->db->driverName === 'mysql' ? 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB' : null));

View File

@ -2,8 +2,10 @@
namespace app\models; namespace app\models;
use app\models\query\PackageQuery;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/** /**
* This is the model class for table "{{%package}}". * This is the model class for table "{{%package}}".
@ -12,10 +14,11 @@ use yii\behaviors\TimestampBehavior;
* @property string $name * @property string $name
* @property string $url * @property string $url
* @property integer $hits * @property integer $hits
* @property string $bower
* @property string $created_at * @property string $created_at
* @property string $updated_at * @property string $updated_at
*/ */
class Package extends \yii\db\ActiveRecord class Package extends ActiveRecord
{ {
/** /**
* @inheritdoc * @inheritdoc
@ -49,6 +52,7 @@ class Package extends \yii\db\ActiveRecord
'name' => 'Name', 'name' => 'Name',
'url' => 'URL', 'url' => 'URL',
'hits' => 'Hits', 'hits' => 'Hits',
'bower' => 'Bower',
'created_at' => 'Created At', 'created_at' => 'Created At',
'updated_at' => 'Updated At', 'updated_at' => 'Updated At',
]; ];
@ -68,4 +72,13 @@ class Package extends \yii\db\ActiveRecord
], ],
]; ];
} }
/**
* @inheritdoc
* @return PackageQuery the active query used by this AR class.
*/
public static function find()
{
return new PackageQuery(get_called_class());
}
} }

View File

@ -0,0 +1,30 @@
<?php
namespace app\models\query;
use yii\db\ActiveQuery;
/**
* This is the ActiveQuery class for [[\app\models\Package]].
*
* @see \app\models\Package
*/
class PackageQuery extends ActiveQuery
{
/**
* @inheritdoc
* @return \app\models\Package[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return \app\models\Package|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}

View File

@ -33,9 +33,10 @@ $this->params['jumbotron'] = '/site/_index-jumbotron';
<h2>Create Packages</h2> <h2>Create Packages</h2>
<p>Create packages with <code>bower register</code>.</p> <p>Create packages with <code>bower register</code>.</p>
<pre>$ bower register &lt;name&gt; &lt;url&gt; <pre>$ bower register &lt;my_mod_name&gt; &lt;git_endpoint&gt;
$ bower register mymod git://github.com/username/mymod.git</pre> # for example
$ bower register example git://github.com/user/example.git</pre>
<!-- <!--
<h2>Search Packages</h2> <h2>Search Packages</h2>

View File

@ -1,11 +1,8 @@
<?php <?php
defined('YII_DEBUG') or define('YII_DEBUG', getenv('YII_DEBUG') ? (getenv('YII_DEBUG') == 'false' ? false : getenv('YII_DEBUG')) : true); require(__DIR__ . '/../config/init.php');
defined('YII_ENV') or define('YII_ENV', getenv('YII_ENV') ? getenv('YII_ENV') : 'dev');
require(__DIR__ . '/../vendor/autoload.php'); require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../config/bootstrap.php');
$config = require(__DIR__ . '/../config/web.php'); $config = require(__DIR__ . '/../config/main.php');
(new yii\web\Application($config))->run(); (new yii\web\Application($config))->run();

8
yii Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php
require(__DIR__ . '/config/init.php');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/config/bootstrap.php');
$config = require(__DIR__ . '/config/main.php');
exit((new yii\console\Application($config))->run());