Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group notation - #33 improvement #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
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
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Some index method phpdoc example:
/**
* Products
*
* @Group("Products")
* @Versions({"v1"})
* @Resource("Products", uri="/products")
*/
class ProductsController extends ApiController
Expand Down
4 changes: 3 additions & 1 deletion src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function getDefinition()
$definition = $identifier.' ['.$definition.']';
}

return '## '.$definition;
$level = $this->resource->getGroupIdentifier() ? '### ' : '## ';

return $level.$definition;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Annotation/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Dingo\Blueprint\Annotation;

/**
* @Annotation
*/
class Group
{
/**
* @var string
*/
public $identifier;
}
126 changes: 80 additions & 46 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function generate(Collection $controllers, $name, $version, $includePath
{
$this->includePath = $includePath;

$resources = $controllers->map(function ($controller) use ($version) {
$groups = new Collection;

$resources = $controllers->map(function ($controller) use ($version, $groups) {
$controller = $controller instanceof ReflectionClass ? $controller : new ReflectionClass($controller);

$actions = new Collection;
Expand All @@ -108,25 +110,48 @@ public function generate(Collection $controllers, $name, $version, $includePath

$annotations = new Collection($this->reader->getClassAnnotations($controller));

return new Resource($controller->getName(), $controller, $annotations, $actions);
$versions = $annotations->filter(function($item) {
return $item instanceof Annotation\Versions;
})->first();

// if the group does not have the intended version, the group is skipped
if( isset($versions->value) && in_array($version, $versions->value) )
{
$resource = new Resource($controller->getName(), $controller, $annotations, $actions);

// Find or create group and add resource to it
$group = $groups->where('identifier', $resource->getGroupIdentifier())->first() ?: new Group($resource);
$group->addResource($resource);
if (! $resource->getGroupIdentifier()) {
// Prepend resource group with no identifier so this group appears first
$groups->contains($group) ?: $groups->prepend($group);
} else {
$groups->contains($group) ?: $groups->push($group);
}

return $resource;
}


return false;
});

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

$this->includePath = null;

return $contents;
}

/**
* Generate the documentation contents from the resources collection.
* Generate the documentation contents from the groups collection.
*
* @param \Illuminate\Support\Collection $resources
* @param \Illuminate\Support\Collection $groups
* @param string $name
*
* @return string
*/
protected function generateContentsFromResources(Collection $resources, $name)
protected function generateContentsFromGroups(Collection $groups, $name)
{
$contents = '';

Expand All @@ -135,61 +160,70 @@ protected function generateContentsFromResources(Collection $resources, $name)
$contents .= sprintf('# %s', $name);
$contents .= $this->line(2);

$resources->each(function ($resource) use (&$contents) {
if ($resource->getActions()->isEmpty()) {
return;
}

$contents .= $resource->getDefinition();
$groups->each(function ($group) use (&$contents) {

if ($description = $resource->getDescription()) {
$contents .= $this->line();
$contents .= $description;
if ($group->getIdentifier()) {
$contents .= $group->getDefinition();
$contents .= $this->line(2);
}

if (($parameters = $resource->getParameters()) && ! $parameters->isEmpty()) {
$this->appendParameters($contents, $parameters);
}
$group->getResources()->each(function ($resource) use (&$contents) {

$resource->getActions()->each(function ($action) use (&$contents, $resource) {
$contents .= $this->line(2);
$contents .= $action->getDefinition();
if ($resource->getActions()->isEmpty()) {
return;
}

$contents .= $resource->getDefinition();

if ($description = $action->getDescription()) {
if ($description = $resource->getDescription()) {
$contents .= $this->line();
$contents .= $description;
}

if (($attributes = $action->getAttributes()) && ! $attributes->isEmpty()) {
$this->appendAttributes($contents, $attributes);
}

if (($parameters = $action->getParameters()) && ! $parameters->isEmpty()) {
if (($parameters = $resource->getParameters()) && ! $parameters->isEmpty()) {
$this->appendParameters($contents, $parameters);
}

if ($request = $action->getRequest()) {
$this->appendRequest($contents, $request, $resource);
}
$resource->getActions()->each(function ($action) use (&$contents, $resource) {
$contents .= $this->line(2);
$contents .= $action->getDefinition();

if ($response = $action->getResponse()) {
$this->appendResponse($contents, $response, $resource);
}
if ($description = $action->getDescription()) {
$contents .= $this->line();
$contents .= $description;
}

if (($attributes = $action->getAttributes()) && ! $attributes->isEmpty()) {
$this->appendAttributes($contents, $attributes);
}

if (($parameters = $action->getParameters()) && ! $parameters->isEmpty()) {
$this->appendParameters($contents, $parameters);
}

if ($transaction = $action->getTransaction()) {
foreach ($transaction->value as $value) {
if ($value instanceof Annotation\Request) {
$this->appendRequest($contents, $value, $resource);
} elseif ($value instanceof Annotation\Response) {
$this->appendResponse($contents, $value, $resource);
} else {
throw new RuntimeException('Unsupported annotation type given in transaction.');
if ($request = $action->getRequest()) {
$this->appendRequest($contents, $request, $resource);
}

if ($response = $action->getResponse()) {
$this->appendResponse($contents, $response, $resource);
}

if ($transaction = $action->getTransaction()) {
foreach ($transaction->value as $value) {
if ($value instanceof Annotation\Request) {
$this->appendRequest($contents, $value, $resource);
} elseif ($value instanceof Annotation\Response) {
$this->appendResponse($contents, $value, $resource);
} else {
throw new RuntimeException('Unsupported annotation type given in transaction.');
}
}
}
}
});
});

$contents .= $this->line(2);
$contents .= $this->line(2);
});
});

return stripslashes(trim($contents));
Expand Down Expand Up @@ -405,7 +439,7 @@ protected function prepareBody($body, $contentType)
$path .= '.json';
}

$body = $this->files->get($this->includePath.'/'.$path);
$body = $this->files->get($includePath.'/'.$path);

json_decode($body);

Expand Down Expand Up @@ -454,4 +488,4 @@ protected function getFormat()
{
return 'FORMAT: 1A';
}
}
}
79 changes: 79 additions & 0 deletions src/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Dingo\Blueprint;

use Illuminate\Support\Collection;

class Group extends Section
{
/**
* Group identifier.
*
* @var string
*/
public $identifier = null;

/**
* Collection of resources belonging to group.
*
* @var array
*/
protected $resources;

/**
* Create a new group instance.
*
* @param \Dingo\Blueprint\Resource $resource
*
* @return void
*/
public function __construct(Resource $resource)
{
$this->resources = new Collection([$resource]);
$this->identifier = $resource->getGroupIdentifier();
}

/**
* Get the group identifier.
*
* @return string|null
*/
public function getIdentifier()
{
return $this->identifier;
}

/**
* Add resource to the group.
*
* @param \Dingo\Blueprint\Resource $resource
*
* @return void
*/
public function addResource(Resource $resource)
{
$this->resources->contains($resource) ?: $this->resources->push($resource);
}

/**
* Get the resources belonging to the group.
*
* @return \Illuminate\Support\Collection
*/
public function getResources()
{
return $this->resources;
}

/**
* Get the group definition.
*
* @return string
*/
public function getDefinition()
{
if ($this->identifier) {
return '# Group '.$this->identifier;
}
}
}
31 changes: 27 additions & 4 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use ReflectionClass;
use phpDocumentor\Reflection\DocBlock;

class Resource extends Section
{
Expand Down Expand Up @@ -94,7 +95,31 @@ public function getDefinition()
$definition = $method.' '.$definition;
}

return '# '.$this->getIdentifier().($definition == '/' ? '' : ' ['.$definition.']');
$level = $this->getGroupIdentifier() ? '## ' : '# ';

return $level.$this->getIdentifier().($definition == '/' ? '' : ' ['.$definition.']');
}

/**
* Get the resource group annotation.
*
* @return \Dingo\Blueprint\Annotation\Group
*/
public function getGroupAnnotation()
{
return $this->getAnnotationByType('Group');
}

/**
* Get the resource group.
*
* @return string
*/
public function getGroupIdentifier()
{
if (($annotation = $this->getGroupAnnotation()) && isset($annotation->identifier)) {
return $annotation->identifier;
}
}

/**
Expand Down Expand Up @@ -142,9 +167,7 @@ public function getDescription()
{
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$docblock = $factory->create($this->reflector);

$text = $docblock->getSummary().$docblock->getDescription();

return $text;
}

Expand Down Expand Up @@ -209,4 +232,4 @@ public function getResponseHeaders()

return $this->responseHeaders;
}
}
}
Loading