65 private array $routeFiles;
70 private array $extraRoutes;
76 private ?array $routesFromFiles =
null;
79 private ?array $routeFileTimestamps =
null;
82 private ?
string $configHash =
null;
98 ObjectFactory $objectFactory,
111 $this->routeFiles = $routeFiles;
112 $this->extraRoutes = $extraRoutes;
121 if ( $this->configHash === null ) {
122 $this->configHash = md5( json_encode( [
123 'class' => __CLASS__,
125 'extraRoutes' => $this->extraRoutes,
126 'fileTimestamps' => $this->getRouteFileTimestamps()
129 return $this->configHash;
139 private function getRoutesFromFiles(): array {
140 if ( $this->routesFromFiles !== null ) {
141 return $this->routesFromFiles;
144 $this->routesFromFiles = [];
145 $this->routeFileTimestamps = [];
146 foreach ( $this->routeFiles as $fileName ) {
147 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
149 $routes = $this->loadJsonFile( $fileName );
151 $this->routesFromFiles = array_merge( $this->routesFromFiles, $routes );
154 return $this->routesFromFiles;
162 private function getRouteFileTimestamps(): array {
163 if ( $this->routeFileTimestamps === null ) {
164 $this->routeFileTimestamps = [];
165 foreach ( $this->routeFiles as $fileName ) {
166 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
169 return $this->routeFileTimestamps;
177 foreach ( $this->getAllRoutes() as $spec ) {
178 $key = $spec[
'path'];
180 $methods = isset( $spec[
'method'] ) ? (array)$spec[
'method'] : [
'GET' ];
182 $paths[$key] = array_merge( $paths[$key] ?? [], $methods );
191 private function getAllRoutes() {
192 $iterator =
new AppendIterator;
193 $iterator->append(
new ArrayIterator( $this->getRoutesFromFiles() ) );
194 $iterator->append(
new ArrayIterator( $this->extraRoutes ) );
199 $routeDefs = $this->getAllRoutes();
201 foreach ( $routeDefs as $route ) {
202 if ( !isset( $route[
'path'] ) ) {
206 $path = $route[
'path'];
207 $method = $route[
'method'] ??
'GET';
208 $info = $this->makeRouteInfo( $route );
210 $this->addRoute( $method,
$path, $info );
219 private function makeRouteInfo( array $route ): array {
220 static $objectSpecKeys = [
228 if ( isset( $route[
'redirect'] ) ) {
231 'spec' => [
'class' => RedirectHandler::class ],
237 'spec' => array_intersect_key( $route, array_flip( $objectSpecKeys ) ),
238 'config' => array_diff_key( $route, array_flip( $objectSpecKeys ) ),
242 $info[
'path'] = $route[
'path'];