From 74e2b2936781d10a209f8d04b117a4a2f31d9fe0 Mon Sep 17 00:00:00 2001 From: Igor Chepurnoy Date: Mon, 6 Feb 2017 22:44:10 +0200 Subject: [PATCH 1/4] add pageSize property to AssignmentSearch --- ConsoleModule.php | 4 ++-- Module.php | 13 ++++++++----- README.md | 16 +++++++++++++--- base/ItemController.php | 12 ++++++++++-- commands/MigrateController.php | 2 +- composer.json | 1 + controllers/AssignmentController.php | 23 ++++++++++++----------- controllers/RouteController.php | 2 +- controllers/RuleController.php | 2 +- migrations/Migration.php | 2 +- models/BizRuleModel.php | 2 +- models/search/AssignmentSearch.php | 11 +++++++++-- tests/models/BizRuleTest.php | 4 ++-- tests/models/RoleTest.php | 2 +- 14 files changed, 63 insertions(+), 33 deletions(-) diff --git a/ConsoleModule.php b/ConsoleModule.php index bde60ec..db7be96 100644 --- a/ConsoleModule.php +++ b/ConsoleModule.php @@ -7,7 +7,7 @@ * * Use [[\yii\base\Module::$controllerMap]] to change property of controller. * - * ~~~ + * ```php * 'controllerMap' => [ * 'migrate' => [ * 'class' => 'yii2mod\rbac\commands\MigrateController', @@ -16,7 +16,7 @@ * 'templateFile' => 'your own template file' * ] * ] - * ~~~ + * ``` */ class ConsoleModule extends Module { diff --git a/Module.php b/Module.php index dc60acf..f206bc1 100755 --- a/Module.php +++ b/Module.php @@ -7,22 +7,25 @@ * * Use [[\yii\base\Module::$controllerMap]] to change property of controller. * - * ~~~ + * ```php * 'controllerMap' => [ * 'assignment' => [ * 'class' => 'yii2mod\rbac\controllers\AssignmentController', * 'userIdentityClass' => 'app\models\User', - * 'searchClass' => 'Your own search model' + * 'searchClass' => [ + * 'class' => 'yii2mod\rbac\models\search\AssignmentSearch', + * 'pageSize' => 10, + * ], * 'idField' => 'id', * 'usernameField' => 'username' * 'gridViewColumns' => [ * 'id', * 'username', * 'email' - * ] - * ] + * ], + * ], * ], - * ~~~ + * ```php */ class Module extends \yii\base\Module { diff --git a/README.md b/README.md index e87a157..ffa7b02 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,15 @@ return [ 'modules' => [ 'rbac' => [ 'class' => 'yii2mod\rbac\Module', - // Some controller property maybe need to change. + // Some controller property maybe need to change. 'controllerMap' => [ 'assignment' => [ 'class' => 'yii2mod\rbac\controllers\AssignmentController', 'userIdentityClass' => 'app\models\User', - 'searchClass' => 'Your own search model class', + 'searchClass' => [ + 'class' => 'yii2mod\rbac\models\search\AssignmentSearch', + 'pageSize' => 10, + ], 'idField' => 'id', 'usernameField' => 'username', 'gridViewColumns' => [ @@ -57,7 +60,14 @@ return [ 'username', 'email' ] - ] + ], + 'role' => [ + 'class' => 'yii2mod\rbac\controllers\RoleController', + 'searchClass' => [ + 'class' => 'yii2mod\rbac\models\search\AuthItemSearch', + 'pageSize' => 10, + ], + ], ] ], ] diff --git a/base/ItemController.php b/base/ItemController.php index 9bfc7d4..ce34eb2 100644 --- a/base/ItemController.php +++ b/base/ItemController.php @@ -18,6 +18,13 @@ */ class ItemController extends Controller { + /** + * @var string search class name for auth items search + */ + public $searchClass = [ + 'class' => AuthItemSearch::class, + ]; + /** * @var int Type of Auth Item */ @@ -35,7 +42,7 @@ public function behaviors() { return [ 'verbs' => [ - 'class' => VerbFilter::className(), + 'class' => VerbFilter::class, 'actions' => [ 'index' => ['get'], 'view' => ['get'], @@ -63,7 +70,8 @@ public function behaviors() */ public function actionIndex() { - $searchModel = new AuthItemSearch(['type' => $this->type]); + $searchModel = Yii::createObject($this->searchClass); + $searchModel->type = $this->type; $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ diff --git a/commands/MigrateController.php b/commands/MigrateController.php index a5e48ae..781f290 100755 --- a/commands/MigrateController.php +++ b/commands/MigrateController.php @@ -53,7 +53,7 @@ class MigrateController extends BaseMigrateController */ public function init() { - $this->db = Instance::ensure($this->db, Connection::className()); + $this->db = Instance::ensure($this->db, Connection::class); parent::init(); } diff --git a/composer.json b/composer.json index f919a24..30d35f9 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ } ], "require": { + "php": ">=5.6", "yiisoft/yii2": "^2.0.8", "2amigos/yii2-arrayquery-component": "*", "yiisoft/yii2-jui": "*" diff --git a/controllers/AssignmentController.php b/controllers/AssignmentController.php index 5c2ffad..dc85e34 100755 --- a/controllers/AssignmentController.php +++ b/controllers/AssignmentController.php @@ -24,7 +24,9 @@ class AssignmentController extends Controller /** * @var string search class name for assignments search */ - public $searchClass; + public $searchClass = [ + 'class' => AssignmentSearch::class, + ]; /** * @var string id column name @@ -42,11 +44,13 @@ class AssignmentController extends Controller public $gridViewColumns = []; /** - * Initializes the object. + * @inheritdoc */ public function init() { - if (empty($this->userIdentityClass)) { + parent::init(); + + if ($this->userIdentityClass === null) { $this->userIdentityClass = Yii::$app->user->identityClass; } @@ -56,14 +60,10 @@ public function init() $this->usernameField, ]; } - - parent::init(); } /** - * Returns a list of behaviors that this component should behave as. - * - * @return array + * @inheritdoc */ public function behaviors() { @@ -94,11 +94,12 @@ public function behaviors() */ public function actionIndex() { - if (empty($this->searchClass)) { - $searchModel = Yii::createObject(AssignmentSearch::className()); + /* @var AssignmentSearch */ + $searchModel = Yii::createObject($this->searchClass); + + if ($searchModel instanceof AssignmentSearch) { $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->userIdentityClass, $this->idField, $this->usernameField); } else { - $searchModel = Yii::createObject($this->searchClass); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); } diff --git a/controllers/RouteController.php b/controllers/RouteController.php index 498303a..99d8e6f 100755 --- a/controllers/RouteController.php +++ b/controllers/RouteController.php @@ -24,7 +24,7 @@ public function behaviors() { return [ 'verbs' => [ - 'class' => VerbFilter::className(), + 'class' => VerbFilter::class, 'actions' => [ 'index' => ['get', 'post'], 'create' => ['post'], diff --git a/controllers/RuleController.php b/controllers/RuleController.php index 5a601a6..e822894 100644 --- a/controllers/RuleController.php +++ b/controllers/RuleController.php @@ -25,7 +25,7 @@ public function behaviors() { return [ 'verbs' => [ - 'class' => VerbFilter::className(), + 'class' => VerbFilter::class, 'actions' => [ 'index' => ['get'], 'view' => ['get'], diff --git a/migrations/Migration.php b/migrations/Migration.php index 2ead707..bc3d1d2 100644 --- a/migrations/Migration.php +++ b/migrations/Migration.php @@ -30,7 +30,7 @@ class Migration extends Component implements MigrationInterface */ public function init() { - $this->authManager = Instance::ensure($this->authManager, DbManager::className()); + $this->authManager = Instance::ensure($this->authManager, DbManager::class); parent::init(); } diff --git a/models/BizRuleModel.php b/models/BizRuleModel.php index e2e3341..7f4b2a1 100755 --- a/models/BizRuleModel.php +++ b/models/BizRuleModel.php @@ -88,7 +88,7 @@ public function classExists() return; } - if (!is_subclass_of($this->className, Rule::className())) { + if (!is_subclass_of($this->className, Rule::class)) { $message = Yii::t('yii2mod.rbac', "'{class}' must extend from 'yii\\rbac\\Rule' or its child class", [ 'class' => $this->className, ]); $this->addError('className', $message); diff --git a/models/search/AssignmentSearch.php b/models/search/AssignmentSearch.php index c71c95b..ce8a239 100644 --- a/models/search/AssignmentSearch.php +++ b/models/search/AssignmentSearch.php @@ -22,6 +22,11 @@ class AssignmentSearch extends Model */ public $username; + /** + * @var int the default page size + */ + public $pageSize = 25; + /** * @inheritdoc */ @@ -49,11 +54,13 @@ public function search($params, $class, $idField, $usernameField) $dataProvider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ - 'pageSize' => 20, + 'pageSize' => $this->pageSize, ], ]); - if (!($this->load($params) && $this->validate())) { + $this->load($params); + + if (!$this->validate()) { return $dataProvider; } diff --git a/tests/models/BizRuleTest.php b/tests/models/BizRuleTest.php index e69a67e..546a27d 100644 --- a/tests/models/BizRuleTest.php +++ b/tests/models/BizRuleTest.php @@ -19,12 +19,12 @@ public function testCreateRule() { $model = new BizRuleModel(); $model->name = 'guest'; - $model->className = GuestRule::className(); + $model->className = GuestRule::class; $this->assertTrue($model->save()); $rule = Yii::$app->authManager->getRule($model->name); - $this->assertInstanceOf(Rule::className(), $rule); + $this->assertInstanceOf(Rule::class, $rule); return $rule; } diff --git a/tests/models/RoleTest.php b/tests/models/RoleTest.php index dbeae84..961ed2e 100644 --- a/tests/models/RoleTest.php +++ b/tests/models/RoleTest.php @@ -23,7 +23,7 @@ public function testCreateRole() $model->description = 'admin role'; $this->assertTrue($model->save()); - $this->assertInstanceOf(Role::className(), Yii::$app->authManager->getRole('admin')); + $this->assertInstanceOf(Role::class, Yii::$app->authManager->getRole('admin')); return Yii::$app->authManager->getRole('admin'); } From 2e506a2d6069e946b38d8e784a9e6bd27064574d Mon Sep 17 00:00:00 2001 From: Igor Chepurnoy Date: Tue, 7 Feb 2017 02:23:27 +0200 Subject: [PATCH 2/4] added pageSize to BizRuleSearch class --- controllers/RuleController.php | 9 ++++++++- models/search/AuthItemSearch.php | 7 ++++++- models/search/BizRuleSearch.php | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/controllers/RuleController.php b/controllers/RuleController.php index e822894..e1c07d0 100644 --- a/controllers/RuleController.php +++ b/controllers/RuleController.php @@ -16,6 +16,13 @@ */ class RuleController extends Controller { + /** + * @var string search class name for rules search + */ + public $searchClass = [ + 'class' => BizRuleSearch::class, + ]; + /** * Returns a list of behaviors that this component should behave as. * @@ -44,7 +51,7 @@ public function behaviors() */ public function actionIndex() { - $searchModel = new BizRuleSearch(); + $searchModel = Yii::createObject($this->searchClass); $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); return $this->render('index', [ diff --git a/models/search/AuthItemSearch.php b/models/search/AuthItemSearch.php index de91860..705f1ca 100644 --- a/models/search/AuthItemSearch.php +++ b/models/search/AuthItemSearch.php @@ -35,6 +35,11 @@ class AuthItemSearch extends Model */ public $ruleName; + /** + * @var int the default page size + */ + public $pageSize = 25; + /** * @inheritdoc */ @@ -96,7 +101,7 @@ public function search($params) 'attributes' => ['name'], ], 'pagination' => [ - 'pageSize' => 25, + 'pageSize' => $this->pageSize, ], ]); } diff --git a/models/search/BizRuleSearch.php b/models/search/BizRuleSearch.php index 5b9a89f..fa7e1cb 100644 --- a/models/search/BizRuleSearch.php +++ b/models/search/BizRuleSearch.php @@ -19,6 +19,11 @@ class BizRuleSearch extends Model */ public $name; + /** + * @var int the default page size + */ + public $pageSize = 25; + /** * @inheritdoc */ @@ -51,7 +56,7 @@ public function search($params) 'attributes' => ['name'], ], 'pagination' => [ - 'pageSize' => 25, + 'pageSize' => $this->pageSize, ], ]); } From 608547cde3b38a0326be93b145e80289c26258a7 Mon Sep 17 00:00:00 2001 From: Igor Chepurnoy Date: Tue, 7 Feb 2017 02:25:47 +0200 Subject: [PATCH 3/4] update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index f49db9d..5cc3274 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,13 @@ return [ 'pageSize' => 10, ], ], + 'rule' => [ + 'class' => 'yii2mod\rbac\controllers\RuleController', + 'searchClass' => [ + 'class' => 'yii2mod\rbac\models\search\BizRuleSearch', + 'pageSize' => 10 + ], + ], ] ], ] From cdb00ccd3f77af3e206725f4d496912cc6692716 Mon Sep 17 00:00:00 2001 From: Igor Chepurnoy Date: Tue, 7 Feb 2017 02:26:42 +0200 Subject: [PATCH 4/4] update travis config --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8deeddd..1180100 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - - 7.0 + - 7.1 # faster builds on new travis setup not using sudo sudo: false