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