diff --git a/README.md b/README.md index 338a8f5..4acaf7f 100755 --- a/README.md +++ b/README.md @@ -236,6 +236,17 @@ If your server does not have a default timezone set in php.ini, it may return er } ``` +###Inherit Index +This feature will insruct the router to seek the first available file to use when a request to a folder is made and the index is not found. + +```json +{ + "live": [ + "inherit_index": true + ] +} +``` + ###Multi-language Enables multi-language support which needs seperate directories for each language in `docs/` folder. diff --git a/docs/config.json b/docs/config.json index c3f20c2..745e912 100644 --- a/docs/config.json +++ b/docs/config.json @@ -8,6 +8,7 @@ "folders": ["99_Not_Ready"] }, "live": { + "inherit_index": true, "clean_urls": true }, "html": { diff --git a/global.json b/global.json index 7e7428a..84bf6a2 100644 --- a/global.json +++ b/global.json @@ -19,6 +19,7 @@ "timezone": "America/Los_Angeles", "live": { + "inherit_index": false, "clean_urls": false }, diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php index a994b79..04c8580 100644 --- a/libs/Tree/Directory.php +++ b/libs/Tree/Directory.php @@ -54,9 +54,46 @@ class Directory extends Entry return $this->children[$index_key]; } + /* + If the inherit_index flag is set, then we seek child content + */ + if( + !empty($this->getConfig()['live']['inherit_index']) + && $first_page = $this->seekFirstPage() + ){ + return $first_page; + } + return null; } + /** + * Seek the first available page from descendants + * @return Content|null + */ + public function seekFirstPage(){ + if( $this instanceof Directory ){ + $index_key = $this->getConfig()['index_key']; + if (isset($this->children[$index_key])) { + return $this->children[$index_key]; + } + foreach( $this->children AS $node_key => $node ){ + if( $node instanceof Content ){ + return $node; + } + if( + $node instanceof Directory + && strpos($node->getUri(), '.') !== 0 + ){ + if( $node->seekFirstPage() ){ + return $node->seekFirstPage(); + } + } + } + } + return null; + } + /** * @return Content|null */