Skip to content

Commit

Permalink
Merge pull request #62 from memiah/master
Browse files Browse the repository at this point in the history
Ability to include an external file as overview section.
  • Loading branch information
from2day authored Dec 5, 2017
2 parents ffeb6bc + 0374ae7 commit 1dc93b8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: php

dist: trusty

php:
- 5.5.9
- 5.5
Expand Down
36 changes: 32 additions & 4 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ protected function registerAnnotationLoader()
}

/**
* Generate documentation with the name and version.
* Generate documentation with the name, version and optional overview content.
*
* @param \Illuminate\Support\Collection $controllers
* @param string $name
* @param string $version
* @param string $includePath
* @param string $overviewFile
*
* @return bool
*/
public function generate(Collection $controllers, $name, $version, $includePath = null)
public function generate(Collection $controllers, $name, $version, $includePath = null, $overviewFile = null)
{
$this->includePath = $includePath;

Expand Down Expand Up @@ -111,7 +112,7 @@ public function generate(Collection $controllers, $name, $version, $includePath
return new RestResource($controller->getName(), $controller, $annotations, $actions);
});

$contents = $this->generateContentsFromResources($resources, $name);
$contents = $this->generateContentsFromResources($resources, $name, $overviewFile);

$this->includePath = null;

Expand All @@ -123,17 +124,19 @@ public function generate(Collection $controllers, $name, $version, $includePath
*
* @param \Illuminate\Support\Collection $resources
* @param string $name
* @param string $overviewFile
*
* @return string
*/
protected function generateContentsFromResources(Collection $resources, $name)
protected function generateContentsFromResources(Collection $resources, $name, $overviewFile = null)
{
$contents = '';

$contents .= $this->getFormat();
$contents .= $this->line(2);
$contents .= sprintf('# %s', $name);
$contents .= $this->line(2);
$contents .= $this->getOverview($overviewFile);

$resources->each(function ($resource) use (&$contents) {
if ($resource->getActions()->isEmpty()) {
Expand Down Expand Up @@ -454,4 +457,29 @@ protected function getFormat()
{
return 'FORMAT: 1A';
}

/**
* Get the overview file content to append.
*
* @param null $file
* @return null|string
*/
protected function getOverview($file = null)
{
if (null !== $file) {
if (!file_exists($file)) {
throw new RuntimeException('Overview file could not be found.');
}

$content = file_get_contents($file);

if ($content === false) {
throw new RuntimeException('Failed to read overview file contents.');
}

return $content.$this->line(2);
}

return null;
}
}
23 changes: 23 additions & 0 deletions tests/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,27 @@ public function testGeneratingSimpleBlueprints()

$this->assertEquals(trim($expected), $blueprint->generate($resources, 'testing', 'v1', null));
}

public function testGeneratingBlueprintOverview()
{
$resources = new Collection([new Stubs\ActivityController]);

$blueprint = new Blueprint(new SimpleAnnotationReader, new Filesystem);

$expected = <<<'EOT'
FORMAT: 1A
# testing
Overview content here.
# Activity
## Show all activities. [GET /activity]
EOT;

$this->assertEquals(trim($expected), $blueprint->generate($resources, 'testing', 'v1', null, __DIR__.'/Files/overview.apib'));


}
}
1 change: 1 addition & 0 deletions tests/Files/overview.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Overview content here.

0 comments on commit 1dc93b8

Please sign in to comment.