Skip to content

Commit 2d3d8fc

Browse files
committed
code improved
1 parent 025de68 commit 2d3d8fc

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

src/RouterBuilder.php

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,44 @@ final class RouterBuilder
2727
{
2828

2929
/**
30-
* @var null|ContainerInterface
30+
* @var ContainerInterface|null
3131
*/
3232
private $container = null;
3333

3434
/**
35-
* @var null|CacheInterface
35+
* @var CacheInterface|null
3636
*/
3737
private $cache = null;
3838

3939
/**
40-
* @var null|array<string, string[]>
40+
* @var string|null
41+
*/
42+
private $cacheKey = null;
43+
44+
/**
45+
* @var array<string, string[]>|null
4146
*/
4247
private $hosts = null;
4348

4449
/**
45-
* @var null|MiddlewareInterface[]
50+
* @var MiddlewareInterface[]|null
4651
*/
4752
private $middlewares = null;
4853

4954
/**
50-
* @var null|Loader\CollectableFileLoader
55+
* @var Loader\ConfigLoader|null
5156
*/
5257
private $configLoader = null;
5358

5459
/**
55-
* @var null|Loader\DescriptorDirectoryLoader
60+
* @var Loader\DescriptorLoader|null
5661
*/
57-
private $metadataLoader = null;
62+
private $descriptorLoader = null;
5863

5964
/**
60-
* @param null|ContainerInterface $container
65+
* Sets the given container to the builder
66+
*
67+
* @param ContainerInterface|null $container
6168
*
6269
* @return self
6370
*/
@@ -69,7 +76,9 @@ public function setContainer(?ContainerInterface $container) : self
6976
}
7077

7178
/**
72-
* @param null|CacheInterface $cache
79+
* Sets the given cache to the builder
80+
*
81+
* @param CacheInterface|null $cache
7382
*
7483
* @return self
7584
*/
@@ -81,33 +90,71 @@ public function setCache(?CacheInterface $cache) : self
8190
}
8291

8392
/**
93+
* Sets the given cache key to the builder
94+
*
95+
* @param string|null $cacheKey
96+
*
97+
* @return self
98+
*
99+
* @since 2.10.0
100+
*/
101+
public function setCacheKey(?string $cacheKey) : self
102+
{
103+
$this->cacheKey = $cacheKey;
104+
105+
return $this;
106+
}
107+
108+
/**
109+
* Uses the config loader when building
110+
*
84111
* @param string[] $resources
85112
*
86113
* @return self
87114
*/
88115
public function useConfigLoader(array $resources) : self
89116
{
90-
$this->configLoader = new Loader\CollectableFileLoader();
117+
$this->configLoader = new Loader\ConfigLoader();
91118
$this->configLoader->attachArray($resources);
92119

93120
return $this;
94121
}
95122

96123
/**
124+
* Uses the descriptor loader when building
125+
*
126+
* @param string[] $resources
127+
*
128+
* @return self
129+
*/
130+
public function useDescriptorLoader(array $resources) : self
131+
{
132+
$this->descriptorLoader = new Loader\DescriptorLoader();
133+
$this->descriptorLoader->attachArray($resources);
134+
135+
return $this;
136+
}
137+
138+
/**
139+
* Uses the metadata loader when building
140+
*
141+
* Alias to the useDescriptorLoader method.
142+
*
97143
* @param string[] $resources
98144
*
99145
* @return self
100146
*/
101147
public function useMetadataLoader(array $resources) : self
102148
{
103-
$this->metadataLoader = new Loader\DescriptorDirectoryLoader();
104-
$this->metadataLoader->attachArray($resources);
149+
$this->useDescriptorLoader($resources);
105150

106151
return $this;
107152
}
108153

109154
/**
110-
* @param null|array<string, string[]> $hosts
155+
* Sets the given hosts to the builder
156+
*
157+
* @param array<string, string[]>|null $hosts
111158
*
112159
* @return self
113160
*/
@@ -119,7 +166,9 @@ public function setHosts(?array $hosts) : self
119166
}
120167

121168
/**
122-
* @param null|MiddlewareInterface[] $middlewares
169+
* Sets the given middlewares to the builder
170+
*
171+
* @param MiddlewareInterface[]|null $middlewares
123172
*
124173
* @return self
125174
*/
@@ -131,6 +180,8 @@ public function setMiddlewares(?array $middlewares) : self
131180
}
132181

133182
/**
183+
* Builds the router
184+
*
134185
* @return Router
135186
*/
136187
public function build() : Router
@@ -142,10 +193,11 @@ public function build() : Router
142193
$router->load($this->configLoader);
143194
}
144195

145-
if (isset($this->metadataLoader)) {
146-
$this->metadataLoader->setContainer($this->container);
147-
$this->metadataLoader->setCache($this->cache);
148-
$router->load($this->metadataLoader);
196+
if (isset($this->descriptorLoader)) {
197+
$this->descriptorLoader->setContainer($this->container);
198+
$this->descriptorLoader->setCache($this->cache);
199+
$this->descriptorLoader->setCacheKey($this->cacheKey);
200+
$router->load($this->descriptorLoader);
149201
}
150202

151203
if (!empty($this->hosts)) {

0 commit comments

Comments
 (0)