14
14
namespace CodeIgniter \Filters ;
15
15
16
16
use CodeIgniter \Config \Filters as BaseFiltersConfig ;
17
- use CodeIgniter \Exceptions \ConfigException ;
18
17
use CodeIgniter \Filters \Exceptions \FilterException ;
19
18
use CodeIgniter \HTTP \RequestInterface ;
20
19
use CodeIgniter \HTTP \ResponseInterface ;
@@ -66,25 +65,53 @@ class Filters
66
65
protected $ initialized = false ;
67
66
68
67
/**
69
- * The processed filters that will
70
- * be used to check against.
68
+ * The filter list to execute for the current request (URI path).
71
69
*
70
+ * This property is for display. Use $filtersClass to execute filters.
72
71
* This does not include "Required Filters".
73
72
*
74
- * @var array<string, array>
73
+ * [
74
+ * 'before' => [
75
+ * 'alias',
76
+ * 'alias:arg1',
77
+ * 'alias:arg1,arg2',
78
+ * ],
79
+ * 'after' => [
80
+ * 'alias',
81
+ * 'alias:arg1',
82
+ * 'alias:arg1,arg2',
83
+ * ],
84
+ * ]
85
+ *
86
+ * @var array{
87
+ * before: list<string>,
88
+ * after: list<string>
89
+ * }
75
90
*/
76
91
protected $ filters = [
77
92
'before ' => [],
78
93
'after ' => [],
79
94
];
80
95
81
96
/**
82
- * The collection of filters' class names that will
83
- * be used to execute in each position .
97
+ * The collection of filters' class names to execute for the current request
98
+ * (URI path) .
84
99
*
85
100
* This does not include "Required Filters".
86
101
*
87
- * @var array<string, array>
102
+ * [
103
+ * 'before' => [
104
+ * [classname, arguments],
105
+ * ],
106
+ * 'after' => [
107
+ * [classname, arguments],
108
+ * ],
109
+ * ]
110
+ *
111
+ * @var array{
112
+ * before: list<array{0: class-string, 1: list<string>}>,
113
+ * after: list<array{0: class-string, 1: list<string>}>
114
+ * }
88
115
*/
89
116
protected $ filtersClass = [
90
117
'before ' => [],
@@ -95,13 +122,17 @@ class Filters
95
122
* Any arguments to be passed to filters.
96
123
*
97
124
* @var array<string, list<string>|null> [name => params]
125
+ *
126
+ * @deprecated 4.6.0 No longer used.
98
127
*/
99
128
protected $ arguments = [];
100
129
101
130
/**
102
131
* Any arguments to be passed to filtersClass.
103
132
*
104
133
* @var array<class-string, list<string>|null> [classname => arguments]
134
+ *
135
+ * @deprecated 4.6.0 No longer used.
105
136
*/
106
137
protected $ argumentsClass = [];
107
138
@@ -193,17 +224,17 @@ public function run(string $uri, string $position = 'before')
193
224
*/
194
225
private function runBefore (array $ filterClasses )
195
226
{
196
- foreach ($ filterClasses as $ className ) {
227
+ foreach ($ filterClasses as $ filterClassInfo ) {
228
+ $ className = $ filterClassInfo [0 ];
229
+ $ arguments = ($ filterClassInfo [1 ] === []) ? null : $ filterClassInfo [1 ];
230
+
197
231
$ class = new $ className ();
198
232
199
233
if (! $ class instanceof FilterInterface) {
200
234
throw FilterException::forIncorrectInterface ($ class ::class);
201
235
}
202
236
203
- $ result = $ class ->before (
204
- $ this ->request ,
205
- $ this ->argumentsClass [$ className ] ?? null
206
- );
237
+ $ result = $ class ->before ($ this ->request , $ arguments );
207
238
208
239
if ($ result instanceof RequestInterface) {
209
240
$ this ->request = $ result ;
@@ -231,18 +262,17 @@ private function runBefore(array $filterClasses)
231
262
232
263
private function runAfter (array $ filterClasses ): ResponseInterface
233
264
{
234
- foreach ($ filterClasses as $ className ) {
265
+ foreach ($ filterClasses as $ filterClassInfo ) {
266
+ $ className = $ filterClassInfo [0 ];
267
+ $ arguments = ($ filterClassInfo [1 ] === []) ? null : $ filterClassInfo [1 ];
268
+
235
269
$ class = new $ className ();
236
270
237
271
if (! $ class instanceof FilterInterface) {
238
272
throw FilterException::forIncorrectInterface ($ class ::class);
239
273
}
240
274
241
- $ result = $ class ->after (
242
- $ this ->request ,
243
- $ this ->response ,
244
- $ this ->argumentsClass [$ className ] ?? null
245
- );
275
+ $ result = $ class ->after ($ this ->request , $ this ->response , $ arguments );
246
276
247
277
if ($ result instanceof ResponseInterface) {
248
278
$ this ->response = $ result ;
@@ -277,9 +307,11 @@ public function runRequired(string $position = 'before')
277
307
278
308
foreach ($ filters as $ alias ) {
279
309
if (is_array ($ aliases [$ alias ])) {
280
- $ filterClasses [$ position ] = array_merge ($ filterClasses [$ position ], $ aliases [$ alias ]);
310
+ foreach ($ this ->config ->aliases [$ alias ] as $ class ) {
311
+ $ filterClasses [$ position ][] = [$ class , []];
312
+ }
281
313
} else {
282
- $ filterClasses [$ position ][] = $ aliases [$ alias ];
314
+ $ filterClasses [$ position ][] = [ $ aliases [$ alias], [] ];
283
315
}
284
316
}
285
317
@@ -406,6 +438,11 @@ public function initialize(?string $uri = null)
406
438
// Set the toolbar filter to the last position to be executed
407
439
$ this ->filters ['after ' ] = $ this ->setToolbarToLast ($ this ->filters ['after ' ]);
408
440
441
+ // Since some filters like rate limiters rely on being executed once a request,
442
+ // we filter em here.
443
+ $ this ->filters ['before ' ] = array_unique ($ this ->filters ['before ' ]);
444
+ $ this ->filters ['after ' ] = array_unique ($ this ->filters ['after ' ]);
445
+
409
446
$ this ->processAliasesToClass ('before ' );
410
447
$ this ->processAliasesToClass ('after ' );
411
448
@@ -436,6 +473,11 @@ public function reset(): self
436
473
/**
437
474
* Returns the processed filters array.
438
475
* This does not include "Required Filters".
476
+ *
477
+ * @return array{
478
+ * before: list<string>,
479
+ * after: list<string>
480
+ * }
439
481
*/
440
482
public function getFilters (): array
441
483
{
@@ -445,6 +487,11 @@ public function getFilters(): array
445
487
/**
446
488
* Returns the filtersClass array.
447
489
* This does not include "Required Filters".
490
+ *
491
+ * @return array{
492
+ * before: list<array{0: class-string, 1: list<string>}>,
493
+ * after: list<array{0: class-string, 1: list<string>}>
494
+ * }
448
495
*/
449
496
public function getFiltersClass (): array
450
497
{
@@ -485,29 +532,31 @@ public function addFilter(string $class, ?string $alias = null, string $when = '
485
532
* are passed to the filter when executed.
486
533
*
487
534
* @param string $name filter_name or filter_name:arguments like 'role:admin,manager'
535
+ * or filter classname.
488
536
*/
489
537
private function enableFilter (string $ name , string $ when = 'before ' ): void
490
538
{
491
- // Get arguments and clean name
492
- [$ name , $ arguments ] = $ this ->getCleanName ($ name );
493
- $ this ->arguments [$ name ] = ($ arguments !== []) ? $ arguments : null ;
494
-
495
- if (class_exists ($ name )) {
496
- $ this ->config ->aliases [$ name ] = $ name ;
497
- } elseif (! array_key_exists ($ name , $ this ->config ->aliases )) {
498
- throw FilterException::forNoAlias ($ name );
539
+ // Normalize the arguments.
540
+ [$ alias , $ arguments ] = $ this ->getCleanName ($ name );
541
+ if ($ arguments === []) {
542
+ $ filter = $ alias ;
543
+ } else {
544
+ $ filter = $ alias . ': ' . implode (', ' , $ arguments );
499
545
}
500
546
501
- $ classNames = ( array ) $ this -> config -> aliases [ $ name ];
502
-
503
- foreach ( $ classNames as $ className ) {
504
- $ this -> argumentsClass [ $ className ] = $ this -> arguments [ $ name ] ?? null ;
547
+ if ( class_exists ( $ alias )) {
548
+ $ this -> config -> aliases [ $ alias ] = $ alias ;
549
+ } elseif (! array_key_exists ( $ alias , $ this -> config -> aliases ) ) {
550
+ throw FilterException:: forNoAlias ( $ alias ) ;
505
551
}
506
552
507
- if (! isset ($ this ->filters [$ when ][$ name ])) {
508
- $ this ->filters [$ when ][] = $ name ;
509
- $ this ->filtersClass [$ when ] = array_merge ($ this ->filtersClass [$ when ], $ classNames );
553
+ if (! isset ($ this ->filters [$ when ][$ filter ])) {
554
+ $ this ->filters [$ when ][] = $ filter ;
510
555
}
556
+
557
+ // Since some filters like rate limiters rely on being executed once a request,
558
+ // we filter em here.
559
+ $ this ->filters [$ when ] = array_unique ($ this ->filters [$ when ]);
511
560
}
512
561
513
562
/**
@@ -557,6 +606,8 @@ public function enableFilters(array $names, string $when = 'before')
557
606
* Returns the arguments for a specified key, or all.
558
607
*
559
608
* @return array<string, string>|string
609
+ *
610
+ * @deprecated 4.6.0 Already does not work.
560
611
*/
561
612
public function getArguments (?string $ key = null )
562
613
{
@@ -692,27 +743,15 @@ protected function processFilters(?string $uri = null)
692
743
$ path = $ settings ['before ' ];
693
744
694
745
if ($ this ->pathApplies ($ uri , $ path )) {
695
- // Get arguments and clean name
696
- [$ name , $ arguments ] = $ this ->getCleanName ($ alias );
697
-
698
- $ filters ['before ' ][] = $ name ;
699
-
700
- $ this ->registerArguments ($ name , $ arguments );
746
+ $ filters ['before ' ][] = $ alias ;
701
747
}
702
748
}
703
749
704
750
if (isset ($ settings ['after ' ])) {
705
751
$ path = $ settings ['after ' ];
706
752
707
753
if ($ this ->pathApplies ($ uri , $ path )) {
708
- // Get arguments and clean name
709
- [$ name , $ arguments ] = $ this ->getCleanName ($ alias );
710
-
711
- $ filters ['after ' ][] = $ name ;
712
-
713
- // The arguments may have already been registered in the before filter.
714
- // So disable check.
715
- $ this ->registerArguments ($ name , $ arguments , false );
754
+ $ filters ['after ' ][] = $ alias ;
716
755
}
717
756
}
718
757
}
@@ -736,31 +775,6 @@ protected function processFilters(?string $uri = null)
736
775
}
737
776
}
738
777
739
- /**
740
- * @param string $name filter alias
741
- * @param array $arguments filter arguments
742
- * @param bool $check if true, check if already defined
743
- */
744
- private function registerArguments (string $ name , array $ arguments , bool $ check = true ): void
745
- {
746
- if ($ arguments !== []) {
747
- if ($ check && array_key_exists ($ name , $ this ->arguments )) {
748
- throw new ConfigException (
749
- '" ' . $ name . '" already has arguments: '
750
- . (($ this ->arguments [$ name ] === null ) ? 'null ' : implode (', ' , $ this ->arguments [$ name ]))
751
- );
752
- }
753
-
754
- $ this ->arguments [$ name ] = $ arguments ;
755
- }
756
-
757
- $ classNames = (array ) $ this ->config ->aliases [$ name ];
758
-
759
- foreach ($ classNames as $ className ) {
760
- $ this ->argumentsClass [$ className ] = $ this ->arguments [$ name ] ?? null ;
761
- }
762
- }
763
-
764
778
/**
765
779
* Maps filter aliases to the equivalent filter classes
766
780
*
@@ -772,32 +786,28 @@ protected function processAliasesToClass(string $position)
772
786
{
773
787
$ filterClasses = [];
774
788
775
- foreach ($ this ->filters [$ position ] as $ alias => $ rules ) {
776
- if (is_numeric ($ alias ) && is_string ($ rules )) {
777
- $ alias = $ rules ;
778
- }
789
+ foreach ($ this ->filters [$ position ] as $ filter ) {
790
+ // Get arguments and clean alias
791
+ [$ alias , $ arguments ] = $ this ->getCleanName ($ filter );
779
792
780
793
if (! array_key_exists ($ alias , $ this ->config ->aliases )) {
781
794
throw FilterException::forNoAlias ($ alias );
782
795
}
783
796
784
797
if (is_array ($ this ->config ->aliases [$ alias ])) {
785
- $ filterClasses = [...$ filterClasses , ...$ this ->config ->aliases [$ alias ]];
798
+ foreach ($ this ->config ->aliases [$ alias ] as $ class ) {
799
+ $ filterClasses [] = [$ class , $ arguments ];
800
+ }
786
801
} else {
787
- $ filterClasses [] = $ this ->config ->aliases [$ alias ];
802
+ $ filterClasses [] = [ $ this ->config ->aliases [$ alias], $ arguments ];
788
803
}
789
804
}
790
805
791
- // when using enableFilter() we already write the class name in $filterClasses as well as the
792
- // alias in $filters. This leads to duplicates when using route filters.
793
806
if ($ position === 'before ' ) {
794
807
$ this ->filtersClass [$ position ] = array_merge ($ filterClasses , $ this ->filtersClass [$ position ]);
795
808
} else {
796
809
$ this ->filtersClass [$ position ] = array_merge ($ this ->filtersClass [$ position ], $ filterClasses );
797
810
}
798
-
799
- // Since some filters like rate limiters rely on being executed once a request we filter em here.
800
- $ this ->filtersClass [$ position ] = array_values (array_unique ($ this ->filtersClass [$ position ]));
801
811
}
802
812
803
813
/**
0 commit comments