@@ -47,11 +47,11 @@ class RouterManager
4747 * @var array[]
4848 * [
4949 * 'default' => 'main-site', // this is default router.
50- *
5150 * 'main-site' => [
5251 * 'driver' => 'default',
5352 * 'conditions' => [
54- * 'domain' => 'domain.com',
53+ * 'domains' => 'abc.com',
54+ * 'schemes' => ['https'],
5555 * ],
5656 * 'options' => [
5757 * // some setting for router.
@@ -61,7 +61,7 @@ class RouterManager
6161 * 'doc-site' => [
6262 * 'driver' => 'cached',
6363 * 'conditions' => [
64- * 'domain ' => 'docs.domain .com',
64+ * 'domains ' => 'docs.abc .com',
6565 * ],
6666 * 'options' => [
6767 * 'cacheFile' => '/path/to/routes-cache.php',
@@ -75,12 +75,29 @@ class RouterManager
7575 /**
7676 * @var array
7777 * [
78- * 'condition ID' => 'name',
79- * 'condition ID1' => 'main-site'
78+ * 'main-site' => [
79+ * 'domains' => 'abc.com',
80+ * 'schemes' => ['https'],
81+ * ],
82+ * 'doc-site' => [
83+ * 'domains' => 'docs.abc.com',
84+ * 'schemes' => ['https'],
85+ * ],
86+ * 'th3-site' => [
87+ * 'domains' => 'th3.abc.com',
88+ * ],
8089 * ]
8190 */
8291 private $ conditions = [];
8392
93+ /**
94+ * @var array
95+ */
96+ private $ supportedConditions = [
97+ 'domains ' => 'domain ' ,
98+ 'schemes ' => 'scheme ' ,
99+ ];
100+
84101 /**
85102 * @var array
86103 */
@@ -112,23 +129,61 @@ public function __construct(array $configs = [])
112129
113130 /**
114131 * get router by condition
115- * @param array $conditions
132+ * @param array|string $condition
133+ * array:
116134 * [
117- * 'domain' => 'domain.com'
135+ * 'domain' => 'abc.com',
136+ * 'scheme' => 'https',
118137 * ]
138+ * string:
139+ * get by name. same of call getByName()
119140 * @return AbstractRouter|RouterInterface
120141 * @throws \InvalidArgumentException
121142 */
122- public function get ($ conditions = null ): AbstractRouter
143+ public function get ($ condition = null ): AbstractRouter
123144 {
124- if (!$ conditions ) {
145+ if (!$ condition ) {
125146 return $ this ->getDefault ();
126147 }
127148
128- $ key = $ this ->genConditionID ($ conditions );
129- $ name = $ this ->conditions [$ key ] ?? self ::DEFAULT_ROUTER ;
149+ // alias of getByName()
150+ if (\is_string ($ condition )) {
151+ return $ this ->getByName ($ condition );
152+ }
153+
154+ $ useName = self ::DEFAULT_ROUTER ;
155+
156+ foreach ($ this ->conditions as $ name => $ cond ) {
157+ if ($ this ->compareArray ($ cond , $ condition )) {
158+ $ useName = $ name ;
159+ break ;
160+ }
161+ }
130162
131- return $ this ->getByName ($ name );
163+ return $ this ->getByName ($ useName );
164+ }
165+
166+ /**
167+ * @param array $define
168+ * @param array $input
169+ * @return bool
170+ */
171+ protected function compareArray (array $ define , array $ input ): bool
172+ {
173+ $ match = true ;
174+
175+ foreach ($ this ->supportedConditions as $ def => $ key ) {
176+ if (isset ($ define [$ def ], $ input [$ key ])) {
177+ $ defValues = (array )$ define [$ def ];
178+
179+ if (!\in_array ($ input [$ key ], $ defValues , true )) {
180+ $ match = false ;
181+ break ;
182+ }
183+ }
184+ }
185+
186+ return $ match ;
132187 }
133188
134189 /**
@@ -158,7 +213,7 @@ public function getByName(string $name): AbstractRouter
158213 $ config = $ this ->configs [$ config ];
159214 }
160215
161- return ($ this ->routers [$ name ] = $ this ->createRouter ($ config ));
216+ return ($ this ->routers [$ name ] = $ this ->createRouter ($ config, $ name ));
162217 }
163218
164219 /**
@@ -169,21 +224,13 @@ public function getDefault(): AbstractRouter
169224 return $ this ->getByName (self ::DEFAULT_ROUTER );
170225 }
171226
172- /**
173- * @param string|array $conditions
174- * @return string
175- */
176- private function genConditionID ($ conditions ): string
177- {
178- return \md5 (\is_array ($ conditions ) ? \json_encode ($ conditions ) : $ conditions );
179- }
180-
181227 /**
182228 * @param array $config
229+ * @param string $name
183230 * @return AbstractRouter
184231 * @throws \InvalidArgumentException
185232 */
186- private function createRouter (array $ config ): AbstractRouter
233+ private function createRouter (array $ config, string $ name = '' ): AbstractRouter
187234 {
188235 $ driver = $ config ['driver ' ] ?? self ::DEFAULT_ROUTER ;
189236 $ options = $ config ['options ' ] ?? [];
@@ -192,6 +239,10 @@ private function createRouter(array $config): AbstractRouter
192239 throw new \InvalidArgumentException ("The router driver name ' $ driver' does not exists! " );
193240 }
194241
242+ if ($ name && !isset ($ options ['name ' ])) {
243+ $ options ['name ' ] = $ name ;
244+ }
245+
195246 return new $ class ($ options );
196247 }
197248
@@ -237,8 +288,7 @@ public function setConfigs(array $configs)
237288
238289 foreach ($ configs as $ name => $ config ) {
239290 if (isset ($ config ['conditions ' ])) {
240- $ key = $ this ->genConditionID ($ config ['conditions ' ]);
241- $ this ->conditions [$ key ] = $ name ;
291+ $ this ->conditions [$ name ] = $ config ['conditions ' ];
242292 }
243293 }
244294 }
0 commit comments