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 new information to LRD #365

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
4c049d9
add phpDocumentor package to extract doc blocks
May 26, 2024
61954bb
add rulesOrder for ordering the display of rules
May 26, 2024
f6c64c0
adds new properties to the component
May 26, 2024
e7c120d
changes display of field rules and adds description and examples to f…
May 26, 2024
205fce7
add title to route grouping and summary e description to route
May 26, 2024
3be2902
change menu width
May 26, 2024
1d06ede
add title to route group
May 26, 2024
4ef501c
add new params
May 26, 2024
5cecb13
change css properties
May 26, 2024
059f297
Update readme
May 26, 2024
e13b3bb
Validate controller without docBlock
May 27, 2024
bd945b6
Insert new values in the mock response
May 28, 2024
d0631de
Change the parse of LRDtags
May 28, 2024
0580b88
Merge branch 'refs/heads/develop'
May 29, 2024
35c404f
Resolve conflict and lint
May 29, 2024
08450aa
Adjust lint
May 29, 2024
94c55bc
Adjust lint
May 29, 2024
fe09ae6
(ci) removed php 7.4 from yml
kevincobain2000 May 30, 2024
5f445fa
add phpDocumentor package to extract doc blocks
May 26, 2024
41fd5ae
add rulesOrder for ordering the display of rules
May 30, 2024
b04297b
adds new properties to the component
May 26, 2024
0fd985f
changes display of field rules and adds description and examples to f…
May 26, 2024
d2a7ad4
add title to route grouping and summary e description to route
May 26, 2024
e9bc970
change menu width
May 26, 2024
1d153a3
add title to route group
May 26, 2024
70f6c10
add new params
May 26, 2024
a03aeb4
change css properties
May 26, 2024
5f0fdd0
Update readme
May 26, 2024
fe0867e
Validate controller without docBlock
May 27, 2024
b9b4853
Insert new values in the mock response
May 28, 2024
17531cc
Change the parse of LRDtags
May 28, 2024
ee6edf3
Resolve conflict and lint
May 29, 2024
385665e
Adjust lint
May 29, 2024
e08679a
Adjust lint
May 29, 2024
d91436d
Merge remote-tracking branch 'origin/master'
May 30, 2024
2934117
Clears tag value on each iteration
Jun 3, 2024
c0dbecc
Adjust lint
Jun 3, 2024
8a8f6ed
Change tags to tag
Jun 4, 2024
1b0f798
Translate readme
Jun 4, 2024
498385d
Adjust code style
Jun 4, 2024
0fab236
Adjust try catch
Jun 5, 2024
75f1221
Adjust code style
Jun 5, 2024
349075c
Adjust plural/singular
Jun 5, 2024
8c2833f
Create tests and adjust code
Jun 8, 2024
c82b614
adjust to maintain compatibility
Jun 9, 2024
d1464e7
Adjust url
Jun 10, 2024
ff9bef7
Add Plural for OpenAPI
Jun 11, 2024
721a085
Validate boolean
Jun 11, 2024
0356b3c
Update readme
Jun 11, 2024
eb22bf4
Adjust lint
Jun 11, 2024
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
4 changes: 1 addition & 3 deletions .github/workflows/phptest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
php: [7.4, 8.0, 8.1, 8.2, 8.3]
php: [8.0, 8.1, 8.2, 8.3]
laravel: ['8.*', '11.*']
include:
- php: 8.0
Expand All @@ -28,8 +28,6 @@ jobs:
- php: '8.2'
laravel: 11.*
exclude:
- laravel: 11.*
php: 7.4
- laravel: 11.*
php: 8.0
- laravel: 11.*
Expand Down
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ Example of using it in the controller:
{
```

# Route grouping
You can add a `@LRDtag` parameter to the description of your controller to group the routes on the dashboard. Endpoint grouping is still done by routes and these tag will only be used as a friendly name for viewing the dashboard.
@LRDTags is used to generate documentation in OpenAPI format
piovezanfernando marked this conversation as resolved.
Show resolved Hide resolved
Example of using it in the controller:

```php
/**
* Class DemoController
* @package App\Http\Controllers
* @LRDtag Demonstration
* Demonstration management endpoint
*/
class DemoController extends Controller
{
```

# Endpoint summary and description

The LRD extracts the summary and description of the PHPDoc endpoint from the controller method, this information is rendered as a summary and description of the routes in the LRD dashboard.
To extract this information, the standard established by Laravel's PHPDoc is used, where the first line of the PHPDoc is the summary and the rest of the PHPDoc is the description until a tag with @ [PHPDoc documentation ](https://docs.phpdoc.org/3.0/guide/references/phpdoc/basic-syntax.html#:~:text=param%20and%20%40return.-,Summary,-The%20summary%20is).
Summary and description are used to generate documentation in OpenAPI format

```php
/**
* Summary of method.
* Description of method
* @param MyIndexRequest $request
*/
public function index(MyIndexRequest $request): Resource
{
```

# Extra parameters

You define extra parameters using `@LRDparam`.\
Expand All @@ -178,6 +210,61 @@ So, the precedence is `Controller method PHPDoc` < `Rules method PHPDoc` < `Rule
public function index(MyIndexRequest $request): Resource
{
```
# Rules and field description and example
To obtain a description of the fields and rules you can add a `fieldDescriptions()` method to your `formRequest` and return an associative array where the key is the name of the field and the value is the description and example of the field.
This method is not required, if it is not found, the descriptions will not be added.
The examples are only used for documentation and are not used for sending the request.
Descriptions and examples are used to generate documentation in OpenAPI format
```php
class UserRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required|string|max:50',
'password' => 'string|min:8|max:100|',
'remember' => 'required|boolean|required_if',
];
}

/**
* Provides a detailed description of the expected parameters
* in the body of an HTTP request.
*/
public function fieldDescriptions(): array
{
return [
'name' => 'Name of user'
'password' => 'The password of the user being created',
'remember' => 'Whether or not the user should be reminded'
];
}
```

# Rule order
The order of the rules that will be displayed in the documentation will be changed to the rule registered in the rules method as a way of maintaining the display standard. If not informed, the display rule will be the same as the `rules` method.
It is recommended that `nullable` be inserted at the end of the rules to correctly present the description `or null`

```php
'rules_order' => [
'required',
'required_if',
'file',
'integer',
'string',
'bool',
'date',
'file',
'image',
'array',
'min',
'max',
'nullable',
]
```

# Response codes
Without explicitly declaring response codes,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"illuminate/contracts": "^8.37|^9.0|^10.0|^11.0",
"kitloong/laravel-app-logger": "^1.0",
"spatie/laravel-package-tools": "^1.4.3",
"ext-json": "*"
"ext-json": "*",
"phpdocumentor/reflection-docblock": "^5.4.1"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.12|dev-master",
Expand Down
23 changes: 23 additions & 0 deletions config/request-docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,28 @@
'exclude_fields' => [
],

// Use factory to create data examples
'use_factory' => false,

// Regex to extract the model name from the controller name
'pattern_model_from_controller_name' => '/APIController$/',

// Path where the project's factories are located
'factory_path' => 'Database\Factories',

'rules_order' => [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I know why do we need to add this ordering?

If the user want to decided the ordering, can't they define from the rules ?

'page' => 'nullable|integer|min:1',

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition can only be done through the format that the operator defines in the rule as you posted, but when winning as a team the rules are not always defined in the same order so we don't have a standard and at least in my opinion it looks messy in documents like In the print below, I added this option simply to organize the rules cohesively but there is still the possibility of not passing the ordering and thus returning as defined in the method
image

'required',
'required_if',
'file',
'integer',
'string',
'bool',
'date',
'file',
'image',
'array',
'min',
'max',
'nullable',
]
];
149 changes: 136 additions & 13 deletions src/Doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Doc implements Arrayable
private array $rules;

/**
* The parsed validation rules.
* Examples generated by factories.
*
* @var array<string, string[]>
*/
Expand Down Expand Up @@ -113,16 +113,57 @@ class Doc implements Arrayable
private int $groupIndex;

/**
* @param string $uri
* @param string[] $methods
* @param string[] $middlewares
* @param string $controller
* @param string $controllerFullPath
* @param string $method
* @param string $httpMethod
* @param array<string, string[]> $rules
* @param string $docBlock
* Description list and examples for fields.
*
* @var array<string, string[]>
*/
private array $fieldInfo;

/**
* Rules order.
*
* @var array<string>
*/
private array $rulesOrder;

/**
* Endpoint summary.
*
* @var string
*/
private string $summary;

/**
* Endpoint description.
*
* @var string
*/
private string $description;

/**
* Endpoint tag.
*
* @var string
*/
private string $tag;

/**
* @param string $uri
* @param string[] $methods
* @param string[] $middlewares
* @param string $controller
* @param string $controllerFullPath
* @param string $method
* @param string $httpMethod
* @param array $pathParameters
* @param array<string, string[]> $rules
* @param string $docBlock
* @param array<string, string[]> $examples
* @param array<string, string[]> $fieldInfo
* @param array<string> $rulesOrder
* @param string $summary
* @param string $description
* @param string $tag
*/
public function __construct(
string $uri,
Expand All @@ -136,6 +177,11 @@ public function __construct(
array $rules,
string $docBlock,
array $examples,
array $fieldInfo,
array $rulesOrder,
string $summary,
string $description,
string $tag
) {
$this->uri = $uri;
$this->methods = $methods;
Expand All @@ -148,7 +194,12 @@ public function __construct(
$this->rules = $rules;
$this->docBlock = $docBlock;
$this->responses = [];
$this->examples = $rules;
$this->examples = $examples;
$this->fieldInfo = $fieldInfo;
$this->rulesOrder = $rulesOrder;
$this->summary = $summary;
$this->description = $description;
$this->tag = $tag;
}

/**
Expand Down Expand Up @@ -304,9 +355,9 @@ public function getExamples(): array
}

/**
* @param array<string, string[]> $rules
* @param array<string, string[]> $examples
*/
public function setExamples(array $rules): void
public function setExamples(array $examples): void
{
$this->examples = $examples;
}
Expand Down Expand Up @@ -375,10 +426,79 @@ public function clone(): Doc
return clone $this;
}

/**
* @return array<string, string[]>
*/
public function getFieldInfo(): array
{
return $this->fieldInfo;
}

/**
* @param array<string, string[]> $fieldInfo
*/
public function setFieldInfo(array $fieldInfo): void
{
foreach ($fieldInfo as $key => $item) {
$this->fieldInfo[$key] = [
'description' => $item,
'example' => $this->examples[$key] ?? ''
];
}
}

/**
* @return array<string>
*/
public function getRulesOrder(): array
{
return $this->rulesOrder;
}

/**
* @param array<string> $rulesOrder
*/
public function setRulesOrder(array $rulesOrder): void
{
$this->rulesOrder = $rulesOrder;
}

public function getSummary(): string
{
return $this->summary;
}

public function setSummary(string $summary): void
{
$this->summary = $summary;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

public function getTag(): string
{
return $this->tag;
}

public function setTag(string $tag): void
{
$this->tag = $tag;
}

public function toArray(): array
{
$result = [
'uri' => $this->uri,
'summary' => $this->summary,
'description' => $this->description,
'middlewares' => $this->middlewares,
'controller' => $this->controller,
'controller_full_path' => $this->controllerFullPath,
Expand All @@ -389,6 +509,9 @@ public function toArray(): array
'doc_block' => $this->docBlock,
'responses' => $this->responses,
'examples' => $this->examples,
'field_info' => $this->fieldInfo,
'rules_order' => $this->rulesOrder,
'tag' => $this->tag,
];

if (isset($this->group)) {
Expand Down
Loading
Loading