diff --git a/.gitignore b/.gitignore index 81b9258..86d9b11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock phpunit.xml vendor +.idea \ No newline at end of file diff --git a/src/Blueprint.php b/src/Blueprint.php index 65c861e..318de5a 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -147,6 +147,8 @@ protected function generateContentsFromResources(Collection $resources, $name, $ $typeDefinitions .= $this->line(2); $typeDefinitions .= $this->getOverview($overviewFile); + $this->appendTableOfContents($contents, $resources); + $resources->each(function ($resource) use (&$contents, &$typeDefinitions, $name) { if ($resource->getActions()->isEmpty()) { return; @@ -524,6 +526,83 @@ protected function appendSection(&$contents, $name, $indent = 0, $lines = 2, $pr $contents .= $prefix.$name; } + /** + * Append table of contents. + * + * @param string $contents + * @param \Illuminate\Support\Collection $resources + * + * @return void + */ + protected function appendTableOfContents(&$contents, Collection $resources) + { + $contents .= sprintf('## Table of contents'); + $contents .= $this->line(2); + + $resources->each(function ($resource) use (&$contents) { + if ($resource->getActions()->isEmpty()) { + return; + } + + $link = $this->slugify($resource->getUri()); + + if ($method = $resource->getMethod()) { + $link = $method . $link; + } + + $link = $this->slugify($resource->getIdentifier()) . '-' . ($link == '/' ? '' : $link.'-'); + + $contents .= '* [' . str_replace('# ', '', $resource->getDefinition()) . '](#' . $link . ')'; + + + $resource->getActions()->each(function ($action) use (&$contents, $resource) { + $link = $this->slugify($action->getMethod() . '-' . $action->getUri()); + $link = $this->slugify($action->getIdentifier()) . '-' . ($link == '/' ? '' : $link.'-'); + + $contents .= $this->line(); + $contents .= ' * '; + $contents .= '[' . str_replace('## ', '', $action->getDefinition()) . ']' . '(#' . $link . ')'; + }); + + + $contents .= $this->line(2); + }); + } + + /** + * Make a slug with the passed text. + * + * @param string $text + * + * @return string + */ + protected function slugify($text) + { + // replace non letter or digits by - + $text = preg_replace('~[^\pL\d]+~u', '-', $text); + + // transliterate + $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); + + // remove unwanted characters + $text = preg_replace('~[^-\w]+~', '', $text); + + // trim + $text = trim($text, '-'); + + // remove duplicate - + $text = preg_replace('~-+~', '-', $text); + + // lowercase + $text = strtolower($text); + + if (empty($text)) { + return 'n-a'; + } + + return $text; + } + /** * Prepare a body. * diff --git a/tests/BlueprintTest.php b/tests/BlueprintTest.php index 283b1ca..4a19d01 100644 --- a/tests/BlueprintTest.php +++ b/tests/BlueprintTest.php @@ -21,6 +21,13 @@ public function testGeneratingBlueprintForSingleResource() # testing +## Table of contents + +* [Users [/users]](#users-users-) + * [Show all users. [GET /users]](#show-all-users-get-users-) + * [Show existing user. [GET /users/{id}]](#show-existing-user-get-users-id-) + * [Create new user. [POST /users]](#create-new-user-post-users-) + # Users [/users] Users Resource. @@ -120,6 +127,17 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionOne() # testing +## Table of contents + +* [Users [/users]](#users-users-) + * [Show all users. [GET /users]](#show-all-users-get-users-) + * [Show existing user. [GET /users/{id}]](#show-existing-user-get-users-id-) + * [Create new user. [POST /users]](#create-new-user-post-users-) + +* [User Photos [/users/{userId}/photos]](#user-photos-users-userid-photos-) + * [Show all photos. [GET /users/{userId}/photos{?sort,order}]](#show-all-photos-get-users-userid-photos-sort-order-) + * [Upload new photo. [POST /users/{userId}/photos]](#upload-new-photo-post-users-userid-photos-) + # Users [/users] Users Resource. @@ -279,6 +297,19 @@ public function testGeneratingBlueprintForMultipleResourcesWithVersionTwo() # testing +## Table of contents + +* [Users [/users]](#users-users-) + * [Show all users. [GET /users]](#show-all-users-get-users-) + * [Show existing user. [GET /users/{id}]](#show-existing-user-get-users-id-) + * [Create new user. [POST /users]](#create-new-user-post-users-) + +* [User Photos [/users/{userId}/photos]](#user-photos-users-userid-photos-) + * [Show all photos. [GET /users/{userId}/photos{?sort,order}]](#show-all-photos-get-users-userid-photos-sort-order-) + * [Show individual photo. [GET /users/{userId}/photos/{photoId}]](#show-individual-photo-get-users-userid-photos-photoid-) + * [Upload new photo. [POST /users/{userId}/photos]](#upload-new-photo-post-users-userid-photos-) + * [Delete photo. [DELETE /users/{userId}/photos/{photoId}]](#delete-photo-delete-users-userid-photos-photoid-) + # Users [/users] Users Resource. @@ -480,6 +511,11 @@ public function testGeneratingSimpleBlueprints() # testing +## Table of contents + +* [Activity](#activity-n-a-) + * [Show all activities. [GET /activity]](#show-all-activities-get-activity-) + # Activity ## Show all activities. [GET /activity] @@ -501,6 +537,11 @@ public function testGeneratingBlueprintOverview() Overview content here. +## Table of contents + +* [Activity](#activity-n-a-) + * [Show all activities. [GET /activity]](#show-all-activities-get-activity-) + # Activity ## Show all activities. [GET /activity] diff --git a/tests/LaravelIntegrationTest.php b/tests/LaravelIntegrationTest.php index 0e4d3ca..ac97d95 100644 --- a/tests/LaravelIntegrationTest.php +++ b/tests/LaravelIntegrationTest.php @@ -53,6 +53,11 @@ class LaravelIntegrationTest extends PHPUnit_Framework_TestCase # testing +## Table of contents + +* [Activity](#activity-n-a-) + * [Show all activities. [GET /activity]](#show-all-activities-get-activity-) + # Activity ## Show all activities. [GET /activity]