Skip to content

Commit 8ddff2e

Browse files
committed
add Behaviors from Params Helper
1 parent 61adeda commit 8ddff2e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace app\helpers;
4+
5+
use Yii;
6+
use yii\filters\auth\CompositeAuth;
7+
8+
/**
9+
* Class AuthMethodsFromParamsHelper provide useful method to create your controller behaviors from application parameter
10+
* @author Heru Arief Wijaya @2020
11+
*/
12+
13+
class BehaviorsFromParamsHelper
14+
{
15+
16+
/**
17+
* Give available behaviors configuration from params.php
18+
* the result of BehaviorsFromParamsHelper::behaviors would be like following
19+
* ```php
20+
* $behaviors['authenticator'] = [
21+
* HttpBearerAuth::class,
22+
* HttpBasicAuth::class
23+
* ];
24+
* $behaviors['rateLimiter']['enableRateLimitHeader'] = false;
25+
* ```
26+
*
27+
* example use of this method in your controller
28+
* ```php
29+
* public function behaviors(){
30+
* $behaviors = parent::behaviors
31+
* $behaviors = BehaviorsFromParamsHelper::behaviors($behaviors);
32+
*
33+
* // you may use this if you want to add more behaviors
34+
* # $behaviors['otherMethod'] = $value;
35+
* return $behaviors;
36+
* }
37+
* ```
38+
*
39+
* @param object $behaviors object you use in your controller. $behaviors = parent::behaviors();
40+
* @return object $behaviors object you use in your controller. $behaviors = parent::behaviors();
41+
*/
42+
public static function behaviors($behaviors){
43+
$behaviorsFromParamsHelperObject = new self();
44+
return $behaviorsFromParamsHelperObject->getBehaviors($behaviors);
45+
}
46+
47+
private function getBehaviors($behaviors){
48+
$behaviors['authenticator'] = [
49+
'class' => CompositeAuth::class,
50+
'authMethods' => AuthMethodsFromParamsHelper::authMethods(),
51+
];
52+
if(Yii::$app->params['useRateLimiter']){
53+
$behaviors['rateLimiter']['enableRateLimitHeaders'] = false;
54+
}
55+
return $behaviors;
56+
}
57+
}

0 commit comments

Comments
 (0)