Skip to content

Commit

Permalink
Ability to include an external file as the blueprint overview.
Browse files Browse the repository at this point in the history
  • Loading branch information
memiah-steve committed Jun 29, 2017
1 parent 690bdf0 commit 990ed7c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function registerAnnotationLoader()
*
* @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 +111,7 @@ public function generate(Collection $controllers, $name, $version, $includePath
return new Resource($controller->getName(), $controller, $annotations, $actions);
});

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

$this->includePath = null;

Expand All @@ -123,17 +123,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 +456,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 990ed7c

Please sign in to comment.