Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
phpunit.xml
vendor
.idea
79 changes: 79 additions & 0 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions tests/LaravelIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down