4
4
5
5
use Arris \AppRouter \FastRoute \ConfigureRoutes ;
6
6
use Arris \AppRouter \FastRoute \Dispatcher ;
7
+ use Arris \AppRouter \Helper ;
7
8
use Arris \AppRouter \Stack ;
8
9
use Arris \Exceptions \AppRouterHandlerError ;
9
10
use Arris \Exceptions \AppRouterMethodNotAllowedException ;
@@ -90,13 +91,6 @@ class AppRouter implements AppRouterInterface
90
91
*/
91
92
private static string $ current_prefix = '' ;
92
93
93
- /**
94
- * Default replace pattern
95
- *
96
- * @var string
97
- */
98
- // private static string $routeReplacePattern = '%%$1%%';
99
-
100
94
/**
101
95
* Current Routing Info
102
96
*
@@ -151,6 +145,13 @@ class AppRouter implements AppRouterInterface
151
145
*/
152
146
private static array $ routeRule = [];
153
147
148
+ /**
149
+ * STUB для внутренних опций
150
+ *
151
+ * @var array
152
+ */
153
+ private static array $ options = [];
154
+
154
155
/**
155
156
* Разрешать ли пустые группы (без роутов, но с опциями или миддлварами)
156
157
*
@@ -193,8 +194,10 @@ class AppRouter implements AppRouterInterface
193
194
*/
194
195
private static bool $ option_use_aliases = false ;
195
196
196
- /* ============================================================================================================================================*/
197
197
198
+ /**
199
+ * @inheritDoc
200
+ */
198
201
public function __construct (
199
202
LoggerInterface $ logger = null ,
200
203
string $ namespace = '' ,
@@ -205,6 +208,9 @@ public function __construct(
205
208
self ::init ($ logger , namespace: $ namespace , prefix: $ prefix , allowEmptyGroups: $ allowEmptyGroups , allowEmptyHandlers: $ allowEmptyHandlers );
206
209
}
207
210
211
+ /**
212
+ * @inheritDoc
213
+ */
208
214
public static function init (
209
215
LoggerInterface $ logger = null ,
210
216
string $ namespace = '' ,
@@ -280,12 +286,7 @@ public static function setDefaultNamespace(string $namespace = ''):void
280
286
self ::$ current_namespace = $ namespace ;
281
287
}
282
288
283
- /**
284
- * Указывает нэймспейс для миддлваров-посредников
285
- *
286
- * @param string $namespace
287
- * @return void
288
- */
289
+
289
290
public static function setMiddlewaresNamespace (string $ namespace = '' ):void
290
291
{
291
292
// return true;
@@ -344,6 +345,7 @@ public static function post($route, $handler, $name = null)
344
345
];
345
346
}
346
347
348
+
347
349
public static function put ($ route , $ handler , $ name = null )
348
350
{
349
351
if (is_null ($ route ) || is_null ($ handler )) {
@@ -637,7 +639,6 @@ public static function dispatch()
637
639
}
638
640
639
641
$ r ->addRoute ($ rule ['httpMethod ' ], $ route , $ handler , $ rule );
640
- // $r->addRoute($rule['httpMethod'], $rule['route'], $handler, $rule);
641
642
}
642
643
});
643
644
@@ -746,7 +747,7 @@ public static function dispatch()
746
747
/**
747
748
* @inheritDoc
748
749
*
749
- * @return array| Dispatcher\Result\ResultInterface
750
+ * @return Dispatcher\Result\ResultInterface
750
751
*/
751
752
public static function getRoutingInfo ()
752
753
{
@@ -794,7 +795,7 @@ private static function getInternalRuleKey(string $httpMethod, $handler, string
794
795
}
795
796
796
797
if ($ handler instanceof \Closure) {
797
- $ internal_name = self ::getClosureInternalName ($ handler );
798
+ $ internal_name = Helper ::getClosureInternalName ($ handler );
798
799
} elseif (is_array ($ handler )) {
799
800
800
801
// Хэндлер может быть массивом, но пустым. Вообще, это ошибка, поэтому генерим имя на основе md5 роута и метода.
@@ -822,7 +823,7 @@ private static function getInternalRuleKey(string $httpMethod, $handler, string
822
823
* @param bool $is_static
823
824
* @return void
824
825
*/
825
- public static function checkClassExists ($ class , bool $ is_static = false ): void
826
+ private static function checkClassExists ($ class , bool $ is_static = false ): void
826
827
{
827
828
// быстрее, чем рефлексия
828
829
if (!class_exists ($ class )){
@@ -845,7 +846,7 @@ public static function checkClassExists($class, bool $is_static = false): void
845
846
* @param bool $is_static
846
847
* @return void
847
848
*/
848
- public static function checkMethodExists ($ class , $ method , bool $ is_static = false ): void
849
+ private static function checkMethodExists ($ class , $ method , bool $ is_static = false ): void
849
850
{
850
851
$ prompt = $ is_static ? 'static class ' : 'class ' ;
851
852
@@ -878,7 +879,7 @@ public static function checkMethodExists($class, $method, bool $is_static = fals
878
879
* @return bool
879
880
* @throws AppRouterHandlerError
880
881
*/
881
- public static function checkFunctionExists ($ handler ): bool
882
+ private static function checkFunctionExists ($ handler ): bool
882
883
{
883
884
if (!function_exists ($ handler )){
884
885
self ::$ logger ->error ("Handler function ' {$ handler }' not found " , [ self ::$ uri , self ::$ httpMethod , $ handler ]);
@@ -892,52 +893,6 @@ public static function checkFunctionExists($handler): bool
892
893
return true ;
893
894
}
894
895
895
- /**
896
- * Возвращает "внутреннее" имя замыкание, сгенерированное на основе таймштампа (до мс) и аргументов функции
897
- * Closure(LineStart-LineEnd)=аргумент1:аргумент2:аргумент3
898
- *
899
- * Или, если возникло исключение ReflectionException
900
- * Closure(<md5(1, 4096)>)=.
901
- *
902
- * @param $closure
903
- * @return string
904
- */
905
- public static function getClosureInternalName ($ closure ): string
906
- {
907
- $ name = "Closure( " ;
908
-
909
- try {
910
- $ reflected = new \ReflectionFunction ($ closure );
911
- $ args = implode (': ' ,
912
- // создаем статичную функцию и сразу вызываем
913
- (static function ($ r ) {
914
- return
915
- array_map (
916
- // обработчик
917
- static function ($ v )
918
- {
919
- return is_object ($ v ) ? $ v ->name : $ v ;
920
- },
921
- // входной массив
922
- array_merge (
923
- $ r ->getParameters (),
924
- array_keys (
925
- $ r ->getStaticVariables ()
926
- )
927
- )
928
- );
929
- })
930
- ($ reflected ) // а это её аргумент
931
- ); // эта скобочка относится к implode
932
- // "value:value2:s1:s2"
933
- $ name .= $ reflected ->getStartLine () . "- " . $ reflected ->getEndLine () . ")= " . $ args ;
934
- } catch (\ReflectionException $ e ) {
935
- $ name .= md5 (mt_rand (1 , PHP_MAXPATHLEN )) . ")=. " ;
936
- }
937
-
938
- return $ name ;
939
- }
940
-
941
896
/**
942
897
* Компилирует хэндлер из строчки, замыкания или массива [класс, метод] в действующий хэндлер
943
898
* с отловом ошибок несуществования роута
@@ -946,7 +901,7 @@ static function($v)
946
901
* @param bool $is_middleware
947
902
* @return array|\Closure|string
948
903
*/
949
- public static function compileHandler ($ handler , bool $ is_middleware = false )
904
+ private static function compileHandler ($ handler , bool $ is_middleware = false ): array | \ Closure | string
950
905
{
951
906
if (empty ($ handler )) {
952
907
return [];
@@ -987,7 +942,7 @@ public static function compileHandler($handler, bool $is_middleware = false)
987
942
if (is_string ($ handler ) && str_contains ($ handler , '@ ' )) {
988
943
// 'Class@method'
989
944
990
- list ($ class , $ method ) = self ::explode ($ handler , [null , '__invoke ' ], '@ ' );
945
+ list ($ class , $ method ) = Helper ::explode ($ handler , [null , '__invoke ' ], '@ ' );
991
946
self ::checkClassExists ($ class );
992
947
self ::checkMethodExists ($ class , $ method );
993
948
@@ -1037,31 +992,13 @@ public static function compileHandler($handler, bool $is_middleware = false)
1037
992
return $ handler ;
1038
993
}
1039
994
1040
-
1041
- /**
1042
- * Выполняет explode строки роута с учетом дефолтной маски
1043
- * Заменяет list($a, $b) = explode(separator, string) с дефолтными значениями элементов
1044
- * Хотел назвать это replace_array_callback(), но передумал
1045
- *
1046
- * @param $income
1047
- * @param array $default
1048
- * @param string $separator
1049
- * @return array
1050
- */
1051
- private static function explode ($ income , array $ default = [ null , '__invoke ' ], string $ separator = '@ ' ): array
1052
- {
1053
- return array_map (static function ($ first , $ second ) {
1054
- return empty ($ second ) ? $ first : $ second ;
1055
- }, $ default , \explode ($ separator , $ income ));
1056
- }
1057
-
1058
995
/**
1059
996
* Experimental - use only if str_contains('{')
1060
997
*
1061
998
* @param $route
1062
999
* @return array|string|string[]|null
1063
1000
*/
1064
- private static function applyAliases ($ route )
1001
+ private static function applyAliases ($ route ): array | string | null
1065
1002
{
1066
1003
return preg_replace_callback ('/\{(\w+)\}/ ' , function ($ matches ) {
1067
1004
$ paramName = $ matches [1 ];
@@ -1073,11 +1010,7 @@ private static function applyAliases($route)
1073
1010
}
1074
1011
1075
1012
/**
1076
- * Experimental - addAlias
1077
- *
1078
- * @param array|string $name
1079
- * @param string|null $regexp
1080
- * @return void
1013
+ * @inheritDoc
1081
1014
*/
1082
1015
public static function addAlias (array |string $ name , ?string $ regexp = null ): void
1083
1016
{
@@ -1091,9 +1024,7 @@ public static function addAlias(array|string $name, ?string $regexp = null): voi
1091
1024
}
1092
1025
1093
1026
/**
1094
- * Experimental: возвращает список алиасов
1095
- *
1096
- * @return array
1027
+ * @inheritDoc
1097
1028
*/
1098
1029
public static function getAliases ():array
1099
1030
{
@@ -1102,4 +1033,4 @@ public static function getAliases():array
1102
1033
1103
1034
}
1104
1035
1105
- # -eof-
1036
+ # -eof- #
0 commit comments